Skip to content

Instantly share code, notes, and snippets.

@dan-gamble
Created December 6, 2014 14:20
Show Gist options
  • Save dan-gamble/1ee4a75091d126772027 to your computer and use it in GitHub Desktop.
Save dan-gamble/1ee4a75091d126772027 to your computer and use it in GitHub Desktop.
from django.core.management.base import BaseCommand, CommandError
from django.utils.text import slugify
from apps.clubs.models import Club
from apps.leagues.models import League
from apps.nations.models import Nation
import requests
class Command(BaseCommand):
def handle(self, *args, **options):
print('Trying base URL')
r = requests.get('https://www.easports.com/uk/fifa/ultimate-team/api/fut/item?jsonParamObject=%7B%22page%22:1%7D')
print('Success!')
json = r.json()
total_pages = json['totalPages']
print('There are {} pages to be looped over'.format(total_pages))
base_page = 1
for i in range(base_page, total_pages + 1):
print('Scraping page {}'.format(i))
try:
page = requests.get('https://www.easports.com/uk/fifa/ultimate-team/api/fut/item?jsonParamObject=%7B%22page%22:{}%7D'.format(i)).json()
print('Got page {}'.format(i))
except Exception as error:
print(error)
for i, item in enumerate(page['items']):
club = item['club']
nation = item['nation']
league = item['league']
obj, created = Nation.objects.get_or_create(
asset_id=nation['id'],
name=nation['name'],
name_abbr=nation['abbrName'],
slug=slugify(nation['name'])
)
if created:
print('Created {}'.format(obj))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment