use different ical prefix, better print diffs
Periodic refresh of events / Run-Scraper (push) Waiting to run
Periodic refresh of events / Run-Scraper (push) Waiting to run
This commit is contained in:
@@ -7,7 +7,8 @@ import aiohttp
|
||||
import json
|
||||
import os
|
||||
import base64
|
||||
import time
|
||||
import jsondiff as jd
|
||||
import pprint
|
||||
|
||||
from google.oauth2 import service_account
|
||||
from googleapiclient.discovery import build
|
||||
@@ -22,13 +23,14 @@ BATCH_LIMIT = 190
|
||||
async def clear_sakuracon_events(config_path="config/sakuracon.json"):
|
||||
with open(config_path) as f:
|
||||
cfg = json.load(f)
|
||||
print(cfg)
|
||||
print("using config:")
|
||||
pprint.pp(cfg, indent=2)
|
||||
clear_gcals(cfg)
|
||||
|
||||
async def collect_sakuracon_events(config_path="config/sakuracon.json"):
|
||||
with open(config_path) as f:
|
||||
cfg = json.load(f)
|
||||
print(cfg)
|
||||
pprint.pp(cfg, indent=2)
|
||||
events, tracks = await get_event_data()
|
||||
cals = convert_events_to_icals(events, tracks, cfg)
|
||||
write_ics(cals)
|
||||
@@ -103,15 +105,16 @@ async def insert_fields(events):
|
||||
event["description"] = f"Track: {event['tag_title']}\n{tags_str}\n{desc}"
|
||||
return events
|
||||
|
||||
def batch_create_handler(req_id, resp, exception):
|
||||
def batch_create_handler(_, resp, exception):
|
||||
if exception is not None:
|
||||
print(exception)
|
||||
return
|
||||
print(f"Event created: g {resp['id']}")
|
||||
|
||||
def batch_exception_handler(req_id, resp, exception):
|
||||
def batch_exception_handler(_, resp, exception):
|
||||
if exception is not None:
|
||||
print(exception)
|
||||
pprint.pp(resp)
|
||||
pprint.pp(exception)
|
||||
|
||||
def convert_events_to_icals(all_events, all_tracks, cfg) -> dict[str, Calendar]:
|
||||
# Group events by track_title
|
||||
@@ -211,6 +214,12 @@ def update_gcal(scraped_events, tracks, cfg):
|
||||
for e in events:
|
||||
ical_uid = uid_prefix + e["id"]
|
||||
try: # sometimes events have weird start/end times that fail gcal's validation, but that shouldn't kill our vibe
|
||||
start = datetime.fromisoformat(e["start_calendar"])
|
||||
end = datetime.fromisoformat(e["end_calendar"])
|
||||
|
||||
if start >= end:
|
||||
raise Exception((f"Event {e['title']} (uid: {ical_uid}) has an invalid duration, {start} -> {end}"))
|
||||
|
||||
e_content = {
|
||||
"summary": e["title"],
|
||||
"start": e["start_calendar"] + tz_suffix,
|
||||
@@ -249,33 +258,32 @@ def update_gcal(scraped_events, tracks, cfg):
|
||||
|
||||
# if changes exist, update existing event
|
||||
if not changed:
|
||||
print(f"Event {e['title']} seems to be the same, leaving as is")
|
||||
print(f"Event {e['title']} (uid: {ical_uid}) seems to be the same, leaving as is")
|
||||
else:
|
||||
print(f"Updating event {e['title']} (ical: {ical_uid}/g: {g_e['id']})")
|
||||
pprint.pp(jd.diff(g_content, e_content, syntax="symmetric"), depth=3)
|
||||
# print(e_content)
|
||||
# print(g_content)
|
||||
g_e.update(gcal_body)
|
||||
batch.add(service.events().patch(calendarId=track_gcal_id, eventId=g_e["id"], body=g_e), callback=batch_exception_handler)
|
||||
batchlen += 1
|
||||
else: # if event is new, insert
|
||||
print(f"Event with uid {ical_uid} seems to be new, adding...")
|
||||
service.events().insert(calendarId=track_gcal_id, body=gcal_body).execute()
|
||||
#batch.add(service.events().insert(calendarId=track_gcal_id, body=gcal_body), callback=batch_create_handler)
|
||||
#batchlen += 1
|
||||
print(f"Event {e['title']} (uid: {ical_uid}) seems to be new, adding...")
|
||||
#service.events().insert(calendarId=track_gcal_id, body=gcal_body).execute()
|
||||
batch.add(service.events().insert(calendarId=track_gcal_id, body=gcal_body), callback=batch_create_handler)
|
||||
batchlen += 1
|
||||
|
||||
if batchlen == BATCH_LIMIT:
|
||||
batch.execute()
|
||||
batchlen = 0
|
||||
batch = service.new_batch_http_request()
|
||||
print("batch zzz")
|
||||
time.sleep(60)
|
||||
except HttpError as error:
|
||||
print(f"An error occurred: {error}")
|
||||
print(f"An error occurred while processing the event: {error}")
|
||||
print(gcal_body)
|
||||
except Exception as error:
|
||||
print(f"An error occurred while processing the event: {error}")
|
||||
batch.execute()
|
||||
batchlen = 0
|
||||
print("batch zzz")
|
||||
time.sleep(60)
|
||||
|
||||
# delete all gcal_events which are not in the events array
|
||||
# we have the list of gcal events
|
||||
@@ -285,24 +293,13 @@ def update_gcal(scraped_events, tracks, cfg):
|
||||
|
||||
#print(gcalevent_gcal_map)
|
||||
|
||||
batch = service.new_batch_http_request()
|
||||
batchlen = 0
|
||||
# dont bother batching the deletes b/c cal has to be same in batch, lazy...
|
||||
for event in gcal_events:
|
||||
#print(event)
|
||||
#print(f"icaluid: {event.get('iCalUID')}")
|
||||
if "iCalUID" not in event or event["iCalUID"] not in uids and event["status"] != "cancelled":
|
||||
cal = gcalevent_gcal_map[event["id"]]
|
||||
print(f"gcal event g {event['id']} not in latest scrape, deleting...")
|
||||
|
||||
batch.add(service.events().delete(calendarId=cal, eventId=event["id"]), callback=batch_exception_handler)
|
||||
batchlen += 1
|
||||
if batchlen == BATCH_LIMIT:
|
||||
batch.execute()
|
||||
batch = service.new_batch_http_request()
|
||||
batchlen = 0
|
||||
batch.execute()
|
||||
batch = service.new_batch_http_request()
|
||||
batchlen = 0
|
||||
service.events().delete(calendarId=cal, eventId=event["id"]).execute()
|
||||
|
||||
except HttpError as error:
|
||||
print(f"An error occurred: {error}")
|
||||
|
||||
Reference in New Issue
Block a user