Skip to content

Instantly share code, notes, and snippets.

@matiasb
Created May 30, 2018 16:25
Show Gist options
  • Save matiasb/182010ff2b64c2160f012954f18d3f36 to your computer and use it in GitHub Desktop.
Save matiasb/182010ff2b64c2160f012954f18d3f36 to your computer and use it in GitHub Desktop.
Import Russia 2018 matches into fenics
# run in a fenics django shell; requires tournament already created (slug='rusia-2018')
# it will create teams, add them to the tournament and create the matches
import csv
import requests
import pytz
from datetime import datetime
from django.utils.timezone import get_default_timezone, make_aware
from ega.models import Team, Tournament
tournament = Tournament.objects.get(slug='rusia-2018')
r = requests.get('https://raw.githubusercontent.com/el-ega/worldcup/master/docs/matches/2018.csv')
reader = csv.DictReader(r.text.splitlines())
for row in reader:
when = make_aware(datetime.strptime(row['date'], "%d/%m/%Y %H:%M"), pytz.utc)
if row['round'] == 'Fase de grupos':
home, _ = Team.objects.get_or_create(name=row['home'],
defaults={'code': row['home'][:3].upper(), 'slug': row['home'].lower()})
away, _ = Team.objects.get_or_create(name=row['away'],
defaults={'code': row['away'][:3].upper(), 'slug': row['away'].lower()})
tournament.teams.add(home)
tournament.teams.add(away)
match, _ = tournament.match_set.get_or_create(
home=home, away=away, description='Grupo %s' % row['group'], when=when, location=row['stadium'])
else:
home = row['home']
away = row['away']
match, _ = tournament.match_set.get_or_create(
home_placeholder=home, away_placeholder=away, knockout=True,
description=row['round'], when=when, location=row['stadium'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment