-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfind_public_league.py
55 lines (48 loc) · 1.7 KB
/
find_public_league.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
from io import StringIO
from time import sleep
from pandas import read_html
from ffbot.scraper import create_session, generate_user_agent
s = create_session()
found_public, found_public_idp = False, False
for public_league in range(0, 1000):
url = "https://football.fantasysports.yahoo.com/f1/{}/settings".format(
public_league
)
s.headers["User-Agent"] = generate_user_agent()
r = s.get(url)
# Parse tables from page, if tables exist (i.e. public league)
try:
dfs = read_html(StringIO(r.text))
except ValueError as e:
if str(e) == "No tables found":
print(f"{public_league} is not a valid public league")
sleep(10)
continue
else:
raise e
# Parse league settings to detect if is DEF or IDP
df = dfs[0]
positions = df.loc[df["Setting"] == "Roster\xa0Positions:", "Value"].iloc[0]
if "DEF" in positions:
found_public = True
print(f"{public_league} is a valid public league")
elif "DB" in positions:
# Confirm players list is accessible
url = "https://football.fantasysports.yahoo.com/f1/{}/players".format(
public_league
)
s.headers["User-Agent"] = generate_user_agent()
r = s.get(url)
try:
dfs = read_html(StringIO(r.text))
except ValueError as e:
if str(e) == "No tables found":
print(f"{public_league} is not a valid public IDP league")
sleep(10)
continue
else:
raise e
found_public_idp = True
print(f"{public_league} is a valid public IDP league")
if found_public and found_public_idp:
break