-
-
Save ramiro/cafc0400d158a0aa57a69e09a9b98654 to your computer and use it in GitHub Desktop.
Import Russia 2018 matches into fenics
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
# 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 | |
import django | |
from django.utils.timezone import get_default_timezone, make_aware | |
django.setup() | |
from ega.models import Team, Tournament | |
tournament, _ = Tournament.objects.get_or_create(slug='rusia-2018', defaults={'name': '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