-
Notifications
You must be signed in to change notification settings - Fork 0
Pretalx data #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Pretalx data #30
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3814ffd
first draft of downloading and storing the pretalx data
artcz 9672021
add basic admin support
artcz 7265cec
small refactoring and review feedback
artcz 8e7fac9
PretalxData admin sanity check
artcz 47f9024
fix urls
artcz 8dbb561
add schema migration
artcz f8e1aa7
fix lint
artcz 5bf441c
fix format
artcz 58aa3c5
add management command
artcz 9461e9c
add basic support for cron jobs
artcz 7fafb65
fix typo
artcz 01b03d0
tweak pagination
artcz c4136b6
remove extra breakpoint
artcz 86fd6ab
Update intbot/core/integrations/pretalx.py
artcz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
- name: Scheduled tasks using the bot user | ||
hosts: intbot_app | ||
|
||
tasks: | ||
- name: "Download pretalx data every hour" | ||
ansible.builtin.cron: | ||
name: "Download pretalx data every hour" | ||
minute: "5" # run on the 5th minute of every hour | ||
job: "make prod/cron/pretalx" |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
MAKE_APP="docker compose run app make" | ||
|
||
echo: | ||
"Dummy target, to not run something accidentally" | ||
|
||
prod/migrate: | ||
docker compose run app make in-container/migrate | ||
$(MAKE_APP) in-container/migrate | ||
|
||
prod/shell: | ||
docker compose run app make in-container/shell | ||
$(MAKE_APP) in-container/shell | ||
|
||
prod/db_shell: | ||
docker compose run app make in-container/db_shell | ||
$(MAKE_APP) in-container/db_shell | ||
|
||
prod/manage: | ||
docker compose run app make in-container/manage ARG=$(ARG) | ||
$(MAKE_APP) in-container/manage ARG=$(ARG) | ||
|
||
prod/cron/pretalx: | ||
$(MAKE_APP) in-container/manage ARG="download_pretalx_data --event=europython-2025" | ||
|
||
logs: | ||
docker compose logs -f |
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import logging | ||
from typing import Any | ||
|
||
import httpx | ||
from core.models import PretalxData | ||
from django.conf import settings | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
PRETALX_EVENTS = [ | ||
"europython-2022", | ||
"europython-2023", | ||
"europython-2024", | ||
"europython-2025", | ||
] | ||
|
||
ENDPOINTS = { | ||
# Questions need to be passed to include answers in the same endpoint, | ||
# saving us later time with joining the answers. | ||
PretalxData.PretalxResources.submissions: "submissions/?questions=all", | ||
PretalxData.PretalxResources.speakers: "speakers/?questions=all", | ||
} | ||
|
||
|
||
JsonType = dict[str, Any] | ||
|
||
|
||
def get_event_url(event): | ||
assert event in PRETALX_EVENTS | ||
|
||
return f"https://pretalx.com/api/events/{event}/" | ||
|
||
|
||
def fetch_pretalx_data( | ||
event: str, resource: PretalxData.PretalxResources | ||
) -> list[JsonType]: | ||
headers = { | ||
"Authorization": f"Token {settings.PRETALX_API_TOKEN}", | ||
"Content-Type": "application/json", | ||
} | ||
|
||
base_url = get_event_url(event) | ||
endpoint = ENDPOINTS[resource] | ||
url = f"{base_url}{endpoint}" | ||
|
||
# Pretalx paginates the output, so we will need to do multiple requests and | ||
# then merge multiple pages to one big dictionary | ||
results = [] | ||
page = 0 | ||
|
||
# This takes advantage of the fact that url will contain a url to the | ||
# next page, until there is more data to fetch. If this is the last page, | ||
# then the url will be None (falsy), and thus stop the while loop. | ||
while url: | ||
page += 1 | ||
response = httpx.get(url, headers=headers) | ||
|
||
if response.status_code != 200: | ||
raise Exception(f"Error {response.status_code}: {response.text}") | ||
|
||
logger.info("Fetching data from %s, page %s", url, page) | ||
|
||
data = response.json() | ||
results += data["results"] | ||
url = data["next"] | ||
|
||
return results | ||
|
||
|
||
def download_latest_submissions(event: str) -> PretalxData: | ||
data = fetch_pretalx_data(event, PretalxData.PretalxResources.submissions) | ||
|
||
pretalx_data = PretalxData.objects.create( | ||
resource=PretalxData.PretalxResources.submissions, | ||
content=data, | ||
) | ||
|
||
return pretalx_data | ||
|
||
|
||
def download_latest_speakers(event: str) -> PretalxData: | ||
data = fetch_pretalx_data(event, PretalxData.PretalxResources.speakers) | ||
|
||
pretalx_data = PretalxData.objects.create( | ||
resource=PretalxData.PretalxResources.speakers, | ||
content=data, | ||
) | ||
|
||
return pretalx_data |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from core.integrations.pretalx import ( | ||
PRETALX_EVENTS, | ||
download_latest_speakers, | ||
download_latest_submissions, | ||
) | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Downloads latest pretalx data" | ||
|
||
def add_arguments(self, parser): | ||
# Add keyword argument event | ||
parser.add_argument( | ||
"--event", | ||
choices=PRETALX_EVENTS, | ||
help="slug of the event (for example `europython-2025`)", | ||
required=True, | ||
) | ||
|
||
def handle(self, **kwargs): | ||
event = kwargs["event"] | ||
|
||
self.stdout.write(f"Downloading latest speakers from pretalx... {event}") | ||
download_latest_speakers(event) | ||
|
||
self.stdout.write(f"Downloading latest submissions from pretalx... {event}") | ||
download_latest_submissions(event) |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Generated by Django 5.1.4 on 2025-04-18 11:43 | ||
|
||
import uuid | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("core", "0004_add_inbox_item_model"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="PretalxData", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("uuid", models.UUIDField(default=uuid.uuid4)), | ||
( | ||
"resource", | ||
models.CharField( | ||
choices=[ | ||
("submissions", "Submissions"), | ||
("speakers", "Speakers"), | ||
("schedule", "Schedule"), | ||
], | ||
max_length=255, | ||
), | ||
), | ||
("content", models.JSONField()), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("modified_at", models.DateTimeField(auto_now=True)), | ||
("processed_at", models.DateTimeField(blank=True, null=True)), | ||
], | ||
), | ||
] |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.