|
| 1 | +import logging |
1 | 2 | from typing import Any
|
2 | 3 |
|
3 | 4 | import httpx
|
4 | 5 | from core.models import PretalxData
|
5 | 6 | from django.conf import settings
|
6 | 7 |
|
7 |
| -PRETALX_EVENT = "ep2025" |
8 |
| -base_url = f"https://pretalx.com/api/events/{PRETALX_EVENT}/" |
| 8 | +logger = logging.getLogger(__name__) |
9 | 9 |
|
10 |
| -RESOURCES = { |
| 10 | +PRETALX_EVENTS = [ |
| 11 | + "ep2023", |
| 12 | + "ep2024", |
| 13 | + "ep2025", |
| 14 | +] |
| 15 | + |
| 16 | +ENDPOINTS = { |
11 | 17 | # Questions need to be passed to include answers in the same endpoint,
|
12 | 18 | # saving us later time with joining the answers.
|
13 |
| - PretalxData.PretalxEndpoints.submissions: "submissions?questions=all", |
14 |
| - PretalxData.PretalxEndpoints.speakers: "speakers?questions=all", |
| 19 | + PretalxData.PretalxResources.submissions: "submissions?questions=all", |
| 20 | + PretalxData.PretalxResources.speakers: "speakers?questions=all", |
15 | 21 | }
|
16 | 22 |
|
17 | 23 |
|
18 | 24 | JsonType = dict[str, Any]
|
19 | 25 |
|
20 | 26 |
|
21 |
| -def fetch_pretalx_data(resource) -> list[JsonType]: |
| 27 | +def get_event_url(event): |
| 28 | + assert event in PRETALX_EVENTS |
| 29 | + |
| 30 | + return f"https://pretalx.com/api/events/{event}/" |
| 31 | + |
| 32 | + |
| 33 | +def fetch_pretalx_data(event: str, resource: PretalxData.PretalxResources) -> list[JsonType]: |
22 | 34 | headers = {
|
23 | 35 | "Authorization": f"Token {settings.PRETALX_API_TOKEN}",
|
24 | 36 | "Content-Type": "application/json",
|
25 | 37 | }
|
26 | 38 |
|
27 |
| - endpoint = RESOURCES[resource] |
28 |
| - url = base_url + f"{endpoint}" |
| 39 | + base_url = get_event_url(event) |
| 40 | + endpoint = ENDPOINTS[resource] |
| 41 | + url = f"{base_url}{endpoint}" |
29 | 42 |
|
30 | 43 | # Pretalx paginates the output, so we will need to do multiple requests and
|
31 | 44 | # then merge mutliple pages to one big dictionary
|
32 |
| - res0 = [] |
| 45 | + results = [] |
33 | 46 | data = {"next": url}
|
34 |
| - n = 0 |
| 47 | + page = 0 |
| 48 | + |
| 49 | + # This takes advantage of the fact that "next" will contain a url to the |
| 50 | + # next page, until there is more data to fetch. If this is the last page, |
| 51 | + # then the data["next"] will be None (falsy), and thus stop the while loop. |
35 | 52 | while url := data["next"]:
|
36 |
| - n += 1 |
| 53 | + page += 1 |
37 | 54 | response = httpx.get(url, headers=headers)
|
38 | 55 |
|
39 | 56 | if response.status_code != 200:
|
40 | 57 | raise Exception(f"Error {response.status_code}: {response.text}")
|
41 | 58 |
|
| 59 | + logger.info("Fetching data from %s, page %s", url, page) |
| 60 | + |
42 | 61 | data = response.json()
|
43 |
| - res0 += data["results"] |
| 62 | + results += data["results"] |
44 | 63 |
|
45 |
| - return res0 |
| 64 | + return results |
46 | 65 |
|
47 | 66 |
|
48 |
| -def download_latest_submissions() -> PretalxData: |
49 |
| - data = fetch_pretalx_data(PretalxData.PretalxEndpoints.submissions) |
| 67 | +def download_latest_submissions(event: str) -> PretalxData: |
| 68 | + data = fetch_pretalx_data(event, PretalxData.PretalxResources.submissions) |
50 | 69 |
|
51 | 70 | pretalx_data = PretalxData.objects.create(
|
52 |
| - endpoint=PretalxData.PretalxEndpoints.submissions, |
| 71 | + resource=PretalxData.PretalxResources.submissions, |
53 | 72 | content=data,
|
54 | 73 | )
|
55 | 74 |
|
56 | 75 | return pretalx_data
|
57 | 76 |
|
58 | 77 |
|
59 |
| -def download_latest_speakers() -> PretalxData: |
60 |
| - data = fetch_pretalx_data(PretalxData.PretalxEndpoints.speakers) |
| 78 | +def download_latest_speakers(event: str) -> PretalxData: |
| 79 | + data = fetch_pretalx_data(event, PretalxData.PretalxResources.speakers) |
61 | 80 |
|
62 | 81 | pretalx_data = PretalxData.objects.create(
|
63 |
| - endpoint=PretalxData.PretalxEndpoints.speakers, |
| 82 | + resource=PretalxData.PretalxResources.speakers, |
64 | 83 | content=data,
|
65 | 84 | )
|
66 | 85 |
|
|
0 commit comments