Skip to content

Instantly share code, notes, and snippets.

@pshriwise
Created June 20, 2017 21:24
Show Gist options
  • Save pshriwise/03859f97f69ad8459cf5025fa8a924b1 to your computer and use it in GitHub Desktop.
Save pshriwise/03859f97f69ad8459cf5025fa8a924b1 to your computer and use it in GitHub Desktop.
Script for calculating hockey assists by team, game, or for the entire league based on Ulti-Analytics stats
import argparse
from datetime import datetime
import urllib2, json
from pprint import pprint
from tabulate import tabulate
def sum_dicts(a,b):
out_dict = {}
# list of unique key values
all_keys = list(set(a.keys()+b.keys()))
for key in all_keys:
# if in both, add and enter into output dict
if key in a and key in b:
out_dict[key] = a[key]+ b[key]
# if only in a, use a value
elif key in a:
out_dict[key] = a[key]
# if only in b, use b value
else:
out_dict[key] = b[key]
# return new dicitonary
return out_dict
def ha_for_game_w_date(team_id, date, silent = False):
url = "http://www.ultimate-numbers.com/rest/view/team/"+team_id+"/games"
req = urllib2.Request(url)
data = json.loads(urllib2.urlopen(req, timeout = 10).read())
for game in data:
if game['timestamp'].split()[0] == date:
return ha_for_game(team_id,game['gameId'],silent)
def ha_for_game(team_id, game_id, silent = False):
url = "http://www.ultimate-numbers.com/rest/view/team/"+team_id+"/game/"+game_id
req = urllib2.Request(url)
data = json.loads(urllib2.urlopen(req, timeout = 10).read())
opponent = data['opponentName']
date = data['timestamp'].split()[0]
points = json.loads(data['pointsJson'])
ha_dict = {}
for point in points:
events = point['events']
cache = []
last_event = None
while True:
this_event = events.pop(0)
# if this ia a goal and the event one play ago was a catch, tally hockey assist
if last_event is not None and this_event['action'] == "Goal" and last_event['action'] == "Catch":
player = last_event['passer']
if not player in ha_dict:
ha_dict[player] = 1
else:
ha_dict[player] +=1
last_event = this_event
if len(events) == 0:
break
ha_dict.pop('Anonymous', None)
if not silent:
print "HOCKEY ASSISTS vs. " + opponent + " on " + date
print tabulate(sorted(ha_dict.items(), key=lambda x:x[1], reverse = True))
return ha_dict
def ha_for_all_games(team_id, silent = False):
url = "http://www.ultimate-numbers.com/rest/view/team/"+team_id
req = urllib2.Request(url)
data = json.loads(urllib2.urlopen(req, timeout = 10).read())
team_name = data['name']
url = "http://www.ultimate-numbers.com/rest/view/team/"+team_id+"/games"
req = urllib2.Request(url)
data = json.loads(urllib2.urlopen(req, timeout = 10).read())
game_ids = []
for game in data:
game_ids.append(game['gameId'])
all_dicts = []
for game_id in game_ids:
all_dicts.append(ha_for_game(team_id,game_id, True))
ha_totals = all_dicts.pop(0)
for d in all_dicts:
ha_totals = sum_dicts(ha_totals,d)
if not silent:
print "HOCKEY ASSIST SEASON TOTALS for "+ team_name
print tabulate(sorted(ha_totals.items(), key=lambda x:x[1], reverse = True))
return team_name, ha_totals
team_ids = [5762298832486400,
5199348879065088,
5104011879383040,
5688687119564800,
5634601401712640,
5173627930542080,
5646521009700864,
5672878150254592,
5672188833169408,
5719707252424704,
5701394610782208,
5658753101725696,
5736577883963392,
5767202099691520,
5700228501995520,
5666961832804352,
5721191432060928,
5740216795004928,
5712818124881920,
5654595548217344,
5655358173347840,
5753798823772160,
5707300165648384,
5711261467672576]
def league_totals(team_ids, silent = False):
team_ha_dicts = []
for team_id in team_ids:
team_name, team_ha_dict = ha_for_all_games(str(team_id), True)
new_dict = {}
for key in team_ha_dict:
new_dict[(team_name,key)] = team_ha_dict[key]
team_ha_dicts.append(new_dict)
league_ha_totals = team_ha_dicts.pop(0)
for d in team_ha_dicts:
league_ha_totals = sum_dicts(league_ha_totals,d)
if not silent:
print "AUDL 2017 HOCKEY ASSIST TOTALS"
print tabulate((sorted(league_ha_totals.items(), key=lambda x:x[1], reverse = True)))
if __name__ == "__main__":
ap = argparse.ArgumentParser("Script for returning information about AUDL league or team hockey assist stats. If no arguments are provided, the hockey assists for the entire 2
AUDL league will be provided by team and player.")
ap.add_argument('--team',dest = 'team',type = str, help = "Ulti-Analytics Team ID for which stats will be provided")
ap.add_argument('--game', dest = 'game', type = str, help = "Ulti-Analytics Game ID or date (YYYY-MM-DD) for which stats will be provided")
args = ap.parse_args()
if args.team is not None and args.game is not None:
try:
ha_for_game(args.team, args.game, False)
except:
ha_for_game_w_date(args.team, args.game, False)
elif args.team is not None:
ha_for_all_games(args.team, False)
else:
league_totals(team_ids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment