Facebook
From BigWaltie, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 121
  1. #Import all the packages we need to run the program
  2. import requests
  3. import pandas as pd
  4. import numpy as np
  5. from requests.adapters import HTTPAdapter
  6. from requests.packages.urllib3.util.retry import Retry
  7. import time
  8. #import asyncioa
  9. #import aiohttp
  10. from datetime import date
  11. today = date.today()
  12.  
  13. #This is a function to create the .csv file we will be chucking all the entries into.
  14. def exportfile():
  15.         #You will need to change the address shown below!
  16.         export_csv =  r'c:\Users\Walter\Documents\FPLcode\overall_league_standings_full'+str(today)+'.csv'
  17.         return export_csv
  18.  
  19. #This function actually does the work. It does two things: first requests the data and then writes it to the file
  20. def requestwrite(pagenumber=1,chunk_size=10):
  21.         pagenumber = 1
  22.         lastpage = 0
  23.         #We don't want to run our program when we've run out of pages of data.
  24.         while lastpage == 0:
  25.                 #This is the url of the API that we are using. 314 is the unique league code for the overall standings
  26.                 league_url = 'https://fantasy.premierleague.com/api/leagues-classic/314/standings/'
  27.                 #So now we "request" this information, and add on one to the pagenumber
  28.                 league_json = requests.get(league_url+'?&page_standings='+str(pagenumber)).json()
  29.                 pagenumber += 1
  30.                 #Using the pandas package, we make a dataframe and append the relevant info
  31.                 overall_league_df = pd.DataFrame(league_json['standings']['results'])
  32.                 #We now export this dataframe to the address given by the export_csv function, and we append it in chunks of 10 for stability and speed.
  33.                 overall_league_df.to_csv(export_csv,index=False,header=False,mode='a',chunksize=chunk_size)
  34.                 #If there are no pages left, don't run the loop again.
  35.                 if league_json['standings']['has_next'] == False:
  36.                         lastpage == 1
  37.         return
  38.  
  39. #Here we simply call our two functions, and the csv file will fill up until everybody's score is present in a .csv file called "overall_league_standings2020-XX-XX.csv"
  40. export_csv = exportfile()
  41. requestwrite()
  42.