-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_elf_pbp.py
167 lines (138 loc) · 5.65 KB
/
parse_elf_pbp.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
import logging
import os
import time
# from datetime import datetime
from random import randrange
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from tqdm import tqdm
from get_elf_schedule import get_elf_schedule
def get_efl_raw_pbp(season: int, overwrite_existing_cache: bool = False):
"""
"""
# now = datetime.now()
schedule_df = get_elf_schedule(season_filter=season)
schedule_df = schedule_df.loc[
schedule_df["has_game_started"] == True # noqa: E712
]
schedule_df = schedule_df.loc[
schedule_df["is_cancelled"] == False # noqa: E712
]
week_arr = schedule_df["week"].to_numpy()
game_id_arr = schedule_df["game_id"].to_numpy()
game_datetime_arr = schedule_df["date"].to_numpy()
away_team_id_arr = schedule_df["away_team_id"].to_numpy()
away_team_slug_arr = schedule_df["away_team_slug"].to_numpy()
home_team_id_arr = schedule_df["home_team_id"].to_numpy()
home_team_slug_arr = schedule_df["home_team_slug"].to_numpy()
# response = requests.get(url=url)
driver = webdriver.Chrome()
for i in tqdm(range(0, len(game_id_arr))):
# with open("test.html", "w+", encoding="utf-8") as f:
# f.write(response.text)
pbp_df = pd.DataFrame()
pbp_df_arr = []
temp_df = pd.DataFrame()
game_week = week_arr[i]
game_id = game_id_arr[i]
game_datetime = game_datetime_arr[i]
away_team = away_team_slug_arr[i]
away_team_id = away_team_id_arr[i]
home_team = home_team_slug_arr[i]
home_team_id = home_team_id_arr[i]
if os.path.exists(f"pbp/raw/{game_id}.csv") \
and overwrite_existing_cache is False:
pass
else:
url = "https://europeanleague.football/game-center/schedule/" +\
f"{away_team}-at-{home_team}-{game_id}"
driver.get(url)
time.sleep(10)
has_pbp_data = False
soup = BeautifulSoup(driver.page_source, features='lxml')
if "Internal Server Error" in soup.text:
has_pbp_data = False
else:
rand_num = randrange(1000, 3000)
driver.execute_script(f"window.scrollBy(0,{rand_num})", "")
has_pbp_data = True
try:
pbp_data = soup.find(
"div", {"class": "@container flex flex-col gap-12"}
)
except Exception as e:
has_pbp_data = False
logging.info(
"No PBP data was found, skipping this game. " +
f"Full exception `{e}`"
)
if has_pbp_data is True:
quarters = pbp_data.find_all(
"div", {"class": "flex flex-col gap-6"}
)
quarter_num = 0
for q in quarters:
quarter_num += 1
drives = q.find_all(
"div",
{
"class": "relative rounded-xl " +
"overflow-hidden duration-200 " +
"bg-elf-blue-800 lg:hover:bg-elf-blue-700/70"
}
)
drive_num = 0
for d in drives:
drive_num += 1
plays = d.find_all(
"div",
{
"class": "px-4 md:px-6 py-4 " +
"border-t border-elf-blue-200/30"
}
)
for p in plays:
play_type = p.find(
"p",
{
"class": "text-lg font-bold " +
"mb-1 leading-normal"
}
).text
play_desc = p.find("p", {"class": "text-sm mb-1"}).text
down_and_distance = p.find(
"p", {"class": "text-sm text-elf-blue-100/70"}
).text
temp_df = pd.DataFrame(
{
"season": season,
"game_week": game_week,
"game_datetime": game_datetime,
"away_team": away_team,
"away_team_id": away_team_id,
"home_team": home_team,
"home_team_id": home_team_id,
"quarter_num": quarter_num,
"drive_num": drive_num,
"play_type": play_type,
"play_desc": play_desc,
"down_and_distance": down_and_distance,
},
index=[0]
)
pbp_df_arr.append(temp_df)
del temp_df
if len(pbp_df_arr) == 0:
pass
elif len(pbp_df_arr) > 0:
pbp_df = pd.concat(pbp_df_arr, ignore_index=True)
pbp_df.to_csv(
f"pbp/raw/{game_id}.csv",
index=False
)
if __name__ == "__main__":
get_efl_raw_pbp(
2024,
# overwrite_existing_cache=True
)