Repository navigation
Expand file tree
/
Copy pathrun.py
More file actions
191 lines (176 loc) · 9.06 KB
/
Copy pathrun.py
File metadata and controls
191 lines (176 loc) · 9.06 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
"""ImpactGo — ingest ปฏิทินอิมแพ็ค + คำนวณระดับ + สร้างเว็บ"""
import datetime as dt
import os
import sys
from zoneinfo import ZoneInfo
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import alert, assets, build_site, db, details, fetch, holidays, normalize as N, parse_exhibition, parse_impact, parse_ttm, score, share_card # noqa: E402
BASE = "https://www.impact.co.th/th/visitors/event-calendar"
TTM_URL = "https://m.thaiticketmajor.com/tag/Impact%20Arena"
SNAP = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "snapshots")
ALERT_STATE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "alert_state.json")
HORIZON_WEEKS = 12 # สัปดาห์ที่แสดง — score ต้องคำนวณให้ครบเท่ากัน ไม่งั้นวันท้าย ๆ ขึ้น "ว่าง" ปลอม
def main():
tz = ZoneInfo("Asia/Bangkok")
today = dt.datetime.now(tz).date()
con = db.connect()
db.init_db(con)
src = db.source_id(con, "impact")
all_events, seen = [], set()
for page, start in enumerate(range(0, 24 * 8, 24), 1):
url = BASE if start == 0 else f"{BASE}?start={start}"
try:
status, data = fetch.fetch(url)
except Exception as e: # noqa: BLE001
print(f"[warn] page {page} fetch failed: {e}")
break
digest, path = fetch.save_snapshot(data, SNAP, f"impact-p{page}")
cur = con.execute(
"""INSERT OR IGNORE INTO raw_document (source_id,url,fetched_at,http_status,content_hash,storage_path,bytes)
VALUES (?,?,?,?,?,?,?)""",
(src, url, dt.datetime.now(tz).isoformat(timespec="seconds"), status, digest, path, len(data)),
)
new_doc = cur.rowcount > 0
events = parse_impact.parse_page(data.decode("utf-8", "ignore"))
fresh = [e for e in events if e["source_ref"] not in {x["source_ref"] for x in all_events}]
print(f"[page {page}] {len(events)} cards, {len(fresh)} new")
if new_doc:
doc_id = con.execute(
"SELECT id FROM raw_document WHERE source_id=? AND url=? AND content_hash=?",
(src, url, digest),
).fetchone()["id"]
for e in events:
con.execute(
"""INSERT INTO event_raw (raw_document_id,title_raw,date_text,venue_text,price_text,detail_url)
VALUES (?,?,?,?,?,?)""",
(doc_id, e["title"], e["date_text"], e["venue_text"],
"" if e["is_free"] is None else str(e["is_free"]), e["url"]),
)
if not events or not fresh:
break
all_events += fresh
for e in fresh:
eid = db.upsert_event(con, e, src, verified=dt.datetime.now(tz).isoformat(timespec="seconds"))
seen.add(str(eid))
con.commit()
# ---- แหล่งที่ 2: ThaiTicketMajor ----
ttm_src = db.source_id(con, "ttm")
ttm_ok = False
try:
status, data = fetch.fetch_curl(TTM_URL)
digest, path = fetch.save_snapshot(data, SNAP, "ttm-p1")
con.execute(
"""INSERT OR IGNORE INTO raw_document (source_id,url,fetched_at,http_status,content_hash,storage_path,bytes)
VALUES (?,?,?,?,?,?,?)""",
(ttm_src, TTM_URL, dt.datetime.now(tz).isoformat(timespec="seconds"), status, digest, path, len(data)),
)
ttm_events = [e for e in parse_ttm.parse_page(data.decode("utf-8", "ignore")) if e["end"] >= today]
added, merged = 0, 0
for e in ttm_events:
eid = db.find_by_source_ref(con, ttm_src, e["source_ref"])
if eid:
row = con.execute("SELECT title FROM event WHERE id=?", (eid,)).fetchone()
if len(e["title"]) > len(row["title"]) + 2:
db.update_title(con, eid, e["title"])
db.attach_source(con, eid, ttm_src, e["source_ref"], e["title"])
continue
eid = db.find_identity(con, db.identity_hash(e["title"], e["venue_code"], e["start"]))
if eid:
row = con.execute("SELECT title FROM event WHERE id=?", (eid,)).fetchone()
if len(e["title"]) > len(row["title"]) + 2:
db.update_title(con, eid, e["title"])
db.attach_source(con, eid, ttm_src, e["source_ref"], e["title"])
continue
cands = db.find_candidates(con, e["venue_code"], e["start"], e["end"])
if len(cands) == 1:
eid = cands[0]
row = con.execute("SELECT title FROM event WHERE id=?", (eid,)).fetchone()
if len(e["title"]) > len(row["title"]) + 2:
db.update_title(con, eid, e["title"])
db.attach_source(con, eid, ttm_src, e["source_ref"], e["title"])
merged += 1
else:
eid = db.upsert_event(con, e, ttm_src, verified=dt.datetime.now(tz).isoformat(timespec="seconds"))
added += 1
seen.add(str(eid))
con.commit()
print(f"[ttm] {len(ttm_events)} upcoming, merged={merged}, new={added}")
ttm_ok = True
except Exception as exc: # noqa: BLE001
print(f"[warn] ttm failed: {exc}")
# ---- แหล่งที่ 3: ThailandExhibition (API) ----
ex_src = db.source_id(con, "exhibition")
ex_ok = False
try:
ex_events = [e for e in parse_exhibition.fetch_events(since=today - dt.timedelta(days=1)) if e["end"] >= today]
added, merged, queued = 0, 0, 0
for e in ex_events:
known = db.find_by_source_ref(con, ex_src, e["source_ref"])
if known:
# backfill ลิงก์หน้างานให้งานเดิม (งานแรก ๆ เก็บมาตอน API ไม่ map url)
if e["url"]:
con.execute("UPDATE event SET detail_url=? WHERE id=? AND (detail_url IS NULL OR detail_url='')",
(e["url"], known))
continue
cands = db.find_candidates_by_dates(con, e["start"], e["end"])
best, best_sim = None, 0.0
for c in cands:
sim = N.token_sim(e["title"], c["title"]) + (0.1 if c["venue_code"] == e["venue_code"] else 0)
if sim > best_sim:
best, best_sim = c["id"], sim
if best and best_sim >= 0.6:
db.attach_source(con, best, ex_src, e["source_ref"], e["title"])
merged += 1
else:
eid = db.upsert_event(con, e, ex_src, verified=dt.datetime.now(tz).isoformat(timespec="seconds"))
added += 1
if cands:
db.add_review(con, eid, "possible_duplicate", 2)
queued += 1
con.commit()
print(f"[exhibition] {len(ex_events)} upcoming, merged={merged}, new={added}, queued={queued}")
ex_ok = True
except Exception as exc: # noqa: BLE001
print(f"[warn] exhibition failed: {exc}")
if len(all_events) < 10:
print("[abort] parsed too few events — keep previous data")
_, msgs = alert.end_of_run(ALERT_STATE, {"impact": False, "ttm": ttm_ok, "exhibition": ex_ok})
alert.notify(msgs)
return 1
try:
n_hol = holidays.refresh(con, today=today)
if n_hol:
print(f"[holidays] refreshed {n_hol}")
except Exception as exc: # noqa: BLE001
print(f"[warn] holidays failed: {exc}")
try:
n_desc = details.refresh(con, cap=8)
if n_desc:
print(f"[details] filled {n_desc}")
except Exception as exc: # noqa: BLE001
print(f"[warn] details failed: {exc}")
tentative = db.purge_missing(con, src, seen, today)
score.recompute(con, today, HORIZON_WEEKS * 7)
build_id = "%s-%d" % (dt.datetime.now().strftime("%Y%m%d-%H%M"), 0)
try:
assets.build(os.path.join(os.path.dirname(os.path.abspath(__file__)), "site"), build_id)
print("[assets] icons + manifest + sw")
except Exception as exc: # noqa: BLE001
print(f"[warn] assets failed: {exc}")
try:
cards = share_card.build_cards(con, today, weeks=8)
print(f"[share] {len(cards)} cards")
except Exception as exc: # noqa: BLE001
print(f"[warn] share cards failed: {exc}")
n = build_site.build(con, today, weeks=HORIZON_WEEKS)
con.commit()
lv = dict(con.execute("SELECT level, count(*) c FROM day_score WHERE date>=? GROUP BY level", (today.isoformat(),)).fetchall())
print(f"[done] events={n} (new this run: {len(all_events)}) missing→tentative={tentative}")
print(f"[done] levels: {dict(sorted(lv.items()))}")
print(f"[done] site: {os.path.join(os.path.dirname(os.path.abspath(__file__)), 'site', 'index.html')}")
_, msgs = alert.end_of_run(ALERT_STATE, {"impact": True, "ttm": ttm_ok, "exhibition": ex_ok}, total=n)
alert.notify(msgs)
return 0
if __name__ == "__main__":
raise SystemExit(main())