Skip to content

Commit

Permalink
Make script more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
ianfab committed Sep 5, 2024
1 parent 7eb810f commit 606bc44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion _data/tournaments.tsv
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
start-date end-date variant location tournament source
2024-08-31 2024-09-01 Shogi "House of Culture ""Iskar"", Sofia, Bulgaria" 4th Shogi Cup Sofia <a href='http://fesashogi.eu/index.php?mid=2'>fesashogi.eu</a>
2024-09-07 2024-09-07 Shogi Fundacja NAMI, Wroclaw, Poland Shogi no Nami 2024 <a href='http://fesashogi.eu/index.php?mid=2'>fesashogi.eu</a>
2024-09-09 2024-09-09 Xiangqi - Xiangqitreffen online <a href='http://chinaschach.de/blog/event/xiangqitreffen-online-6/'>chinaschach.de</a>
2024-09-21 2024-09-21 Shogi Berlin-Mitte, Germany 3rd Moyu Tournament Berlin <a href='http://fesashogi.eu/index.php?mid=2'>fesashogi.eu</a>
Expand All @@ -11,6 +10,7 @@ start-date end-date variant location tournament source
2024-10-14 2024-10-14 Xiangqi - Xiangqitreffen online <a href='http://chinaschach.de/blog/event/xiangqitreffen-online-7/'>chinaschach.de</a>
2024-10-19 2024-10-19 Shogi Düsseldorf 3. Rheinland Cup <a href='http://fesashogi.eu/index.php?mid=2'>fesashogi.eu</a>
2024-10-26 2024-10-27 Shogi London Mindsports Centre, 21 Dalling Rd, JD, Great Britain 2024 British Open Championship <a href='http://fesashogi.eu/index.php?mid=2'>fesashogi.eu</a>
2024-11-08 2024-11-12 Janggi Incheon, Korea Janggi World Championship <a href='https://www.facebook.com/events/386564981136316/'>facebook.com</a>
2024-11-11 2024-11-11 Shogi Fundacja NAMI, Wroclaw, Poland XI Turniej Shogi z okazji Swieta Niepodleglosci <a href='http://fesashogi.eu/index.php?mid=2'>fesashogi.eu</a>
2024-11-11 2024-11-11 Xiangqi - Xiangqitreffen online <a href='http://chinaschach.de/blog/event/xiangqitreffen-online-8/'>chinaschach.de</a>
2024-11-16 2024-11-17 Shogi B, The Hague, The Netherlands Hague Shogi and Beer 2024 <a href='http://fesashogi.eu/index.php?mid=2'>fesashogi.eu</a>
Expand Down
36 changes: 20 additions & 16 deletions _scripts/update_tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import urlparse
import re
import sys
import warnings

import icalendar
import requests
Expand Down Expand Up @@ -39,20 +40,23 @@ def render_link(url):


def get_ics_calendar(url, columns, variants):
ics = get_content(url)
gcal = icalendar.Calendar.from_ical(ics)
tournaments = []
for component in gcal.walk():
if component.name == "VEVENT":
tournament_url = component.get('url') or url
tournaments.append([
component.decoded('dtstart').strftime('%Y-%m-%d'),
component.decoded('dtend').strftime('%Y-%m-%d'),
get_variant(component.get('summary'), variants),
component.get('location'),
component.get('summary'),
render_link(tournament_url)
])
ics = get_content(url)
if ics:
gcal = icalendar.Calendar.from_ical(ics)
for component in gcal.walk():
if component.name == "VEVENT":
tournament_url = component.get('url') or url
tournaments.append([
component.decoded('dtstart').strftime('%Y-%m-%d'),
component.decoded('dtend').strftime('%Y-%m-%d'),
get_variant(component.get('summary'), variants),
component.get('location'),
component.get('summary'),
render_link(tournament_url)
])
else:
warnings.warn(f'{url} does not return events')
return pd.DataFrame(tournaments, columns=columns)


Expand All @@ -62,12 +66,12 @@ def get_html_calendar(url, columns):
assert len(df_list) == 1
df = df_list[-1]
df.columns = ['Date', 'Event', 'Place', 'Info']
# add year
# add year from section headers
year_rows = df[df.Date.str.contains('^\d+$', regex=True, na=False)]
years = tuple((row['Date'], index) for index, row in year_rows.iterrows())
df['Year'] = years[0][0]
for year, i in years:
df['Year'][i:] = year
df.loc[i:, 'Year'] = year
df = df.drop(year_rows.index, axis=0)
# reformat date
df = df[df['Date'] != 'Date']
Expand Down Expand Up @@ -113,7 +117,7 @@ def prettify_location(locations):
get_ics_calendar(TOURNEY_MOMENTUMS_URL, current.columns, ('shogi', 'xiangqi', 'janggi', 'makruk')),
get_ics_calendar(DXB_URL, current.columns, ('xiangqi',)),
get_ics_calendar(FFS_URL, current.columns, ('shogi',)),
# get_ics_calendar(SNK_URL, current.columns, ('shogi',)),
get_ics_calendar(SNK_URL, current.columns, ('shogi',)),
get_html_calendar(FESA_URL, current.columns),
])
merged = pd.concat(calendars)
Expand Down

0 comments on commit 606bc44

Please sign in to comment.