-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdownload.py
More file actions
94 lines (73 loc) · 2.54 KB
/
download.py
File metadata and controls
94 lines (73 loc) · 2.54 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
import json
from argparse import ArgumentParser
from typing import Any
import requests
from tqdm import tqdm
from src.config import Config
parser = ArgumentParser(description="Download Pretalx data for EuroPython processing.")
parser.add_argument(
"-e",
"--exclude",
choices=["schedule", "youtube"],
action="append",
help="Exclude certain resources from download.",
)
args = parser.parse_args()
exclude = set(args.exclude or [])
headers = {
"Accept": "application/json, text/javascript",
"Authorization": f"Token {Config.token()}",
"Pretalx-Version": Config.api_version,
}
base_url = f"https://pretalx.com/api/events/{Config.event}/"
schedule_url = (
base_url
+ "schedules/latest?expand="
+ "slots,slots.submission,slots.submission.submission_type,slots.submission.track,slots.room"
)
# Build resource list dynamically based on exclusions
resources = [
"submissions?state=confirmed&expand=answers.question,submission_type,track,slots.room,resources",
"speakers?expand=answers.question",
]
if "youtube" not in exclude:
resources.append("p/youtube")
Config.raw_path.mkdir(parents=True, exist_ok=True)
for resource in resources:
# To get the resource name without extra parameters
resource_name = resource.split("?")[0].split("/")[-1]
url = base_url + resource
res0: list[dict[str, Any]] = []
data: dict[str, Any] = {"next": url}
n = 0
pbar = tqdm(desc=f"Downloading {resource_name}", unit=" page", dynamic_ncols=True)
while url := data["next"]:
n += 1
pbar.update(1)
response = (
requests.get(url)
if resource_name == "youtube"
else requests.get(url, headers=headers)
)
if response.status_code != 200:
raise Exception(f"Error {response.status_code}: {response.text}")
data = response.json()
res0 += data["results"]
pbar.close()
# Save the data to a file
filename = f"{resource_name}_latest.json"
filepath = Config.raw_path / filename
with open(filepath, "w") as fd:
json.dump(res0, fd)
# Download schedule unless excluded
if "schedule" not in exclude:
print("Downloading schedule...", end="")
response = requests.get(schedule_url, headers=headers)
if response.status_code != 200:
raise Exception(f"Error {response.status_code}: {response.text}")
data = response.json()
filename = "schedule_latest.json"
filepath = Config.raw_path / filename
with open(filepath, "w") as fd:
json.dump(data, fd)
print(" done.")