-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestapi-intermediate-test-2.py
71 lines (59 loc) · 1.93 KB
/
restapi-intermediate-test-2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/python3
import math
import os
import random
import re
import sys
import requests
#
# Complete the 'getWinnerTotalGoals' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING competition
# 2. INTEGER year
#
def add_goals(data, home):
total = 0
for g in data:
if home:
total += int(g['team1goals'])
else:
total += int(g['team2goals'])
return total
def get_goals(competition, year, winner, home):
total = 0
page = 1
if home:
team_param = '&team1='
else:
team_param = '&team2='
json = requests.get('https://jsonmock.hackerrank.com/api/football_matches?'
+ 'competition=' + competition
+ '&year=' + str(year)
+ team_param + winner
+ '&page=' + str(page)).json()
data = json['data']
while len(data) > 0:
total += add_goals(data, home)
page += 1
data = requests.get('https://jsonmock.hackerrank.com/api/football_matches?'
+ 'competition=' + competition
+ '&year=' + str(year)
+ team_param + winner
+ '&page=' + str(page)).json()['data']
return total
def getWinnerTotalGoals(competition, year):
winner = competition_winner(competition, year)
total = get_goals(competition, year, winner, True)
total += get_goals(competition, year, winner, False)
return total
def competition_winner(competition, year):
json = requests.get('https://jsonmock.hackerrank.com/api/football_competitions?'
+ 'name=' + competition
+ '&year=' + str(year)).json()
return json['data'][0]['winner']
if __name__ == '__main__':
competition = "La Liga"
year = 2012
print(getWinnerTotalGoals(competition, year))