-
Notifications
You must be signed in to change notification settings - Fork 7
/
draftvid.py
343 lines (328 loc) · 13.2 KB
/
draftvid.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import re, requests,json
from config import *
#initialize chromecast
if chromeCast:
import pychromecast
from pychromecast.controllers.youtube import YouTubeController
chromecasts=pychromecast.get_chromecasts()
chromecasts,browser=pychromecast.get_listed_chromecasts(friendly_names=[chromeCastName])
try:
cast=chromecasts[0]
cast.wait()
ytc=YouTubeController()
cast.register_handler(ytc)
print ("\n\nConnected to: "+chromeCastName+"!")
except:
print ("\n\n"+chromeCastName+" not found, vids will be displayed on this screen")
chromeCast=False
#set up system variables for specific drafting sites
if "sleeper" in site.lower():
import time
site="sleeper"
sApi="https://api.sleeper.app/v1/draft/"+str(boardNum)+"/picks"
from vDictSleeper import vDictSleeper as vDict
elif "clicky" in site.lower():
site="clicky"
from vDictClicky import vDictClicky as vDict
elif "basmith7" in site.lower():
import time
site="basmith7"
from vDictbasmith7 import vDictbasmith7 as vDict
elif "espn" in site.lower():
site="espn"
import time
from vDictEspn import vDictEspn as vDict
mainUrl="https://www.espn.com/fantasy/football/"
boardUrl="fantasy.espn.com/football/draft?leagueId="
elif "yahoo" in site.lower():
site="yahoo"
import time
from vDictYahoo import vDictYahoo as vDict
mainUrl="https://sports.yahoo.com/fantasy/"
boardUrl="football.fantasysports.yahoo.com/draftclient"
else:
print ("\n\n\nConfig Error\n\n")
print ('In the config file, the line site= must have a choice of: "espn", "yahoo", "sleeper", "clicky", or "basmith7".')
import sys
exit()
#initialize selenium drivers and open draft boards
if chromeCast==False or site=="clicky" or site=="espn" or site=="yahoo":
# these lines are needed for all cases that use selenium
from selenium import webdriver
prefs={"profile.default_content_setting_values.notifications":2}
# this section is for draftboard selenium use cases (non youtube)
if site=="clicky" or site=="espn" or site=="yahoo":
draftOptions=webdriver.ChromeOptions()
draftOptions.add_experimental_option("prefs",prefs)
draftOptions.add_experimental_option('excludeSwitches', ['enable-logging'])
draftOptions.add_argument('disable-infobars')
#cannot be headless for espn or yahoo because you need to log in.
if draftBoardVisible==False and site!="espn" and site!="yahoo":
draftOptions.add_argument("--headless")
draftDriver=webdriver.Chrome(chrome_options=draftOptions)
if site=="clicky":
draftDriver.get('https://clickydraft.com/draftapp/board/'+str(boardNum))
elif site=="espn" or site=="yahoo":
draftDriver.get(mainUrl)
url=""
print ("waiting for user to open the "+site+" draft board")
# toggles through open tabs every 5 seconds to find your draft board.
while True:
for handle in draftDriver.window_handles:
draftDriver.switch_to.window(handle)
if boardUrl in draftDriver.current_url:
url=draftDriver.current_url
print ("We found your board! Ready to Draft!!!")
elif site not in draftDriver.current_url:
draftDriver.close()
if url!="":
break
else:
time.sleep(5)
# after the board is found, selenium loads it to give it focus
draftDriver.get(url)
# for yahoo, it will click on "draft results" or else selenium can't see the picks
if site=="yahoo":
from selenium.common.exceptions import NoSuchElementException as NSE
while True:
try:
resultsElement=draftDriver.find_element_by_xpath("//*[contains(text(), 'Draft Results')]")
resultsElement.click()
break
except NSE:
time.sleep(1)
draftDriver.minimize_window()
#setting up youtube display for selenium
if chromeCast==False:
youTubeOptions=webdriver.ChromeOptions()
#youTubeOptions.add_argument("user-data-dir=youTube")
youTubeOptions.add_argument("--start-maximized")
youTubeOptions.add_argument("--kiosk")
youTubeOptions.add_argument('disable-infobars')
youTubeOptions.add_experimental_option('excludeSwitches', ['enable-logging'])
youTubeOptions.add_experimental_option("prefs",prefs)
youTubeDriver=webdriver.Chrome(chrome_options=youTubeOptions)
youTubeDriver.get("https://www.youtube.com")
#setting up youtube links
yt="https://www.youtube.com/watch?v="
yt2="&feature=emb_rel_err"
ytSearch="https://www.youtube.com/results?search_query="
#videoEmbeddable:"true"
#functions to find tags if screen scraping is needed
s=re.compile('[^a-zA-Z]')
def findClickyTag(html,x):
if html.find(x)!=-1:
xhtml=html[html.find(x)+len(x):]
end=xhtml.find('<')
else:
return -1,html
return s.sub('',xhtml[:end]),xhtml
def findbasmith7Tag(html):
if html.find('1050371') < 0:
return False,"","","",""
else:
html=html[html.find('1050371')+21:]
fullName=html[:html.find('\\n')]
posString=html[html.find('\\n')+2:]
dashLoc=posString.find(' - ')
pos=posString[:dashLoc]
team=posString[dashLoc+3:dashLoc+6]
return True,s.sub('',fullName),pos,team,html
def findEspnTag(html):
nText='<span class="playerinfo__playername">'
tText='<span class="playerinfo__playerteam">'
pText='<span class="playerinfo__playerpos ttu">'
aPos=html.find(nText)
if aPos <0:
return html,"","",""
html=html[html.find(nText)+len(nText):]
fullName=html[:html.find('<')]
html=html[html.find(tText)+len(tText):]
team=html[:html.find('<')]
html=html[html.find(pText)+len(pText):]
pos=html[:html.find('<')]
return html,fullName,team,pos
def findYahooTag(html):
idText='<td class="Ta-c">'
fnText=' class="ys-player">'
teamText='<abbr title="'
posText='<abbr class="Mstart-4" title="'
aPos=html.find(idText)
if aPos <0:
return html,"","",""
html=html[html.find(fnText)+len(fnText):]
thisPlayer=html[:html.find('</tr>')]
fullName=thisPlayer[:thisPlayer.find('<')]
if thisPlayer.find(teamText)==-1:
pos="DEF"
return html,fullName,"",pos
thisPlayer=thisPlayer[thisPlayer.find(teamText)+1:]
team=thisPlayer[thisPlayer.find('>')+1:thisPlayer.find('<')]
thisPlayer=thisPlayer[thisPlayer.find(posText)+1:]
pos=thisPlayer[thisPlayer.find('>')+1:thisPlayer.find('<')]
return html,fullName,team,pos
def findYahooD(html): #need a separate function for defenses on yahoo
idText='Fw-b ys-player Mstart-4'
teamText='<span class="Mstart-4"><abbr title="'
posText='<abbr title="">'
if html.find(idText)==-1:
return "","","",""
html=html[html.find(idText):]
html=html[html.find('>')+1:]
fullName=html[:html.find('<')]
html=html[html.find(teamText)+len(teamText):]
html=html[html.find('>')+1:]
team=html[:html.find('<')]
html=html[html.find(posText)+len(posText):]
pos=html[:html.find('<')]
return html,fullName,team,pos
#this function plays the video found from youtube
def playVid(vLink):
if chromeCast:
ytc.play_video(vLink)
else:
youTubeDriver.get(yt+vLink+yt2)
try:
playaE=youTubeDriver.find_element_by_id('movie_player')
except:
nada=0
try:
playaE.send_keys('f')
except:
nada=0
return
#searches Youtube for link
def findVLink(fName,lName):
try:
url=ytSearch+fName+"+"+lName+"+highlights"
response = requests.get(url)
yhtml=response.text
yhtml=yhtml[yhtml.find('href="/watch?v=')+15:]
vLink=yhtml[:yhtml.find('"')]
if "><" in vLink:
yhtml=response.text
yhtml=yhtml[yhtml.find('"videoId":"')+11:]
vLink=yhtml[:yhtml.find('"')]
except requests.exceptions.RequestException:
vLink=""
return vLink
#function runs when player is found on the draft board
def addPlayer(thisPlayer,pTable,choiceActive,vDict,vStr,fName,lName):
if thisPlayer not in pTable:
try:
vLink=vDict[vStr]
except KeyError:
vLink=""
if choiceActive:
if vLink!="":
playVid(vLink)
elif autoSearch:
vLink=findVLink(fName,lName)
if vLink!="":
playVid(vLink)
pTable.append(thisPlayer)
return pTable
def skipAds():
if chromeCast == False:
try:
skipE=youTubeDriver.find_element_by_class_name("ytp-ad-skip-button-container")
skipE.click()
time.sleep(1)
except:
nada=0
return
#create player table
pTable=[]
#choiceActive set to false at the beginning so the first player loop doesn't play
# videos for the players already entered.
choiceActive=False
while (True):
skipAds()
if site=="clicky":
html=draftDriver.page_source
html=html[html.find("<tbody>"):html.find("</tbody>")]
for x in range (0,teams*rounds):
html=html[html.find('class="pickContents"'):]
pos,html=findClickyTag(html,'class="playerPos">')
team,html=findClickyTag(html,'class="playerTeam">')
fName,html=findClickyTag(html,'class="playerFName">')
lName,html=findClickyTag(html,'class="playerLName">')
thisPlayer=[pos,team,fName,lName]
vStr=pos+team+fName+lName
pTable=addPlayer(thisPlayer,pTable,choiceActive,vDict,vStr,fName,lName)
elif site=="sleeper":
time.sleep(1) # 1 second wait in between api calls
while True:
try:
response = requests.get(sApi)
yJson=json.loads(response.text)
break
except requests.exceptions.RequestException:
yJson=""
print ("call to: "+sApi+" failed, trying again in 5 seconds")
time.sleep(5)
for x in range(0,len(yJson)):
pos=yJson[x]["metadata"]["position"]+yJson[x]["metadata"]["team"]
fName=yJson[x]["metadata"]["first_name"]
lName=yJson[x]["metadata"]["last_name"]
thisPlayer=[pos,fName,lName]
vStr=pos+fName+lName
pTable=addPlayer(thisPlayer,pTable,choiceActive,vDict,vStr,fName,lName)
elif site=="basmith7":
time.sleep(5) # 5 second wait in between page requests
while True:
try:
response = requests.get(basmith7URL)
html=response.text
break
except requests.exceptions.RequestException:
html=""
print ("call to: "+basmith7URL+" failed, trying again in 5 seconds")
time.sleep(5)
html=html[html.find("var bootstrapData = ")+20:]
html=html[:html.find(";")]
while True:
foundName,fullName,pos,team,html=findbasmith7Tag(html)
if foundName:
vStr=s.sub('',str(pos)+str(team)+str(fullName))
thisPlayer=[pos,team,fullName]
pTable=addPlayer(thisPlayer,pTable,choiceActive,vDict,vStr,"",fullName)
else:
break
elif site=="espn":
html=draftDriver.page_source
while True:
html,fullName,team,pos=findEspnTag(html)
if fullName=="":
break
vStr=s.sub('',str(pos)+str(team)+str(fullName))
thisPlayer=[pos,team,fullName]
pTable=addPlayer(thisPlayer,pTable,choiceActive,vDict,vStr,"",fullName)
elif site=="yahoo":
html=draftDriver.page_source
dhtml=html
while True:
html,fullName,team,pos=findYahooTag(html)
if fullName=="":
break
if pos!="DEF":
vStr=s.sub('',str(pos)+str(team)+str(fullName))
thisPlayer=[pos,team,fullName]
pTable=addPlayer(thisPlayer,pTable,choiceActive,vDict,vStr,"",fullName)
while True:
dhtml,fullName,team,pos=findYahooD(dhtml)
if fullName=="":
break
if pos=="DEF":
vStr=s.sub('',str(pos)+str(team)+str(fullName))
thisPlayer=[pos,team,fullName]
pTable=addPlayer(thisPlayer,pTable,choiceActive,vDict,vStr,"",fullName)
choiceActive=True
if len(pTable)>=(teams*rounds):
break
if site=="clicky" or site=="espn" or site=="yahoo":
draftDriver.quit()
if chromeCast==False:
print("\n\nDraft completed! YouTube window will close in 5 minutes.\n\n")
time.sleep(300)
youTubeDriver.quit()