Repository navigation
Expand file tree
/
Copy pathplay26.py
More file actions
223 lines (195 loc) · 8.18 KB
/
Copy pathplay26.py
File metadata and controls
223 lines (195 loc) · 8.18 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""import packages"""
import codecs
from datetime import datetime
from playwright.sync_api import Playwright, sync_playwright
from gatePLAY import lst_dates_cumul, TORNEO
# WORKFLOW -> sel25 -> MEROBOT A GIT EN SHELL - LAUNCHWD
lst_jornadas = []
lst_clubes = []
lst_home = []
lst_away = []
lst_match = []
lst_goles = []
lst_goles_home = []
lst_goles_away = []
lst_goles_home_half = []
lst_goles_away_half = []
lst_indexes_home = []
lst_indexes_away = []
lst_dates_filtered = []
dates_final = []
def run(playwright: Playwright) -> None:
"""Main function to run the Playwright script"""
browser = playwright.chromium.launch(headless=True)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.kicker.de/")
page.get_by_role("link", name="Zustimmen & weiter").click(timeout=60000)
get_clubes_stats(page)
# ---------------------
context.close()
browser.close()
def convert_dates(lst_dates_cumul):
"""Function to convert date strings to datetime objects and filter /
them using date mathematics"""
for index, value in enumerate(lst_dates_cumul):
if index < len(lst_dates_cumul) - 1:
element_list_one = lst_dates_cumul[index].split(".")
element_list_two = lst_dates_cumul[index + 1].split(".")
date_first = datetime(int(element_list_one[2]), int(element_list_one[1]), \
int(element_list_one[0]))
date_last = datetime(int(element_list_two[2]), int(element_list_two[1]), \
int(element_list_two[0]))
if index == 0:
date_first_str = datetime.strftime(date_first, '[%a. %d.%#m.]')
lst_dates_filtered.append(date_first_str)
else:
if (date_last - date_first).days > 1:
# convertir el objeto de fecha a un string con el formato deseado
date_last_str = datetime.strftime(date_last, '[%a. %d.%#m.]')
lst_dates_filtered.append(date_last_str)
return lst_dates_filtered
dates_final = convert_dates(lst_dates_cumul)
def get_clubes_stats(page):
"""Function to get the clubs stats from the Bundesliga page"""
page.goto(f'https://kicker.de/bundesliga/spieltag/{TORNEO}/-1', timeout=60000)
clubes = page.locator(".kick__v100-gameCell__team__name").all_inner_texts()
jornadas = page.locator(".kick__section-headline").all_inner_texts()
goles = page.locator(".kick__v100-scoreBoard__scoreHolder__score").all_inner_texts()
# para torneo 2025 solamente o irregularidades cambiando el index
#goles.insert(486, "0")
#goles.insert(487, "2")
lst_clubes = clubes
# por partidos suspendidos por nevadas torneo 2025/26
del lst_clubes[284]
del lst_clubes[284]
del lst_clubes[284]
del lst_clubes[284]
del lst_clubes[300]
del lst_clubes[300]
lst_goles = goles
for jornada in jornadas:
lst_jornadas.append(jornada)
classify_teams(lst_clubes)
get_goals_away_indexes(lst_goles)
get_goals_home_indexes(lst_goles)
set_goles_home(lst_goles)
set_goles_away(lst_goles)
goles_class()
match_in()
actualizar_open(TORNEO, lst_match, lst_jornadas, dates_final)
#me_robot()
#print(len(clubes))
#print(len(jornadas))
#print(len(goles))
def classify_teams(lst_clubes):
"""Function to classify teams into home and away lists"""
for ind, club in enumerate(lst_clubes):
if ind % 2 == 0:
lst_home.append(club)
else:
lst_away.append(club)
def goles_class():
"""Function to classify goals into goals home and goals away lists"""
nbuffer = 0
for n in range(0, len(lst_goles)):
nbuffer = nbuffer + n
if nbuffer < len(lst_goles):
goal = lst_goles[nbuffer]
lst_goles_home.append(goal)
nbuffer = nbuffer + 1
if nbuffer < len(lst_goles):
goal = lst_goles[nbuffer]
lst_goles_home_half.append(goal)
nbuffer = nbuffer + 1
if nbuffer < len(lst_goles):
goal = lst_goles[nbuffer]
lst_goles_away.append(goal)
nbuffer = nbuffer + 1
if nbuffer < len(lst_goles):
goal = lst_goles[nbuffer]
lst_goles_away_half.append(goal)
def get_goals_away_indexes(lst_goles):
"""Function to get indexes of away goals in the goals list jumping halftime scores"""
factor = 2
while factor < len(lst_goles):
lst_indexes_away.append(factor)
factor = factor + 4
def get_goals_home_indexes(lst_goles):
"""Function to get indexes of home goals in the goals list jumping halftime scores"""
factor = 0
while factor < len(lst_goles):
lst_indexes_home.append(factor)
factor = factor + 4
def set_goles_away(lst_goles):
"""Function to set away goals based on the indexes collected"""
for index in lst_indexes_away:
if index < len(lst_goles):
element = lst_goles[index - 1]
lst_goles_away.append(element)
def set_goles_home(lst_goles):
"""Function to set home goals based on the indexes collected"""
for index in lst_indexes_home:
if index < len(lst_goles):
element = lst_goles[index]
lst_goles_home.append(element)
def match_in():
"""Function to create a structured list of matches with home and away teams and their scores"""
for i in range(0, min(len(lst_goles_home), len(lst_goles_away))):
lst_match.append(f' {lst_home[i]} {lst_goles_home[i]}-{lst_goles_away[i]} \
{lst_away[i]}\n')
return lst_match
#def me_robot():
# """Function to write the match data into a text file with the tournament name"""
# with codecs.open(f'C:/Users/enado/Proyectos/Python33/merobot/bundesliga-{TORNEO[:2]}{TORNEO[5:]}.txt', "w", "utf-8") as file:
# file.write("\ufeff")
# count_jornadas = 0
# for g, line in enumerate(lst_match):
# #g = lst_match.index(line)
# if count_jornadas == 16:
# divider = 7
# elif count_jornadas == 17:
# divider = 8
# else:
# divider = 9
#
# if g % divider == 0:
# file.write(lst_jornadas[count_jornadas] + "\n")
# if count_jornadas < len(dates_final):
# file.write(dates_final[count_jornadas] + '\n')
# file.write(f' {line}')
# count_jornadas += 1
# else:
# file.write(f' {line}')
def actualizar_open(TORNEO, lst_match, lst_jornadas, dates_final):
"""Function to write the match data into a text file with the tournament name using open()"""
# La ruta y el nombre del archivo se construyen igual.
# Usamos 'w' para escribir (write) y 'encoding="utf-8"' para especificar la codificación.
with open(f'C:/Users/enado/Proyectos/Python33/merobot/bundesliga-{TORNEO[:2]}{TORNEO[5:]}.txt', "w", encoding="utf-8") as file:
# El carácter '\ufeff' (BOM - Byte Order Mark) ya no suele ser necesario
# cuando se usa 'utf-8' de forma estándar en 'open()', pero si es vital
# para tu aplicación, lo mantienes. Para archivos que solo contienen
# texto, normalmente se omite.
# file.write("\ufeff")
count_jornadas = 0
for g, line in enumerate(lst_match):
# En lugar de usar lst_match.index(line), es más eficiente usar enumerate(lst_match)
# para obtener el índice 'g' y el valor 'line' directamente.
# if count_jornadas == 16:
# divider = 7
# elif count_jornadas == 17:
# divider = 8
# else:
# divider = 9
#
# if g % divider == 0:
if (g % 9 == 0) is True:
if count_jornadas < len(dates_final):
file.write(lst_jornadas[count_jornadas] + "\n")
file.write(dates_final[count_jornadas] + '\n')
file.write(f' {line}')
count_jornadas += 1
else:
file.write(f' {line}')
with sync_playwright() as playwright:
run(playwright)