Last active
October 22, 2017 04:17
-
-
Save JoeyAndres/4164b56dc6317abb90f0933061393bae to your computer and use it in GitHub Desktop.
Gets all the F1 race results from 1950 to 2017. The results are flatten so that they are easily utilized in mapreduce.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import urllib2 | |
import time | |
import json | |
def get_results(output_dir): | |
for year in range(1950, 2017 + 1): | |
for rnd in range(1, 50): | |
results_url = "http://ergast.com/api/f1/{0}/{1}/results.json".format(year, rnd) | |
results = urllib2.urlopen(results_url).read() | |
# Flatten results. | |
race_json = json.loads(results) | |
if len(race_json["MRData"]["RaceTable"]["Races"]) is 0: | |
print "Round {0} is Invalid".format(rnd) | |
break; | |
circuit = race_json["MRData"]["RaceTable"]["Races"][0]["Circuit"]["circuitId"] | |
results_json = race_json["MRData"]["RaceTable"]["Races"][0]["Results"] | |
results = "" | |
for result_json in results_json: | |
#print race_json | |
constructor_json = result_json["Constructor"] | |
driver_json = result_json["Driver"] | |
status = result_json["status"] | |
time_milli = "-1" # Invalid | |
if "Time" in result_json: | |
time_milli = result_json["Time"]["millis"] | |
position = result_json["position"] | |
results += "{0},{1},{2},{3},{4},{5},{6}\n".format( | |
year, | |
circuit, | |
constructor_json["constructorId"], | |
driver_json["driverId"], | |
status, | |
time_milli, | |
position); | |
output_path = "{0}/{1}-{2}.txt".format(output_dir, year, circuit) | |
print output_path | |
output_file = open(output_path, "w") | |
output_file.write(results) | |
output_file.close() | |
time.sleep(2) # Avoid DOS signature. | |
if __name__ == "__main__": | |
output_dir = str(sys.argv[1]) | |
get_results(output_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment