swid = ### espn_s2 = ### league_id = ### allTimeResults = { ###espn id : {'name' : ###name, 'misstarts' : 0, 'lost points' : 0, 'lost wins' : 0, 'lucky wins' : 0, 'games played' : 0, } } import requests from itertools import combinations slotcodes = { 0 : 'QB', 2 : 'RB', 4 : 'WR', 6 : 'TE', 16: 'Def', 17: 'K', 20: 'Bench', 21: 'IR', 23: 'Flex' } def without(a,b): # b is a list/tuple of lists/tuples (can be mixed) if len(b) == 1: return [x for x in a if x not in b[0]] else: return without(without(a,[b[0]]),b[1:]) def adj(tm): eligible = {'WR' : [], 'RB' : [], 'TE' : [], 'Flex' : [], 'QB' : [], 'Def' : [], 'K' : []} DSTpoints = 0 Indivpoints = 0 Indivstarters = [] roster = tm['roster']['entries'] for i in range(len(roster)): p = roster[i] e = p['playerPoolEntry']['player']['eligibleSlots'] if 4 in e: eligible[slotcodes[4]].append(i) if 2 in e: eligible[slotcodes[2]].append(i) if 6 in e: eligible[slotcodes[6]].append(i) if 23 in e: eligible[slotcodes[23]].append(i) if 0 in e: eligible[slotcodes[0]].append(i) if 16 in e: eligible[slotcodes[16]].append(i) if 17 in e: eligible[slotcodes[17]].append(i) if p['lineupSlotId'] == 16: DSTpoints = p['playerPoolEntry']['appliedStatTotal'] if p['lineupSlotId'] in [0,2,4,6,17,23]: Indivpoints += p['playerPoolEntry']['appliedStatTotal'] Indivstarters.append(i) misstarts = 0 lostPoints = 0 foundBetterDST = False DSTdiff = 0 for d in eligible['Def']: if roster[d]['playerPoolEntry']['appliedStatTotal'] > DSTpoints: if not foundBetterDST: misstarts = 1 foundBetterDST = True DSTdiff = roster[d]['playerPoolEntry']['appliedStatTotal'] - DSTpoints else: if DSTdiff < roster[d]['playerPoolEntry']['appliedStatTotal'] - DSTpoints: DSTdiff = roster[d]['playerPoolEntry']['appliedStatTotal'] - DSTpoints lostPoints += DSTdiff bests = [] maxIndivPoints = Indivpoints for r in list(combinations(eligible['RB'], 2)): for w in list(combinations(without(eligible['WR'],[r]), 2)): for t in without(eligible['TE'],[r,w]): for f in without(eligible['Flex'],[r,w,[t]]): for q in without(eligible['QB'],[r,w,[t],[f]]): for k in without(eligible['K'],[r,w,[t],[f],[q]]): P = list(r) + list(w) + [t,f,q,k] S = sum([roster[p]['playerPoolEntry']['appliedStatTotal'] for p in P]) if S == maxIndivPoints: bests.append(P) elif S > maxIndivPoints: bests = [P] maxIndivPoints = S m = 10 for P in bests: M = without(P,[Indivstarters]) if len(M) < m: m = len(M) if m == 10: m = 0 misstarts += m lostPoints += (maxIndivPoints - Indivpoints) return (misstarts, lostPoints) start = 2018 end = 2019 for season in range(start,end+1): print season, "season" url = 'https://fantasy.espn.com/apis/v3/games/ffl/seasons/' + str(season) + '/segments/0/leagues/' + str(league_id) + '?view=mTeam' req = requests.get(url,cookies={"SWID": swid, "espn_s2": espn_s2}) d = req.json() N = len(d['teams']) MS = [0,]*N LP = [0,]*N lostWins = [0,]*N luckyWins = [0,]*N cgp = [0,]*N # competitive games played idToOwner = {} for i in range(N): idToOwner[i] = d['teams'][i]['owners'][0] url = 'https://fantasy.espn.com/apis/v3/games/ffl/seasons/' + str(season) + '/segments/0/leagues/' + str(league_id) +'?view=mMatchup&view=mMatchupScore' W = 16 for week in range(1,W+1): print "Week", week req = requests.get(url, params={'scoringPeriodId': week}, cookies={"SWID": swid, "espn_s2": espn_s2}) d = req.json() for game in range(5*(week-1),5*week): gm = d['schedule'][game] if 'away' in gm.keys(): a = gm['away']['teamId'] A = gm['away']['totalPoints'] (msa,lpa) = adj(d['teams'][a-1]) Aopt = A + lpa MS[a-1] += msa LP[a-1] += lpa cgp[a-1] += 1 h = gm['home']['teamId'] H = gm['home']['totalPoints'] (msh,lph) = adj(d['teams'][h-1]) Hopt = H + lph MS[h-1] += msh LP[h-1] += lph cgp[h-1] += 1 if A < H and Aopt >= H: if Aopt == H: lostWins[a-1] += 0.5 luckyWins[h-1] += 0.5 else: lostWins[a-1] += 1 luckyWins[h-1] += 1 if H < A and Hopt >= A: if Hopt == A: lostWins[h-1] += 0.5 luckyWins[a-1] += 0.5 else: lostWins[h-1] += 1 luckyWins[a-1] += 1 else: h = gm['home']['teamId'] H = gm['home']['totalPoints'] (msh,lph) = adj(d['teams'][h-1]) MS[h-1] += msh LP[h-1] += lph for i in range(N): allTimeResults[idToOwner[i]]['misstarts'] += MS[i] allTimeResults[idToOwner[i]]['lost points'] += LP[i] allTimeResults[idToOwner[i]]['lost wins'] += lostWins[i] allTimeResults[idToOwner[i]]['lucky wins'] += luckyWins[i] allTimeResults[idToOwner[i]]['games played'] += cgp[i] print 'name MS LP lostW luckW' for id in allTimeResults.keys(): print allTimeResults[id]['name'], for cat in ['misstarts','lost points','lost wins','lucky wins']: s = str(float(allTimeResults[id][cat])/allTimeResults[id]['games played'])[:5] print s, print ' '*(5-len(s)), print '\n',