-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_finder.py
More file actions
51 lines (43 loc) · 1.82 KB
/
result_finder.py
File metadata and controls
51 lines (43 loc) · 1.82 KB
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
from serpapi import GoogleSearch
import os
API_KEY = os.environ["SERP_API_KEY"]
def get_recent_result(team: str, national_team_soccer: bool):
"""Gets recent match result."""
params = {
"q": team,
"api_key": API_KEY
}
if national_team_soccer:
params["q"] += " national soccer"
try:
search = GoogleSearch(params)
results = search.get_dict()
sports_results = results["sports_results"]
result = f"The {team} "
if "game_spotlight" in sports_results:
spotlight = sports_results["game_spotlight"]
date = spotlight["date"]
enemy = spotlight["teams"][0]['name']
flipped = False
if enemy == sports_results["title"]:
enemy = spotlight["teams"][-1]['name']
flipped = True
if ',' in date:
result += f"are playing {enemy} on {date}."
else:
score = [ spotlight["teams"][-1]['score']['total'], spotlight["teams"][0]['score']['total']]
if flipped:
score = score[::-1]
outcome = "winning" if score[0] > score[-1] else "losing"
if spotlight['league'] == date:
result += f"are current playing {enemy}, {outcome} {score[0]} to {score[-1]}."
else:
result += f"played against {enemy} on {date}, {outcome} {score[0]} to {score[-1]}."
else:
result += "didn't play any games recently and aren't playing very soon."
return result
# result = "The {team} played against {enemy}, winning/losing score to score"
except KeyError:
return "Couldn't find a recent score."
except Exception:
return "Unknown error occurred."