-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
193 lines (153 loc) · 6.95 KB
/
service.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import requests
import pandas as pd
import json
import os.path
import ml
import numpy as np
HOST = "https://api-football-v1.p.rapidapi.com/v2/"
LIGUE1_ID = "2664"
ROUND_LABEL = "Regular_Season_-_"
FOLDER_PATH = "./rounds/"
_homeTeamName = ""
_awayTeamName = ""
def getApiKey():
f = open("vars", "r")
return f.readline()
HEADERS = {"X-RapidAPI-Key": getApiKey()}
def createJsonFile(championshipRoundJson, championshipRound):
with open(FOLDER_PATH + str(championshipRound) + ".json", "w") as outfile:
json.dump(championshipRoundJson, outfile)
def getWinner(homeTeamScore, awayTeamScore):
if homeTeamScore > awayTeamScore:
return 1
elif homeTeamScore < awayTeamScore:
return 2
else:
return 0
def readJSONFile(pathToFile):
with open(pathToFile, "r") as JSONFile:
readJSON = json.load(JSONFile)
return readJSON
def generateJsonByChampionshipRound(championshipRound):
url = HOST + "fixtures/league/" + LIGUE1_ID + "/" + ROUND_LABEL + str(championshipRound - 1)
req = requests.get(url, headers = HEADERS)
reqJson = json.loads(req.text)
fixtures = reqJson["api"]["fixtures"]
championshipRoundJson = {}
championshipRoundJson["matches"] = []
championshipRoundJson["teams"] = []
for fixture in fixtures:
if fixture["statusShort"] != "PST":
print(fixture["status"])
if fixture["goalsHomeTeam"] != None:
fixtureWinner = getWinner(fixture["goalsHomeTeam"], fixture["goalsAwayTeam"])
championshipRoundJson["teams"].append(fixture["homeTeam"]["team_name"])
championshipRoundJson["teams"].append(fixture["awayTeam"]["team_name"])
homeTeamStats, awayTeamStats = getPredictionsForfixture(fixture["fixture_id"], fixtureWinner)
championshipRoundJson["matches"].append({
"winner": fixtureWinner,
"home": homeTeamStats,
"away": awayTeamStats,
})
createJsonFile(championshipRoundJson, championshipRound-1)
print("Generated JSON File for championship round " + str(championshipRound))
def getTeamFixtureWithRoundAndTeamName(championshipRound, teamName):
global _homeTeamName
global _awayTeamName
url = HOST + "fixtures/league/" + LIGUE1_ID + "/" + ROUND_LABEL + str(championshipRound)
req = requests.get(url, headers = HEADERS)
reqJson = json.loads(req.text)
fixtures = reqJson["api"]["fixtures"]
for fixture in fixtures:
homeTeamName = fixture["homeTeam"]["team_name"]
awayTeamName = fixture["awayTeam"]["team_name"]
if homeTeamName == teamName or awayTeamName == teamName:
print(homeTeamName + " vs " + awayTeamName)
_homeTeamName = homeTeamName
_awayTeamName = awayTeamName
fixtureId = fixture["fixture_id"]
fixtureStats = getFixtureStatsToPredict(fixtureId)
break
return fixtureStats
def getFixtureStatsToPredict(fixtureId):
url = HOST + "predictions/" + str(fixtureId)
req = requests.get(url, headers = HEADERS)
reqJson = json.loads(req.text)
comparison = reqJson["api"]["predictions"][0]["comparison"]
homeTeamStats = {
"goals_avg": reqJson["api"]["predictions"][0]["teams"]["home"]["last_5_matches"]["goals_avg"],
"goals_against_avg": reqJson["api"]["predictions"][0]["teams"]["home"]["last_5_matches"]["goals_against_avg"],
"forme": comparison["forme"]["home"],
"att": comparison["att"]["home"],
"def": comparison["def"]["home"],
"h2h": comparison["h2h"]["home"],
}
awayTeamStats = {
"goals_avg": reqJson["api"]["predictions"][0]["teams"]["away"]["last_5_matches"]["goals_avg"],
"goals_against_avg": reqJson["api"]["predictions"][0]["teams"]["away"]["last_5_matches"]["goals_against_avg"],
"forme": comparison["forme"]["away"],
"att": comparison["att"]["away"],
"def": comparison["def"]["away"],
"h2h": comparison["h2h"]["away"],
}
fixtureStats = {
"home": homeTeamStats,
"away": awayTeamStats,
}
return fixtureStats
def getPredictionsForfixture(fixtureId, fixtureWinner):
url = HOST + "predictions/" + str(fixtureId)
req = requests.get(url, headers = HEADERS)
reqJson = json.loads(req.text)
comparison = reqJson["api"]["predictions"][0]["comparison"]
homeTeamStats = {
"goals_avg": reqJson["api"]["predictions"][0]["teams"]["home"]["last_5_matches"]["goals_avg"],
"goals_against_avg": reqJson["api"]["predictions"][0]["teams"]["home"]["last_5_matches"]["goals_against_avg"],
"forme": comparison["forme"]["home"],
"att": comparison["att"]["home"],
"def": comparison["def"]["home"],
"h2h": comparison["h2h"]["home"],
}
awayTeamStats = {
"goals_avg": reqJson["api"]["predictions"][0]["teams"]["away"]["last_5_matches"]["goals_avg"],
"goals_against_avg": reqJson["api"]["predictions"][0]["teams"]["away"]["last_5_matches"]["goals_against_avg"],
"forme": comparison["forme"]["away"],
"att": comparison["att"]["away"],
"def": comparison["def"]["away"],
"h2h": comparison["h2h"]["away"],
}
return homeTeamStats, awayTeamStats
def getTeamsForChampionshipRound(championshipRound):
filePath = FOLDER_PATH + str(championshipRound-1) + ".json"
if os.path.isfile(filePath):
jsonFile = readJSONFile(filePath)
teams = jsonFile["teams"]
teams.sort()
return teams
else:
print("Retrieving the championship round informations... please wait...")
generateJsonByChampionshipRound(championshipRound)
jsonFile = readJSONFile(filePath)
teams = jsonFile["teams"]
return teams
def combinedAllRoundsFound():
inputs, desired = [], []
for f in os.listdir(FOLDER_PATH):
championshipRoundsJSON = readJSONFile(FOLDER_PATH + f)
inputs2 = ml.extractInputsValuesFromMatches(championshipRoundsJSON["matches"])
desired2 = ml.extractDesiredValuesFromMatches(championshipRoundsJSON["matches"])
inputs += inputs2
desired += desired2
return inputs, desired
def displayPrediction(predictionIndex):
if predictionIndex == 1:
print("Our prediction is that " + _homeTeamName + " should win the match !")
elif predictionIndex == 2:
print("Our prediction is that " + _awayTeamName + " should win the match !")
else:
print("Our prediction is that the match between " + _homeTeamName + " and " + _awayTeamName + " will result in a draw")
def displayOdds(oddsProba):
print("The odds for this match are the following :")
print(_homeTeamName + " has a " + str(round(oddsProba[0][1] * 100,2)) + "% chance of winning the match")
print(_awayTeamName + " has a " + str(round(oddsProba[0][2] * 100,2)) + "% chance of winning the match")
print(str(round(oddsProba[0][0] * 100, 2)) + "% chance to result in a draw")