-
Notifications
You must be signed in to change notification settings - Fork 1
/
cbbpolldata.py
51 lines (39 loc) · 1.54 KB
/
cbbpolldata.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
from urllib.request import urlopen, Request
import json
#Get Team List of current active teams. cbbpoll.net, espn, r/collegebasketball, and kenpom (Unused)
def get_teams():
with open('data/team_list.txt','r') as imp_file:
lines=imp_file.readlines()
flairs={}
rank_names={}
for line in lines:
(team,flair,rank_name,kenpom)=line.replace('\n','').split(',')
#Handle Hawaii Strings from cbbpoll.net
if rank_name == "Hawaiʻi" or rank_name == "HawaiÊ»i":
rank_name = "Hawaii"
flairs[team]=flair
rank_names[rank_name]=team
return flairs,rank_names
#Request Webpage
def webrequest():
try:
req = Request("https://www.cbbpoll.net/")
req.headers["User-Agent"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17'
except:
print("Failed to Pull cbbpoll json file")
raise
return req
#Pull the json
def loaddata(req):
line = urlopen(req).read().decode('utf-8')
#Top 25 Search
start_str = 'type="application/json">'
start_index = line.find(start_str)
data = json.loads(line[start_index + len(start_str): line.find('</script>', start_index)])
top25rank = data['props']['pageProps']['userpoll']
#Week Search
start_str = 'Week <!-- -->'
end_str = '<!-- --> Poll'
start_index = line.find(start_str)
currentweek = line[start_index + len(start_str):line.find(end_str)]
return top25rank, currentweek