| #! /usr/bin/python | |
| import argparse | |
| import json | |
| import urllib2 | |
| """ | |
| A script to determine the 2016 FRC events with highest capture/breach rates | |
| Takes further http://www.chiefdelphi.com/forums/showpost.php?p=1566654&postcount=41 | |
| """ | |
| BASE_URL = 'https://www.thebluealliance.com/api/v2/{}' | |
| APP_HEADER = 'X-TBA-App-Id' | |
| APP_ID = 'plnyyanks:topevents:v0.1' | |
| # See https://github.com/the-blue-alliance/the-blue-alliance/blob/master/consts/event_type.py | |
| VALID_EVENT_TYPES = [0, 1, 2, 3] | |
| def fetch_endpoint(endpoint): | |
| full_url = BASE_URL.format(endpoint) | |
| url = urllib2.Request(full_url, headers={APP_HEADER: APP_ID, 'User-agent': 'Mozilla/5.0'}) | |
| response = urllib2.urlopen(url) | |
| return json.loads(response.read()) | |
| def fetch_event_keys_in_year(year): | |
| api_events = fetch_endpoint("events/{}".format(year)) | |
| return [event["key"] for event in api_events if event["event_type"] in VALID_EVENT_TYPES] | |
| def fetch_event_stats(event_key): | |
| print "Fetching stats for {}".format(event_key) | |
| return fetch_endpoint("event/{}/stats".format(event_key)) | |
| def get_rates(stats_dict): | |
| try: | |
| return (float(stats_dict["year_specific"]["qual"]["breaches"][2]), float(stats_dict["year_specific"]["qual"]["captures"][2])) | |
| except KeyError, e: | |
| return (0, 0) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--year", help="competition season to test", type=int, default=2016) | |
| parser.add_argument("--top", help="number of events to print", type=int, default=5) | |
| args = parser.parse_args() | |
| # Build list of all relevent event keys | |
| print "Fetching events in {}".format(args.year) | |
| event_keys = fetch_event_keys_in_year(args.year) | |
| print "Found {} events".format(len(event_keys)) | |
| # Get list of breach and capture rates | |
| breaches = [] | |
| captures = [] | |
| for event_key in event_keys: | |
| stats = fetch_event_stats(event_key) | |
| rates = get_rates(stats) | |
| breaches.append((event_key, rates[0])) | |
| captures.append((event_key, rates[1])) | |
| breaches = sorted(breaches, key=lambda x: x[1], reverse=True) | |
| captures = sorted(captures, key=lambda x: x[1], reverse=True) | |
| print "Top {} events in qual breach rate".format(args.top) | |
| for i in range(0, args.top): | |
| print "{} - {}".format(breaches[i][0], breaches[i][1]) | |
| print "Top {} events in qual capture rate".format(args.top) | |
| for i in range(0, args.top): | |
| print "{} - {}".format(captures[i][0], captures[i][1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment