· 4 years ago · Jun 22, 2021, 06:18 PM
1def gather_standings(standings):
2 #Building Endpoint URL
3 standings_endpoint = "/standings"
4 league_url = "league=39"
5 season_url = "season=2020"
6 request_url = str(standings_endpoint + "?" + league_url + "&" + season_url)
7 ### Connection string to connect to API and Retrieve Data:
8 conn.request("GET", request_url, headers=headers)
9 ### Gets Response from Request, stores in imported_json, unloads into var "response_raw"
10 imported_json = conn.getresponse().read()
11 response_raw = json.loads(imported_json)
12 ### Access data to purely provide standings Deta
13 standings_data = response_raw["response"][0]["league"]["standings"][0]
14 #Teams storage dict to store team objects
15 teams_storage_dict = {}
16 #Cycles through data in Standings data to store in Team_storage_dict with key = TeamName, value = TeamClassObject
17 for data in standings_data:
18 teams_storage_dict[data["team"]["name"]] = Team(data)
19 #cycles through Team Storage dict and updates MongoDB with the new values for the standings DB.
20 for key, value in teams_storage_dict.items():
21 collection_standings.update_many({"teamName":key}, {"$set":{
22 "teamName":value.teamName,
23 "teamID":value.teamID,
24 "rank":value.rank,
25 "points":value.points,
26 "goalDiff":value.goalDiff,
27 "form":value.form,
28 "played":value.played,
29 "win":value.win,
30 "draw":value.draw,
31 "lose":value.lose,
32 "goalsFor":value.goalsFor,
33 "goalsAgainst":value.goalsAgainst,
34 "homePlayed":value.homePlayed,
35 "homeWin":value.homeWin,
36 "homeDraw":value.homeDraw,
37 "homeLose":value.homeLose,
38 "homeGoalsFor":value.homeGoalsFor,
39 "homeGoalsAgainst":value.homeGoalsAgainst,
40 "awayPlayed":value.awayPlayed,
41 "awayWin":value.awayWin,
42 "awayDraw":value.awayDraw,
43 "awayLose":value.awayLose,
44 "awayGoalsFor":value.awayGoalsFor,
45 "awayGoalsAgainst":value.awayGoalsAgainst
46 }})
47 print("Standings updated")