-
Notifications
You must be signed in to change notification settings - Fork 38
/
playersPickedInLeague.py
161 lines (136 loc) · 5.44 KB
/
playersPickedInLeague.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import requests
import json
import unicodecsv as csv
import argparse
from tqdm import tqdm
import logging, sys
FPL_URL = "https://fantasy.premierleague.com/api/"
LOGIN_URL = "https://users.premierleague.com/accounts/login/"
USER_SUMMARY_SUBURL = "element-summary/"
LEAGUE_CLASSIC_STANDING_SUBURL = "leagues-classic/"
LEAGUE_H2H_STANDING_SUBURL = "leagues-h2h/"
TEAM_ENTRY_SUBURL = "entry/"
PLAYERS_INFO_SUBURL = "bootstrap-static/"
PLAYERS_INFO_FILENAME = "output/allPlayersInfo.json"
USERNAME = '[email protected]'
PASSWORD = 'FPLshow#123'
USER_SUMMARY_URL = FPL_URL + USER_SUMMARY_SUBURL
PLAYERS_INFO_URL = FPL_URL + PLAYERS_INFO_SUBURL
START_PAGE = 1
payload = {
'login':USERNAME,
'password':PASSWORD,
'redirect_uri': 'https://fantasy.premierleague.com/',
'app':'plfpl-web'
}
s = requests.session()
s.post(LOGIN_URL, data=payload)
# Download all player data: https://fantasy.premierleague.com/api/bootstrap-static
def getPlayersInfo():
r = s.get(PLAYERS_INFO_URL)
jsonResponse = r.json()
with open(PLAYERS_INFO_FILENAME, 'w') as outfile:
json.dump(jsonResponse, outfile)
# Get users in league:
# https://fantasy.premierleague.com/api/leagues-classic/43919/standings/?page_new_entries=1&page_standings=2&phase=1
def getUserEntryIds(league_id, ls_page, league_Standing_Url):
entries = []
while(True):
league_url = league_Standing_Url + str(league_id) + "/standings/" + "?page_new_entries=1&page_standings=" + str(ls_page) + "&phase=1"
r = s.get(league_url)
jsonResponse = r.json()
managers = jsonResponse["standings"]["results"]
if not managers:
print "Total managers :",len(entries)
break
for player in managers:
entries.append(player["entry"])
ls_page+=1
return entries
# team picked by user. example: https://fantasy.premierleague.com/api/entry/2677936/event/1/picks with 2677936 being entry_id of the player
def getplayersPickedForEntryId(entry_id, GWNumber):
eventSubUrl = "event/" + str(GWNumber) + "/picks/"
playerTeamUrlForSpecificGW = FPL_URL + TEAM_ENTRY_SUBURL + str(entry_id) + "/" + eventSubUrl
r = s.get(playerTeamUrlForSpecificGW)
jsonResponse = r.json()
try:
picks = jsonResponse["picks"]
except:
if jsonResponse["detail"]:
print "entry_id "+str(entry_id)+" doesn't have info for this gameweek"
return None, None
elements = []
captainId = 1
for pick in picks:
elements.append(pick["element"])
if pick["is_captain"]:
captainId = pick["element"]
return elements, captainId
# read player info from the json file that we downlaoded
def getAllPlayersDetailedJson():
with open(PLAYERS_INFO_FILENAME) as json_data:
d = json.load(json_data)
return d
# writes the results to csv file
def writeToFile(countOfplayersPicked, fileName):
with open(fileName, 'w') as out:
csv_out = csv.writer(out)
csv_out.writerow(['name', 'num'])
for row in countOfplayersPicked:
csv_out.writerow(row)
# Main Script
parser = argparse.ArgumentParser(description='Get players picked in your league in a certain GameWeek')
parser.add_argument('-l','--league', help='league entry id', required=True)
parser.add_argument('-g','--gameweek', help='gameweek number', required=True)
parser.add_argument('-t', '--type', help='league type')
parser.add_argument('-d', '--debug', help='deubg mode on')
args = vars(parser.parse_args())
if args['debug']:
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
getPlayersInfo()
playerElementIdToNameMap = {}
allPlayers = getAllPlayersDetailedJson()
for element in allPlayers["elements"]:
playerElementIdToNameMap[element["id"]] = element["web_name"]#.encode('ascii', 'ignore')
countOfplayersPicked = {}
countOfCaptainsPicked = {}
totalNumberOfPlayersCount = 0
pageCount = START_PAGE
GWNumber = args['gameweek']
leagueIdSelected = args['league']
if args['type'] == "h2h":
leagueStandingUrl = FPL_URL + LEAGUE_H2H_STANDING_SUBURL
print("h2h league mode")
else:
leagueStandingUrl = FPL_URL + LEAGUE_CLASSIC_STANDING_SUBURL
print("classic league mode")
try:
entries = getUserEntryIds(leagueIdSelected, pageCount, leagueStandingUrl)
except Exception, err:
print "Error occured in getting entries/managers in the league."
print err
raise
for entry in tqdm(entries):
try:
elements, captainId = getplayersPickedForEntryId(entry, GWNumber)
except Exception, err:
print "Error occured in getting palyers and captain of the entry/manager"
print err
raise
if not elements:
continue
for element in elements:
name = playerElementIdToNameMap[element]
if name in countOfplayersPicked:
countOfplayersPicked[name] += 1
else:
countOfplayersPicked[name] = 1
captainName = playerElementIdToNameMap[captainId]
if captainName in countOfCaptainsPicked:
countOfCaptainsPicked[captainName] += 1
else:
countOfCaptainsPicked[captainName] = 1
listOfcountOfplayersPicked = sorted(countOfplayersPicked.items(), key=lambda x: x[1], reverse=True)
writeToFile(listOfcountOfplayersPicked, "output/GW"+str(GWNumber)+" Players "+str(leagueIdSelected)+".csv")
listOfCountOfCaptainsPicked = sorted(countOfCaptainsPicked.items(), key=lambda x: x[1], reverse=True)
writeToFile(listOfCountOfCaptainsPicked, "output/GW"+str(GWNumber)+" Captains "+str(leagueIdSelected)+".csv")