Skip to content

Commit

Permalink
[WIP] feat: use api info to populate landing page automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnikaStein committed Mar 9, 2024
1 parent 48afc51 commit bc8a12f
Showing 1 changed file with 176 additions and 0 deletions.
176 changes: 176 additions & 0 deletions notebooks/prototype.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ede2f694-01ad-4a8a-9f2e-b1e7c6fbb069",
"metadata": {},
"source": [
"# Learning to use some APIs for Dummies\n",
"*or, Annika learns that you do not need to download the entire database*\n",
"### Today: the unofficial wca-rest-api, and wcif"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e720ab5-1ebf-4e20-ba92-c616d42060ee",
"metadata": {},
"outputs": [],
"source": [
"# get remote files with urls\n",
"import urllib.request as libreq"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42cc1354-e120-43c5-834e-44eab73e9b02",
"metadata": {},
"outputs": [],
"source": [
"# tool to read in the response as json\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a8f04baf-3906-4001-8dc3-6c8699dd57d6",
"metadata": {},
"outputs": [],
"source": [
"# unofficial api, example returns all competitions in a given country\n",
"with libreq.urlopen('https://raw.githubusercontent.com/robiningelbrecht/wca-rest-api/master/api/competitions/DE.json') as file:\n",
" # the file is understood as json\n",
" json_data = json.load(file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b460dd9-fdd9-4477-a0f0-3367e6e814a9",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"# and that's how it looks\n",
"json_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb323343-828c-44dd-828e-3f5728802adf",
"metadata": {},
"outputs": [],
"source": [
"# want to know starting and end times, and want to perform some calls once a week to populate my landing page\n",
"import datetime\n",
"# when the script runs\n",
"now = datetime.datetime.now()\n",
"# up until a week from now\n",
"d = datetime.timedelta(days=7)\n",
"oneweek_from_now = now + d\n",
"\n",
"# formatting into something close to js syntax for UTC time\n",
"today = now.strftime(\"%Y-%m-%d\")\n",
"oneweek_from_today = oneweek_from_now.strftime(\"%Y-%m-%d\")\n",
"print(today)\n",
"print(oneweek_from_today)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25859561-75ab-4910-8ef3-717ddba903c7",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"# assume this script runs, say every Tuesday.\n",
"# and this will hold the relevant information per compid\n",
"upcoming_comp_info = []\n",
"# nested json, get the actual items and loop over them\n",
"for k in json_data['items']:\n",
" # only those within one week from now\n",
" if k['date']['from'] >= today and k['date']['from'] < oneweek_from_today:\n",
" print(k)\n",
" compid = k['id']\n",
" # for every compid, there is the wcif with further info,\n",
" # parse this as well (inside the loop for every comp that fulfils the criteria)\n",
" with libreq.urlopen(f'https://worldcubeassociation.org/api/v0/competitions/{compid}/wcif/public') as wcif:\n",
" comp_data = json.load(wcif)\n",
" \n",
" # walking through the nested json, pick first item wherever possible to not overcomplicate things\n",
" # grab the earliest activity to determine from when to show the corresponding div (UTC)\n",
" starts = min([a['startTime'] for a in comp_data['schedule']['venues'][0]['rooms'][0]['activities']]).split('T')[1][:-1]\n",
" # grab the latest activity to determine until when to show the corresponding div (UTC)\n",
" ends = max([a['endTime'] for a in comp_data['schedule']['venues'][0]['rooms'][0]['activities']]).split('T')[1][:-1]\n",
" \n",
" # collect everything into a 2d list, one sublist per comp\n",
" upcoming_comp_info.append([compid,\n",
" k['date']['from'],\n",
" k['date']['till'],\n",
" starts,\n",
" ends,\n",
" k['venue']['coordinates']['latitude'],\n",
" k['venue']['coordinates']['longitude']])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cba58e81-d870-4fdb-b14f-de5f4bbcdd2d",
"metadata": {},
"outputs": [],
"source": [
"# now we know the IDs of comps taking place the upcoming weekend, and relevant info to steer the UI\n",
"upcoming_comp_info"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "627d320f-8b62-44ce-86b4-235df84f068d",
"metadata": {},
"outputs": [],
"source": [
"if len(upcoming_comp_info) > 1:\n",
" print('Multi-Comp Weekend!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c0034c1-d139-444c-a5f6-447a8fd5c295",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit bc8a12f

Please sign in to comment.