-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_elf_json.py
60 lines (44 loc) · 1.59 KB
/
get_elf_json.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
from datetime import datetime
import json
import logging
import time
import pandas as pd
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm
def save_elf_game_json(game_id: str):
"""
"""
game_url = f"https://www.sportsmetrics.football/games/{game_id}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/114.0.0.0 Safari/537.36"
}
response = requests.get(game_url, headers=headers)
soup = BeautifulSoup(response.text, features='lxml')
json_string = soup.find(
'script', {'id': '__NEXT_DATA__', 'type': 'application/json'})
json_data = json.loads(json_string.text)
del json_string
with open(f'raw_game_data/JSON/{game_id}.json', 'w+') as f:
f.write(json.dumps(json_data['props']['pageProps'], indent=2))
# print(json_data)
time.sleep(2)
return json_data
def save_all_elf_game_json(season: int):
"""
"""
games_df = pd.read_csv(f'schedule/{season}_elf_schedule.csv')
# games_df = games_df.dropna(subset=['gamebook_url'])
# games_df = games_df.dropna(subset=["away_team_score", "home_team_score"])
games_df = games_df[games_df["has_game_started"]]
game_ids_arr = games_df['sports_metrics_game_id'].to_numpy()
for game_id in tqdm(game_ids_arr):
try:
save_elf_game_json(game_id)
except Exception as e:
logging.warning(f"Could not download `{game_id}`. Reason `{e}`.")
if __name__ == "__main__":
now = datetime.now()
save_all_elf_game_json(now.year)