-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrap_ffmadrid_multithreading.py
166 lines (120 loc) · 4.65 KB
/
scrap_ffmadrid_multithreading.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
import time
from bs4 import BeautifulSoup
import requests as r
import json
from multiprocessing import Pool
from tqdm import tqdm
import json
import urllib3
import os
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
BASE_URL = 'https://www.rffm.es/'
API_URL = 'https://www.rffm.es/api/'
DATA_BASE_DIR = 'data/'
def save_json(filename, data, subfolders=[]):
# Ensure trailing slash (/)
folder_path = os.path.join(DATA_BASE_DIR, *subfolders, "")
# Check if the folder exists, if not create it
if not os.path.exists(folder_path):
os.makedirs(folder_path)
with open(folder_path + filename + '.json', 'w') as outfile:
json.dump(data, outfile, indent=4, ensure_ascii=False)
def get_url_data(url):
response = r.get(url, verify=False)
html_doc = response.text
soup = BeautifulSoup(html_doc, 'html.parser')
return json.loads(soup.find(
"script", {"id": "__NEXT_DATA__"}).text)
def get_match_data(codacta, season, competition, group):
url = BASE_URL + \
f'acta-partido/{codacta}?temporada={season}&competicion={competition}&grupo={group}'
return get_url_data(url)['props']['pageProps']['game']
def get_match_data_wrapper(args):
return get_match_data(*args)
def get_competition_data(season, game_type, competition, group):
url = BASE_URL + \
f'competicion/calendario?temporada={season}&tipojuego={game_type}&competicion={competition}&grupo={group}'
matches = get_url_data(url)['props']['pageProps']['calendar']
competition_name = f'{matches["competicion"].capitalize()} {matches["grupo"].capitalize()} Temporada {matches["temporada"]}'
print(f'Extracting data for competition: {competition_name}')
print(url)
args = []
for round in matches['rounds']:
for match in round['equipos']:
args.append((match['codacta'], season, competition, group))
with Pool() as pool:
parsed_matches = list(tqdm(pool.imap_unordered(
get_match_data_wrapper, args), total=len(args)))
save_json(matches["grupo"], parsed_matches, [
matches["temporada"], matches["competicion"]])
def get_seasons_and_game_types():
print('Getting the competition data')
url = BASE_URL + f'competicion/calendario'
seasons = get_url_data(url)['props']['pageProps']['seasons']
game_types = get_url_data(url)['props']['pageProps']['gameTypes']
save_json('seasons', seasons)
save_json('game-types', game_types)
return seasons, game_types
def get_competitions(season, game_type):
'''
https://www.rffm.es/api/competitions?temporada=19&tipojuego=1
A competition looks as follows:
.. code-block:: json
{
"codigo": "17217772",
"nombre": "COPA R.F.E.F. FASE AUTONOMICA",
"tipo_competicion": "2",
"Orden": "15",
"TipoJuego": "Futbol-11",
"CodigoCategoria": "40050",
"NombreCategoria": "TERCERA FEDERACION",
"cod_grupo_categoria": "4949443",
"nombre_grupo_categoria": "CATEGORIA NACIONAL",
"Activa": "0",
"FechaInicio": "2023-08-16",
"FechaFin": "2023-08-23",
"ptos_ganado": "3",
"ptos_empatado": "1",
"ptos_perdido": "0",
"minutos_juego": "90",
"numero_partes": "2",
"goleadores": "1",
"ver_estadisticas_jugador": "1",
"visible_clasificacion": "1"
}
'''
url = API_URL + f'competitions?temporada={season}&tipojuego={game_type}'
return r.get(url, verify=False).json()
def get_competition_groups(competition):
'''
https://www.rffm.es/api/groups?competicion=17145407
A group looks as follows:
.. code-block:: json
{
"codigo": "17145408",
"nombre": "Grupo 1",
"total_jornadas": "30",
"total_equipos": "16",
"clasificacion_porteros": "1",
"ver_clasificacion": "1",
"clasificacion_goleadores": "1",
"orden": "10"
}
'''
url = API_URL + f'groups?competicion={competition}'
return r.get(url, verify=False).json()
# seasons, game_types = get_seasons_and_game_types()
season = 19
game_type = 1
competition = 17145407
# group = 13768766
###### TIMING STUFF ######
total_start = time.perf_counter()
groups = get_competition_groups(competition)
for group in groups:
start = time.perf_counter()
get_competition_data(season, game_type, competition, group['codigo'])
end = time.perf_counter()
print(f'Finished in {end - start} seconds')
total_end = time.perf_counter()
print(f'Total finished in {total_end - total_start} seconds')