-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
65 lines (54 loc) · 2.08 KB
/
Copy pathstorage.py
File metadata and controls
65 lines (54 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import csv
import os
import threading
from pathlib import Path
from models import BusinessCard, Review
BUSINESSES_FIELDS = [
"card_id", "source", "name", "address", "city", "category",
"phones", "mobile_phones", "rating", "review_count", "working_hours",
"has_website", "website_url", "card_url", "lat", "lon",
"whatsapp", "telegram",
"has_booking", "booking_url",
"pain_point", "pain_point_quote",
"review_summary", "first_message",
"negative_reviews_count", "pain_point_match", "pain_point_evidence",
"rating_min_used", "rating_max_used",
"script_dialog",
]
REVIEWS_FIELDS = ["card_id", "author", "date", "rating", "text"]
_lock = threading.Lock()
def _ensure_file(path: str, fieldnames: list[str]):
p = Path(path)
p.parent.mkdir(parents=True, exist_ok=True)
if not p.exists():
with open(p, "w", newline="", encoding="utf-8-sig") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
def load_saved_urls(businesses_csv: str) -> set[str]:
p = Path(businesses_csv)
if not p.exists():
return set()
with open(p, "r", encoding="utf-8-sig") as f:
reader = csv.DictReader(f)
return {row["card_url"] for row in reader if "card_url" in row}
def append_card(card: BusinessCard, path: str):
_ensure_file(path, BUSINESSES_FIELDS)
with _lock:
with open(path, "a", newline="", encoding="utf-8-sig") as f:
writer = csv.DictWriter(f, fieldnames=BUSINESSES_FIELDS)
writer.writerow(card.to_csv_row())
def append_reviews(reviews: list[Review], path: str):
if not reviews:
return
_ensure_file(path, REVIEWS_FIELDS)
with _lock:
with open(path, "a", newline="", encoding="utf-8-sig") as f:
writer = csv.DictWriter(f, fieldnames=REVIEWS_FIELDS)
for r in reviews:
writer.writerow({
"card_id": r.card_id,
"author": r.author,
"date": r.date,
"rating": r.rating,
"text": r.text,
})