Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- import os
- from pathlib import Path
- BASE = "https://fantasy.premierleague.com/api/"
- season = "2020-21"
- API = f"data/{season}/"
- TEAMS = [352, 41282] # REPLACE YOUR TEAM IDS HERE
- def cache_page_as(page, address, TEAM=None):
- URL = BASE + page
- if TEAM:
- TARGET = f"{API}/user_data/{TEAM}/{address}"
- else:
- TARGET = f"{API}{address}"
- if os.path.exists(TARGET):
- print("Existing target... skipping")
- return
- print(f"Requesting {URL} -> {TARGET}")
- r = requests.get(URL)
- if r.status_code != 200:
- print(r.status_code)
- print(f"Error in request {page}")
- return
- os.makedirs(os.path.dirname(TARGET), exist_ok=True)
- with open(TARGET, 'w') as f:
- json.dump(r.json(), f, indent=2)
- cache_page_as("bootstrap-static/", "main.json")
- cache_page_as("fixtures/", "fixtures/all.json")
- for gw in range(1, 39):
- cache_page_as(f"fixtures/?event={gw}", f"fixtures/{gw}.json")
- cache_page_as(f"event/{gw}/live", f"live/{gw}.json")
- for TEAM in TEAMS:
- Path(f"{API}/user_data/{TEAM}").mkdir(parents=True, exist_ok=True)
- cache_page_as(f"entry/{TEAM}/", "team_main.json", TEAM)
- cache_page_as(f"entry/{TEAM}/history/", "team_history.json", TEAM)
- cache_page_as(f"entry/{TEAM}/transfers/", "team_transfers.json", TEAM)
- for gw in range(1, 39):
- cache_page_as(f"entry/{TEAM}/event/{gw}/picks/", f"picks/{gw}.json", TEAM)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement