-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
569 lines (485 loc) · 22 KB
/
bot.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import time
import logging
import random # to do random waits everywhere
from datetime import datetime, timedelta, date
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, ElementNotVisibleException
from exception import NoPlayerFoundException, SameNameException
from config import ROOT, playerExceptions
class Bot(object):
teams = (
'mia', 'cws', 'nyy', 'lad', 'tor', 'phi', 'cle', 'stl', 'oak',
'hou', 'nym', 'cin', 'sd', 'tex', 'det', 'sea', 'sf', 'col',
'pit', 'laa', 'wsh', 'chc', 'bos', 'tb', 'ari', 'atl',
'mil', 'min', 'kc', 'bal')
def __init__(self, username, password, activeDate, dev=False):
"""
string string -> None
username: str | username on beat the streak
password: str | password for given username
browser: webdriver | firefox web browser
pageTitles: dict | dictionary of page titles for key pages
teams: dict | keys: team full names (e.g Boston Red Sox), values:
3 digit abbrevations used in beatthestreak html code
dev: Indicates we are using this in dev mode, which changes
"today" to "tomorrow", so that we don't run into the issue
of player selections becoming locked
Instantiates the bot, which represents an account
on MLB's beat the streak
"""
assert type(username) == str
assert type(password) == str
assert type(activeDate) == date
## webdriver needs a display to run. This sets up a virtual "fake" one
if ROOT == '/home/faiyamrahman/programming/Python/beatthestreakBots':
print "--> Starting BOT display"
self.display = Display(visible=0, size=(1024, 768))
print "--> successfully assigned display parameters"
self.display.start()
print "--> succesffully started display"
else:
self.display = None
## Set core attributes
self.username = username
self.password = password
self.activeDate = activeDate
print "-->Succesfully set username, password, and activeDate for bot"
self.browser = webdriver.Chrome()
print "-->Succesfully created chrome webdriver"
self.today = datetime.today()
self.dev = dev
if self.dev:
self.today = self.today + timedelta(days=1)
# Create a logger so that we don't get blasted with unnecessary info
# on every error in a test suite. Instead, we only get high priority shit
seleniumLogger = logging.getLogger('selenium.webdriver.remote.remote_connection')
seleniumLogger.setLevel(logging.WARNING)
# used to check if we are on the right page at the right time
self.pageTitles = {
'login':'Account Management - Login/Register | MLB.com: Account',
'picks': 'Beat The Streak | MLB.com: Fantasy'}
def choose_players(self, **kwargs):
"""
TupleOfStrings TupleOfStrings bool -> None|TupleOfFloats
p1 and p2: Either the empty tuple or a tuple of format
(firstName, lastName, teamAbbreviation) where firstName and
lastName define a player in the MLB and teamAbbrevations
is the team abbreviation used in beatthestreak's html code,
as defined in self.teams
activeDate: datetime.date | the date for which we are Choosing
players
If p1 and p2 are both nonEmpty, choose p1 and p2. Else, we choose
p1, which should be nonempty.
"""
## Extract variables
p1 = kwargs['p1']
p2 = kwargs['p2']
## Type checking
# should both be tuples
assert type(p1) == tuple
assert type(p2) == tuple
# p1 should be of length 3, contain strings, and have a valid team
assert len(p1) == 3
for item in p1:
assert type(item) == str
assert p1[2] in self.teams
# p2 should be of length 0 or 3, and in the later case contain strings
# and have a valid team
assert len(p2) in (0,3)
if len(p2) == 3:
for item in p2:
assert type(item) == str
assert p2[2] in self.teams
# Reset selections and then choose players
print "------> Removing any currently selected players"
self._reset_selections()
assert self.browser.title == self.pageTitles['picks']
self.__choose_single_player( player=p1, doubleDown=False)
if len(p2) == 3:
self.__choose_single_player( player=p2, doubleDown=True)
# Close up shop, unless we're devving, in case we want to keep the
# screen open for testing
if not self.dev:
self.quit_browser()
return p1, p2
def get_streak_length(self):
"""
Returns the current streak length for the bot
"""
## Make sure the bot was initalized with the correct date
assert self.activeDate == datetime.today().date() - timedelta(days=1)
try:
# Get yesterday's make pick row
yesterdayRow = self.__get_date_make_pick_row()
# Get the streak length
streakElem = yesterdayRow.find_element_by_class_name('streak')
streak = int(streakElem.text)
## Close the browser and return the value
self.quit_browser()
return streak
except:
try:
self.quit_browser()
raise
except:
raise
def __choose_single_player(self, **kwargs):
"""
TupleOfStrings bool -> None
player: (firstName, lastName, teamAbbreviation)
doubleDown: True if this is a doubleDown pick, False otherwise
Assigns player player to bot.
"""
global playerExceptions
try:
## Extract variables
player = kwargs['player']
doubleDown = kwargs['doubleDown']
## Type checking
assert type(player) == tuple
assert len(player) == 3
assert player[2] in self.teams
## Let the player know what's up
print "------> Choosing player: {}".format(player)
## Variable assignments
firstName, lastName, team = player
# Get team selection dropdwon
self.__get_team_selection_dropdown(team=team)
## Make pick
# get lists of firstNames, lastNames, and select Buttons
namesThereYet = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "last-name")))
lastNameElems = self.browser.find_elements_by_class_name("last-name")
firstNameElems = self.browser.find_elements_by_class_name("first-name")
selectButtons = self.browser.find_elements_by_class_name("select-action")
# If necessary, sift out the appearances of the pitchers name
pitcherDisplayed = ( len(lastNameElems) == (2 * len(selectButtons)) )
# If this is a double down, and the first selection was for a player
# on the same team as this one, then one of the "select buttons"
# is now a remove button, so the condition is slightly altered
pitcherDisplayedPlusSameTeam = ( len(lastNameElems) == (2 * (len(selectButtons) + 1)) )
if pitcherDisplayed or pitcherDisplayedPlusSameTeam:
teamLastNameElems = [elem for elem in lastNameElems
if lastNameElems.index(elem) % 2 == 0]
teamFirstNameElems = [elem for elem in firstNameElems
if firstNameElems.index(elem) % 2 == 0]
else: # pitcher is not displayed
teamLastNameElems = lastNameElems
teamFirstNameElems = firstNameElems
firstNameMatches = [teamFirstNameElems.index(elem) for elem
in teamFirstNameElems if elem.text == firstName]
lastNameMatches = [teamLastNameElems.index(elem) for elem
in teamLastNameElems if elem.text == lastName]
fullNameMatches = [ index for index in lastNameMatches if
(index in firstNameMatches) or # pitcher is displayed
( (index * 2) in firstNameMatches)] # pitcher is not displayed
numMatches = len(fullNameMatches)
if numMatches == 0:
raise NoPlayerFoundException("Player {0} {1} on team {2}".format(
firstName, lastName, team) + " was not found")
if numMatches == 1:
selectButtons[fullNameMatches[0]].click()
elif numMatches > 1: # pragma: no cover
raise SameNameException("The {0} have two players ".format(team) + \
"with the same name: {0} {1}".format(firstName, lastName))
if doubleDown:
# first button : Double Down. SecondButton: Replace Selection
buttonsThereYet = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'ui-button-text-only')))
buttons = self.browser.find_elements_by_class_name('ui-button-text-only')
buttons[0].click()
time.sleep(3)
except NoPlayerFoundException:
if player in playerExceptions.keys():
playerExceptions[player] += 1
else:
playerExceptions[player] = 1
raise
def __get_team_selection_dropdown(self, **kwargs):
"""
kwargs --> None
team: str | team for which we are selecting the dropdown
"""
## Extract variables
team = kwargs['team']
## Typecheck
assert type(team) == str
## Necessary team conversions
team = self.__convert_team(team)
## Make sure the player selection dropdown has been dropped
self._get_player_selection_dropdown()
selectTeam = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.ID, 'team-name')))
selectTeam.click()
time.sleep(3)
# click on desired team
team = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, team)))
team.click()
time.sleep(3)
def _get_player_selection_dropdown(self):
"""
Gets the player selection dropdown box for date self.activeDate
Should be used from the make picks page
"""
# make sure we're on the make picks page
self._get_make_picks_page()
selectTeamBox = self.browser.find_elements_by_id('team-name')[0]
if selectTeamBox.is_displayed():
return
else:
self._click_make_pick_for_date() # includes a sleep at the end
def _click_make_pick_for_date(self):
"""
Clicks on make pick for self.activeDate
"""
## make sure we're on the right page
self._get_make_picks_page()
# get date's row
dateRow = self.__get_date_make_pick_row()
datePlayerInfoRow = dateRow.find_element_by_class_name('player-info-rows')
datePlayerInfoRow.click()
time.sleep(3)
def __get_date_make_pick_row(self):
"""
Returns the row on the make picks page corresponding to self.activeDate
"""
# make sure we are on the get make picks page
self._get_make_picks_page()
## Get the row corresponding to d
firstRow = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.TAG_NAME, 'tr')))
rows = self.browser.find_elements_by_tag_name('tr')
## date in 'mm/dd/yyyy' format
dateFormatted = self.activeDate.strftime('%m/%d/%Y')
dateRow = [ row for row in rows if
row.get_attribute('data-date') == dateFormatted][0]
return dateRow
def get_recommended_players(self):
"""
Returns a tuple of players (firstName, lastName, teamAbbrev) corresponding
to the players recommended by MLB for date self.activeDate
"""
try:
# Variable initializations
recommendedPlayers = []
# Make sure we are on the make picks page and check that the
# recommendedPlayers player grid has dropped down
self._get_player_selection_dropdown()
WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.ID, 'bts-players-grid')))
# get rows correspodning to recommended players
playerGrid = self.browser.find_element_by_id('bts-players-grid')
playerRows = playerGrid.find_elements_by_tag_name('tr')[1:] # 0th elem is header of table
# collect information about recommended players
for row in playerRows:
playerInfo = row.find_element_by_class_name('player-team')
playerTuple = (
str( playerInfo.find_element_by_class_name('first-name').text),
str( playerInfo.find_element_by_class_name('last-name').text),
str( playerInfo.find_element_by_class_name('team').text).lower(),
)
recommendedPlayers.append(playerTuple)
except:
self.quit_browser()
raise
## Close up shop
self.quit_browser()
# tuplify the list and return it
return tuple(recommendedPlayers)
def get_opposing_pitcher_era(self, p1=()):
"""
TupleOfStrings -> float
p1 and p2: A tuple of format
(firstName, lastName, teamAbbreviation)
where firstName and lastName define a player in the MLB and
teamAbbrevations is the team abbreviation used in beatthestreak's
html code, as defined in self.teams
Returns the ERA of the starting pitcher facing player today
Assumes the player is playing today
"""
firstName, lastName, team = p1
assert team in self.teams
# Get team selection dropdwon
self.__get_team_selection_dropdown(team)
# Get the era and return it
eraElem = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'era')))
eraText = eraElem.text # e.g u'4.12 ERA'
era = float(eraText.split()[0])
self.quit_browser()
return era
def __convert_team(self, team):
"""
MLB and ESPN have some discrepancies in how they list players to viewers
and how they list them in their html code. THis function converts it
from viewer format to html code format, if necessary
"""
## Type check/sanity check
assert team in self.teams
if team == 'laa': # los angeles angels of anaheim
team = 'ana'
elif team == 'lad': # los angeles dodgers
team = 'la'
elif team == 'wsh': # washington nationals
team = 'was'
return team
def claim_mulligan(self):
print "-->Claiming Mulligan for u, p: {0}, {1}\n".format(self.username, self.password)
try:
## navigate to claim mulligan page
self._get_make_picks_page()
more = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "last"))
)
more.click()
button1 = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "bam-button-primary"))
)
buttons = self.browser.find_elements_by_class_name('bam-button-primary')
buttons[3].click()
## Claim the mulligan
mulOption = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "mulligan-list"))
)
mulOption.click()
claimMul = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'claim-mulligan-btn'))
)
claimMul.click()
# give it some time, then quit the browser and if necessary, stop the display
time.sleep(10)
self.quit_browser()
except:
try:
self.quit_browser()
raise
except:
raise
def has_claimed_mulligan(self):
"""
Returns True if the account with self.username and self.password
has claimed its mulligan, false otherwise
"""
## navigate to claim mulligan page
self._get_make_picks_page()
more = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "last"))
)
more.click()
button1 = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "bam-button-primary"))
)
buttons = self.browser.find_elements_by_class_name('bam-button-primary')
buttons[3].click()
## Check if we've claimed the mulligan
mulOption = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "mulligan-list"))
)
mulOption.click()
try:
claimMul = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'claim-mulligan-btn')))
except TimeoutException:
return True
return False
def get_username(self):
return self.username
def get_password(self):
return self.password
def _get_login_page(self):
"""
Opens a web broswer and navigates to the beat the streak login page
"""
url = 'https://secure.mlb.com/enterworkflow.do?flowId=fantasy.bts.' + \
'btsloginregister&forwardUrl=http://mlb.mlb.com/mlb/fantasy/' + \
'bts/y2014/'
self.browser.get(url)
time.sleep(3)
assert self.browser.title == self.pageTitles['login']
def _get_make_picks_page(self):
"""
Logs in to mlb beat the streak site
"""
## Are we already on it?
if self.pageTitles['picks'] in self.browser.title:
return
## Else navigate to the login page
if not self.pageTitles['login'] in self.browser.title:
self._get_login_page()
## If we were already logged in, then now we are on the picks page
if self.pageTitles['picks'] in self.browser.title:
return
# Otherwise, we need to login
login = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.ID, 'login_email')))
login.send_keys(self.username)
pword = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.ID, 'login_password')))
pword.send_keys(self.password)
submit = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.NAME, 'submit')))
submit.click()
time.sleep(3)
assert self.browser.title == self.pageTitles['picks']
def _reset_selections(self):
"""
If this bot has already made some selections today, removes
the selections
"""
# make the remove Buttons show up
self._get_player_selection_dropdown()
# if it's the empty Bot, then return
if len(self._get_chosen_players()) == 0:
return
## Use a webdriverWait so we throw an exception if shit isnt there
firstRemoveButton = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'remove-action')))
## Get date's row
dateRow = self.__get_date_make_pick_row()
removeButtonsRaw = dateRow.find_elements_by_class_name('remove-action')
removeButtons = [elem for elem in removeButtonsRaw
if elem.get_attribute('class') == 'remove-action player-row']
while removeButtons != []:
removeButtons[0].click()
time.sleep(3)
subAndCancel = self.browser.find_elements_by_class_name('ui-button-text-only')
subAndCancel[0].click()
time.sleep(3)
## get the remaining remove Buttons
# need to refresh todayRow because it might have moved off DOM
dateRow = self.__get_date_make_pick_row()
removeButtonsRaw = dateRow.find_elements_by_class_name('remove-action')
removeButtons = [ elem for elem in removeButtonsRaw
if elem.get_attribute('class') == 'remove-action player-row']
time.sleep(3)
def quit_browser(self):
"""
Closes self.browser
"""
self.browser.quit()
if self.display:
print "--> Closing Bot display"
self.display.stop()
def _get_chosen_players(self):
"""
None -> Tuple
Returns a tuple of strings containing the names
of players this bot currently has selected
"""
# Get today's make pick row
todayRow = self.__get_date_make_pick_row()
# Find the players selected for today
# html elements that correspond to players are the first item
# in a list. Other html elements on the make pick page that
# are first items in a list have text that either says
# 'date locked', 'double down', or 'make pick'
firstItems = todayRow.find_elements_by_class_name('first')
players = [ elem.text for elem in firstItems if
elem.tag_name == 'li' and elem.text not in ('Date Locked',
'Double Down', 'Make Pick')]
return tuple(players)