-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
127 lines (102 loc) · 4.22 KB
/
api.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from datetime import datetime, timedelta
from perms import API_TOKEN
import requests
import perms
def get_matches(x_days):
today_date = datetime.now().strftime("%Y-%m-%d")
new_date = datetime.now() + timedelta(days=x_days)
formatted_new_date = new_date.strftime("%Y-%m-%d")
#Hver gang get_matches() kjører henter vi inn kamper som er fra denne dagen, og 7 dager frem i tid
api_url = "https://v3.football.api-sports.io/fixtures"
headers = {
"x-rapidapi-host": "v3.football.api-sports.io",
"x-rapidapi-key": API_TOKEN # Be cautious with your API key
}
query_fixtures = {
"league": "103",
"season": "2024",
"timezone": "Europe/Oslo",
"from": today_date,
"to": formatted_new_date
}
response = requests.get(api_url, headers=headers, params=query_fixtures)
if response.status_code == 200:
data = response.json()
match_details = []
for fixture in data['response']:
match_info = {
'date': fixture['fixture']['date'],
'home_team': fixture['teams']['home']['name'],
'away_team': fixture['teams']['away']['name'],
'round': fixture['league']['round'],
'match_id': fixture['fixture']['id'],
'status': fixture['fixture']['status']['short']
}
#if current_round == match_info['round']:
match_details.append(match_info)
for match in match_details:
print(f"Date: {match['date']}, Home Team: {match['home_team']}, Away Team: {match['away_team']}, "
f"Round: {match['round']}, Match ID: {match['match_id']}, Status: {match['status']}")
return match_details
async def get_match_results(match_id):
api_url = "https://v3.football.api-sports.io/fixtures"
headers = {
"x-rapidapi-host": "v3.football.api-sports.io",
"x-rapidapi-key": API_TOKEN # Be cautious with your API key
}
params = {
'id': match_id,
}
response = requests.get(api_url, headers=headers, params=params)
data = response.json()
for fixture in data['response']:
print(fixture['fixture']['status']['short'])
if fixture['fixture']['status']['short'] in ['FT', 'PEN', 'AET', 'AWD', 'WO']:
home_team = fixture['teams']['home']['name']
away_team = fixture['teams']['away']['name']
home_win = fixture['teams']['home']['winner']
if home_win is True:
result = True # Home win
elif home_win is False:
result = False # Away win
else:
result = None # Draw
elif fixture['fixture']['status']['short'] in ['CANC', 'ABD', 'PST', 'TBD']:
return "Game never started"
else:
return "No result"
print(f"Home team: {home_team}\n")
print(f"Away team: {away_team}\n")
print(f"Result: {result}\n")
return result, home_team, away_team
def get_match_results_non_async(match_id):
api_url = "https://v3.football.api-sports.io/fixtures"
headers = {
"x-rapidapi-host": "v3.football.api-sports.io",
"x-rapidapi-key": API_TOKEN # Be cautious with your API key
}
params = {
'id': match_id,
}
response = requests.get(api_url, headers=headers, params=params)
data = response.json()
for fixture in data['response']:
print(fixture['fixture']['status']['short'])
if fixture['fixture']['status']['short'] in ['FT', 'PEN', 'AET', 'AWD', 'WO']:
home_team = fixture['teams']['home']['name']
away_team = fixture['teams']['away']['name']
home_win = fixture['teams']['home']['winner']
if home_win is True:
result = True # Home win
elif home_win is False:
result = False # Away win
else:
result = None # Draw
elif fixture['fixture']['status']['short'] in ['CANC', 'ABD', 'PST', 'TBD']:
return "Game never started"
else:
return "No result"
print(f"Result: {result}\n")
print("Home team: " + home_team + "\n")
print("Away team: " + away_team + "\n")
return result, home_team, away_team