-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedule_stats.py
163 lines (130 loc) · 5.26 KB
/
schedule_stats.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
import mysql.connector
import requests
from bs4 import BeautifulSoup
import os
def getTeam(soup,cursor,cnx):
i = 19
j = 20
print "________________________________________________________________________________________________________________________"
print "Team Info: " + str(i) + "/" + str(j) + " season"
list = soup.find('ul', {"class": "list-unstyled"})
# puts the list items into an array
list_items = list.findAll('li')
# print list_items
teamName = list_items[0].text
return teamName
def scheduleStats(soup, cursor, cnx, teamName):
#table 56 18/19 schedule
#table 57 17/18 schedule
#table 58 16/17 schedule
#table 59 15/16 schedule
tables = soup.find_all("table")
i = 19
j = 20
for table in tables[56:60]:
print "________________________________________________________________________________________________________________________"
print "Schedule Stats: ", i,"/",j," season"
rows = table.find_all("tr")
for row in rows[1:]:
points = row.find_all("td")
dateText = points[0].text
team = teamName
if points[1].text == "H":
home = 1
else:
home = 0
opponent = points[3].text
if points[5].text == "W":
win = 1
else:
win = 0
pointsScored = int(points[7].text)
# print("Points Scored:" + str(pointsScored))
pointsAllowed = int(points[8].text)
# print("Points Allowed:" + str(pointsAllowed))
margin = int(points[9].text)
# print("Margin:" + str(margin))
simpleRPI = int(points[12].text)
# print("Simple RPI:" + str(simpleRPI))
inserts = (team, dateText, home, opponent, win, pointsScored, pointsAllowed, margin, simpleRPI)
insertStats = "INSERT INTO scheduleStats(team, dateText, home, opponent, win, ptsScored, ptsAllowed, margin, simpleRPI) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)"
# inserts the stats into whatever table is designated
cursor.execute(insertStats, inserts)
cnx.commit()
print "Finished inserting data for: " + team + " vs. " + opponent
i-=1
j-=1
return
def scheduleStatsThisYear(soup, cursor, cnx, teamName):
#table 56 18/19 schedule
#table 57 17/18 schedule
#table 58 16/17 schedule
#table 59 15/16 schedule
tables = soup.find_all("table")
i = 19
j = 20
for table in tables[-10]:
print "________________________________________________________________________________________________________________________"
print "Schedule Stats: ", i,"/",j," season"
rows = table.find_all("tr")
for row in rows[1:]:
seasonID = 5
points = row.find_all("td")
dateText = points[0].text
team = teamName
if points[1].text == "H":
home = 1
else:
home = 0
opponent = points[3].text
if points[5].text == "W":
win = 1
else:
win = 0
pointsScored = int(points[7].text)
# print("Points Scored:" + str(pointsScored))
pointsAllowed = int(points[8].text)
# print("Points Allowed:" + str(pointsAllowed))
margin = int(points[9].text)
# print("Margin:" + str(margin))
simpleRPI = int(points[12].text)
# print("Simple RPI:" + str(simpleRPI))
inserts = (seasonID,team, dateText, home, opponent, win, pointsScored, pointsAllowed, margin, simpleRPI)
insertStats = "INSERT INTO scheduleStats(seasonID, team, dateText, home, opponent, win, ptsScored, ptsAllowed, margin, simpleRPI) VALUES(%s,%s, %s, %s, %s, %s, %s, %s, %s, %s)"
# inserts the stats into whatever table is designated
cursor.execute(insertStats, inserts)
cnx.commit()
print "Finished inserting data for: " + team + " vs. " + opponent
i-=1
j-=1
return
def main():
cnx = mysql.connector.connect(user="wsa",
host="34.68.250.121",
database="NCAAWomens",
password="LeBron>MJ!")
cursor = cnx.cursor(buffered=True)
# whoever uses this needs to change the directory in the string
for subdir, dirs, files in os.walk("/Users/lissjust/Desktop/NCAAWomens/upcomingFilesNeeded/teamFiles/teamOneTeamFile"):
for file in files:
#print os.path.join(subdir, file)
filepath = subdir + os.sep + file
if filepath.endswith(".htm"):
html = open(filepath).read()
soup = BeautifulSoup(html, 'html.parser')
teamName = "Wisconsin"
scheduleStatsThisYear(soup, cursor, cnx, teamName)
print (filepath)
# print(path_in_str)
'''fileName = 'lakenwairau.htm'
print fileName
html = open(fileName).read()
soup = BeautifulSoup(html, 'html.parser')
teamStats(soup, cursor, cnx)
'''
cursor.close()
cnx.commit()
cnx.close()
return
if __name__=="__main__":
main()