cm12321

Download FPL data multi ID

Jun 20th, 2021 (edited)
756
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2. import json
  3. import os
  4. from pathlib import Path
  5.  
  6. BASE = "https://fantasy.premierleague.com/api/"
  7. season = "2020-21"
  8. API = f"data/{season}/"
  9. TEAMS = [352, 41282]  # REPLACE YOUR TEAM IDS HERE
  10.  
  11.  
  12. def cache_page_as(page, address, TEAM=None):
  13.     URL = BASE + page
  14.     if TEAM:
  15.         TARGET = f"{API}/user_data/{TEAM}/{address}"
  16.     else:
  17.         TARGET = f"{API}{address}"
  18.  
  19.     if os.path.exists(TARGET):
  20.         print("Existing target... skipping")
  21.         return
  22.  
  23.     print(f"Requesting {URL} -> {TARGET}")
  24.     r = requests.get(URL)
  25.     if r.status_code != 200:
  26.         print(r.status_code)
  27.         print(f"Error in request {page}")
  28.         return
  29.     os.makedirs(os.path.dirname(TARGET), exist_ok=True)
  30.     with open(TARGET, 'w') as f:
  31.         json.dump(r.json(), f, indent=2)
  32.  
  33.  
  34. cache_page_as("bootstrap-static/", "main.json")
  35. cache_page_as("fixtures/", "fixtures/all.json")
  36. for gw in range(1, 39):
  37.     cache_page_as(f"fixtures/?event={gw}", f"fixtures/{gw}.json")
  38.     cache_page_as(f"event/{gw}/live", f"live/{gw}.json")
  39.  
  40. for TEAM in TEAMS:
  41.     Path(f"{API}/user_data/{TEAM}").mkdir(parents=True, exist_ok=True)
  42.  
  43.     cache_page_as(f"entry/{TEAM}/", "team_main.json", TEAM)
  44.     cache_page_as(f"entry/{TEAM}/history/", "team_history.json", TEAM)
  45.     cache_page_as(f"entry/{TEAM}/transfers/", "team_transfers.json", TEAM)
  46.     for gw in range(1, 39):
  47.         cache_page_as(f"entry/{TEAM}/event/{gw}/picks/", f"picks/{gw}.json", TEAM)
  48.  
RAW Paste Data