-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsofaScore.py
183 lines (151 loc) · 8.05 KB
/
sofaScore.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
# -*- coding: utf-8 -*-
import time
from random import seed
from random import randint
import requests
import json
from utils import *
from models import db, objects
BASE_URL = "https://api.sofascore.com/api/v1/"
HEADERS = {
"cache-control": "max-age=0",
"sec-ch-ua": '"Google Chrome";v="93", " Not;A Brand";v="99", "Chromium";v="93"',
"sec-ch-ua-mobile": "?0",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36",
"sec-ch-ua-platform": "\"Windows\"",
"accept": "*/*",
"origin": "https://www.sofascore.com",
"sec-fetch-site": "same-site",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://www.sofascore.com/",
# "accept-encoding": "gzip, deflate, br",
"accept-language": "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7"
}
def getJSONFromURL(url):
tried = 0
while tried < 3:
try:
content = requests.request("GET", url, data = "", headers = HEADERS).text
tried = 3
except Exception as e:
tried += 1
sleep(randint(3, 5))
if tried == 3:
print("[ERROR] Connection error for the website {}: {}".format(url, e))
return False
#print(content)
try:
jsonContent = json.loads(content)
return jsonContent
except:
print("[ERROR] The player with the URL {} has not been JSON loaded".format(url))
return False
def getPlayers(fromID = 1, toID = None):
seed(1)
currentID = fromID
while currentID <= toID:
print("# Analyzing the team with ID {}...".format(currentID))
url = "{}{}".format(BASE_URL, currentID)
dataJSON = getJSONFromURL(url)
if dataJSON:
print("PLAYER!!!")
if not "error" in dataJSON and "team" in dataJSON and dataJSON['team']['category']['sport']['id'] == 5:
print(dataJSON)
currentID += 1
secondsToWait = randint(10, 15)
time.sleep(secondsToWait)
def checkBreaksUndefinedGamesByPlayer(playerID, lastGames):
breakData = {'definedGames': lastGames['definedGames'], 'games': []}
for indexGame, game in enumerate(lastGames['games']):
if 'opponent' in game and (game['breakDone'] == -1 or game['breakReceived'] == -1):
found = False
definedGame = False
monthDay = game['date'][5:10]
gameYear = int(game['date'][:4])
if monthDay < '01-07':
years = (gameYear, gameYear + 1)
else:
years = (gameYear,)
if not game['error']:
for year in years:
if definedGame:
break
gameDate = '{}-{}'.format(year, monthDay)
url = '{}sport/tennis/scheduled-events/{}'.format(BASE_URL, gameDate)
print(url)
dataJSON = getJSONFromURL(url)
if not isinstance(dataJSON, bool) and 'events' in dataJSON:
for dailyGame in dataJSON['events']:
homePlayer = dailyGame['homeTeam']['id']
awayPlayer = dailyGame['awayTeam']['id']
if homePlayer == playerID and awayPlayer == game['opponent'] or homePlayer == game['opponent'] and awayPlayer == playerID:
found = True
urlGame = '{}event/{}'.format(BASE_URL, dailyGame['id'])
print(urlGame)
gameJSON = getJSONFromURL(urlGame)
if not isinstance(gameJSON, bool) and 'period1' in gameJSON['event']['homeScore']:
wonGamesHome = gameJSON['event']['homeScore']['period1']
wonGamesAway = gameJSON['event']['awayScore']['period1']
if abs(wonGamesHome - wonGamesAway) > 1 and max(wonGamesHome, wonGamesAway) > 5 or max(wonGamesHome, wonGamesAway) == 7:
setFinished = True
else:
setFinished = False
urlStats = '{}event/{}/statistics'.format(BASE_URL, dailyGame['id'])
print(urlStats)
statsJSON = getJSONFromURL(urlStats)
if not isinstance(statsJSON, bool) and 'statistics' in statsJSON:
for phase in statsJSON['statistics']:
if phase['period'] == '1ST':
for group in phase['groups']:
if group['groupName'] == 'Return':
for item in group['statisticsItems']:
if item['name'] == 'Break points converted':
breakItem = {'index': indexGame}
if not setFinished and (int(item['home']) == 0 or int(item['away']) == 0):
# Player retired during the 1st set without break for both players
breakItem['toDelete'] = True
else:
if homePlayer == playerID:
breakItem['breakDone'] = int(item['home']) > 0 and 1 or 0
breakItem['breakReceived'] = int(item['away']) > 0 and 1 or 0
else:
breakItem['breakDone'] = int(item['away']) > 0 and 1 or 0
breakItem['breakReceived'] = int(item['home']) > 0 and 1 or 0
breakData['definedGames'] += 1
definedGame = True
breakData['games'].append(breakItem)
else:
# El matx s'ha d'eliminar
x = 1
break
if not found:
print('Matx no trobat, merci')
print(game)
#return False
return breakData
def getDailyPlayersData(day):
players = []
url = "{}{}{}".format(BASE_URL, "sport/tennis/scheduled-events/", day)
print(url)
dataJSON = getJSONFromURL(url)
for dailyGame in dataJSON['events']:
urlGame = "{}{}{}".format(BASE_URL, "event/", dailyGame['id'])
print(urlGame)
gameJSON = getJSONFromURL(urlGame)
if "Doubles" not in gameJSON['event']['tournament']['name'] and "Mixed" not in gameJSON['event']['tournament']['name']:
if "ranking" in gameJSON['event']['homeTeam']:
player = {
'name': gameJSON['event']['homeTeam']['slug'],
'ranking': gameJSON['event']['homeTeam']['ranking'],
'sofaScoreID': gameJSON['event']['homeTeam']['id']
}
players.append(player)
if "ranking" in gameJSON['event']['awayTeam']:
player = {
'name': gameJSON['event']['awayTeam']['slug'],
'ranking': gameJSON['event']['awayTeam']['ranking'],
'sofaScoreID': gameJSON['event']['awayTeam']['id']
}
players.append(player)
return players