Skip to content

Instantly share code, notes, and snippets.

@seangrogan
Last active August 16, 2019 06:53
Show Gist options
  • Save seangrogan/4c547d3c31f4499242cb to your computer and use it in GitHub Desktop.
Save seangrogan/4c547d3c31f4499242cb to your computer and use it in GitHub Desktop.
""" A simple script for reading 'http://worldcup.sfg.io/matches' and outputting it in the
command prompt. It will display the scores of all the matches as well as a table of the
leading scorer(s) and the next 10 best scorers. If you are interested in getting in touch
I can be reached at grogie05@outlook.com
"""
import urllib.request
import json
resp = urllib.request.urlopen('http://worldcup.sfg.io/matches').read()
m = 0
tg = 0
stats = dict()
# Reading the JSON from http://worldcup.sfg.io/matches -----------------------------------
for jogo in json.loads(resp.decode('utf-8')):
if jogo['status'] == 'completed':
m+=1
# Computing home goals -------------------------------------------------------------------
events = jogo['home_team_events']
for i in events:
if i['type_of_event'] == 'goal' or i['type_of_event'] == 'goal-penalty':
if i['player'] + ' ' + str(jogo['home_team']['code']) not in stats:
stats[i['player'] + ' ' + str(jogo['home_team']['code'])] = 1
else:
g = stats[i['player']+ ' ' + str(jogo['home_team']['code'])]
g += 1
stats[i['player']+ ' ' + str(jogo['home_team']['code'])] = g
# Computing away goals -------------------------------------------------------------------
events = jogo['away_team_events']
for i in events:
if i['type_of_event'] == 'goal' or i['type_of_event'] == 'goal-penalty':
if i['player']+ ' ' + str(jogo['away_team']['code']) not in stats:
stats[i['player'] + ' ' + str(jogo['away_team']['code'])] = 1
else:
g = stats[i['player']+ ' ' + str(jogo['away_team']['code'])]
g += 1
stats[i['player']+ ' ' + str(jogo['away_team']['code'])] = g
# Printing match result ------------------------------------------------------------------
home = jogo['home_team']['country'] + ' ' + str(jogo['home_team']['goals'])
away = jogo['away_team']['country'] + ' ' + str(jogo['away_team']['goals'])
if len(home) < 11:
print (' ', jogo['match_number'], '\t: ',home ,'\t\t', away)
else:
print (' ', jogo['match_number'], '\t: ',home ,'\t', away)
tg += jogo['home_team']['goals'] + jogo['away_team']['goals']
# Finished reading the JSON --------------------------------------------------------------
# Printing out the Goals Per Game --------------------------------------------------------
print('')
print('Total Goals\t: ', tg)
print('GPG \t: ', int((tg/m)*100)/100)
print('')
# Showing the top goal scorer and... -----------------------------------------------------
print('Goal Scoring Leader: ')
gsl = max(stats, key=stats.get)
great = stats[gsl]
while stats[gsl] == great:
num = stats[gsl]
stats.pop(gsl)
print(' ', num,' :\t ', gsl)
gsl = max(stats, key=stats.get)
# ...the 10 next best scorers ------------------------------------------------------------
print('Best of the rest')
for i in range (10):
num = stats[gsl]
stats.pop(gsl)
print(' ', num,' :\t ', gsl)
gsl = max(stats, key=stats.get)
print('')
x = input('Press enter to continue...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment