Last active
February 17, 2024 22:53
-
-
Save DragoonAethis/6b17cf25872685991e75f291a1376a8a to your computer and use it in GitHub Desktop.
Remcon 2024 - Timeline to XLSX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import json | |
from datetime import datetime, timedelta | |
import requests | |
import xlsxwriter | |
DEBUG_DUMP_JSON = False | |
def clean_text(text: str): | |
text = re.sub(r'\\"', '"', text) | |
text = re.sub(r'(\\r|\\n)+', '\n', text) | |
return text.strip() | |
class Event: | |
start_date: datetime | |
duration: int # always 30... | |
room: int | |
name: str | |
author: str | |
description: str | |
h: int | |
x: int | |
y: int | |
def __init__(self, data: dict): | |
self.start_date = datetime.strptime(data["startDate"], '%a, %d %b %Y %H:%M:%S %Z') | |
#self.duration = int(data["time"]) | |
self.room = data["room"] | |
self.name = data["name"] | |
self.author = data["author"] | |
self.description = data["description"] | |
self.h = int(data["h"]) | |
self.x = int(data["x"]) | |
self.y = int(data["y"]) | |
def get_current_build(): | |
r = requests.get("https://remcon.pl") | |
r.raise_for_status() | |
return re.search(r"\"buildId\":\"(?P<id>[A-Za-z0-9_-]+)\"", r.text).group('id') | |
def get_timeline_data_from_website(): | |
r = requests.get(f"https://remcon.pl/_next/data/{get_current_build()}/timeline.json") | |
data = r.json() | |
if DEBUG_DUMP_JSON: | |
with open("data.json", "w") as f: | |
json.dump(data, f, indent=4) | |
return data | |
def get_timeline_data_from_file(filename="timeline.json"): | |
with open("timeline.json") as f: | |
return json.load(f) | |
#data = get_timeline_data_from_file() | |
data = get_timeline_data_from_website() | |
event_data = data["pageProps"]["timeline"]["events"] | |
events = [Event(event) for event in event_data] | |
earliest = events[0].start_date | |
latest = events[0].start_date | |
for event in events: | |
earliest = min(earliest, event.start_date) | |
latest = max(latest, event.start_date) | |
room_data = data["pageProps"]["timeline"]["rooms"] | |
room_to_col = {room["id"]: index + 1 for index, room in enumerate(room_data)} | |
room_to_label = {room["id"]: room["label"] for room in room_data} | |
workbook = xlsxwriter.Workbook("export.xlsx") | |
ws = workbook.add_worksheet() | |
# ws.write(ROW, COLUMN, CONTENT), rows/cols are 0-indexed | |
header_format = workbook.add_format({'bold': True}) | |
event_format = workbook.add_format() | |
event_format.set_align('center') | |
event_format.set_align('vcenter') | |
event_format.set_text_wrap() | |
event_format.set_border() | |
ws.write(0, 0, "Remcon 2024") | |
for room_id, col in room_to_col.items(): | |
ws.write_string(0, col, room_to_label[room_id], cell_format=header_format) | |
date_row = 1 | |
dt = earliest + timedelta(hours=1) # forced TZ | |
while dt < latest + timedelta(hours=4): | |
ws.write_string(date_row, 0, dt.strftime("%H:%M")) | |
dt += timedelta(minutes=30) | |
date_row += 1 | |
for event in events: | |
title = clean_text(f"{event.name.strip()} [{event.author.strip()}]") | |
desc = clean_text(event.description) | |
row = event.y + 1 | |
col = event.x + 1 | |
span = event.h | |
try: | |
if span == 1: | |
ws.write_string(row, col, title, cell_format=event_format) | |
else: | |
ws.merge_range(row, col, row + span - 1, col, title, cell_format=event_format) | |
if desc: | |
ws.write_comment(row, col, desc) | |
except Exception as e: | |
print("[!] FAILURE: ", title) | |
print(e) | |
workbook.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment