-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTime.py
787 lines (704 loc) · 26.6 KB
/
GameTime.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
# -*- coding: cp1252 -*-
## --------------------------------------------------------------------------------------
## Import Modules
## --------------------------------------------------------------------------------------
from datetime import datetime
import time
import os
from bs4 import BeautifulSoup
import urllib2
## http://home.wlu.edu/~lambertk/breezypythongui/
debug = 0
## ----------------------------------------------------------------------------
## Global Constants
## ----------------------------------------------------------------------------
CheckBaseURL = "https://dl.dropboxusercontent.com/u/5739973/NHL/Builds/"
CheckVerURL = "checkverpy.txt"
TeamLineUpBaseURL = "http://www2.dailyfaceoff.com/teams/lines/"
TeamStatCount = 25
TeamStats = ["Rank","GP","W","L","OT","P","ROW","HROW","RROW","P%","G/G",
"GA/G","5-5 F/A","PP%","PK%","S/G","SA/G","Sc 1%","Tr 1st%","Ld 1%",
"Ld 2%","OS%","OSB%","FO%"]
PlayerStatCount = 14
PlayerStats = ["Num", "Pos", "Name", "GP", "G", "A", "P", "+/-", "PIM",
"PP", "SH", "GW", "S", "S%"]
GoalieStatCount = 16
GoalieStats = ["Num", "Name", "GPI", "GS", "Min", "GAA", "W", "L", "OT", "SO",
"SA", "GA", "SV%", "G", "A", "PIM"]
SpecialFields = ["nameabbr", "redditname", "redditicon", "redditabbr"]
TableHeader = [":--:", ":--:", ":--:", ":--:", ":--:", ":--:", ":--:", ":--:",
":--:", ":--:", ":--:", ":--:", ":--:", ":--:", ":--:", ":--:", ":--:",
":--:", ":--:", ":--:", ":--:", ":--:", ":--:"]
InjuryURL = "http://www.tsn.ca/nhl/injuries/"
InjuryFields = [ "Player", "Date", "Status", "Injury" ]
team_dicts = {
'Avalanche': {
'Abbr': 'COL',
'Arena': 'Pepsi Center',
'ArenaPlace': 'Denver, CO, USA',
'City': u'Colorado',
'LineupURL': "20/colorado-avalanche",
'Long': 'Colorado Avalanche',
'NHL': 'Colorado',
'Name': 'Avalanche',
'Subreddit': '/r/coloradoavalanche',
'TSN': 'Colorado',
'TimeZone': 'MT'},
'Blackhawks': {
'Abbr': 'CHI',
'Arena': 'United Center',
'ArenaPlace': 'Chicago, IL, USA',
'City': u'Chicago',
'LineupURL': "19/chicago-blackhawks",
'Long': 'Chicago Blackhawks',
'NHL': 'Chicago',
'Name': 'Blackhawks',
'Subreddit': '/r/hawks',
'TSN': 'Chicago',
'TimeZone': 'CT'},
'BlueJackets': {
'Abbr': 'CBJ',
'Arena': 'Nationwide Arena',
'ArenaPlace': 'Columbus, OH, USA',
'City': u'Columbus',
'LineupURL': "21/columbus-blue-jackets",
'Long': 'Columbus Blue Jackets',
'NHL': 'Columbus',
'Name': 'BlueJackets',
'Subreddit': '/r/bluejackets',
'TSN': 'Columbus',
'TimeZone': 'ET'},
'Blues': {
'Abbr': 'STL',
'Arena': 'Scottrade Center',
'ArenaPlace': 'St Louis, MO, USA',
'City': u'St. Louis',
'LineupURL' : "38/st-louis-blues",
'Long': 'St. Louis Blues',
'NHL': 'St Louis',
'Name': 'Blues',
'Subreddit': '/r/stlouisblues',
'TSN': 'St. Louis',
'TimeZone': 'CT'},
'Bruins': {
'Abbr': 'BOS',
'Arena': 'TD Garden',
'ArenaPlace': 'Boston, MA, USA',
'City': u'Boston',
'LineupURL': "15/boston-bruins/",
'Long': 'Boston Bruins',
'NHL': 'Boston',
'Name': 'Bruins',
'Subreddit': '/r/bostonbruins',
'TSN': 'Boston',
'TimeZone': 'ET'},
'Canadiens': {
'Abbr': 'MTL',
'Arena': 'Bell Centre',
'ArenaPlace': 'Montreal, QC, CAN',
'City': u'Montr\xc3\xa9al',
'LineupURL': "28/montreal-canadiens",
'Long': 'Montreal Canadiens',
'NHL': 'Montreal',
'Name': 'Canadiens',
'Subreddit': '/r/habs',
'TSN': 'Montreal',
'TimeZone': 'ET'},
'Canucks': {
'Abbr': 'VAN',
'Arena': 'Rogers Arena',
'ArenaPlace': 'Vancouver, BC, CAN',
'City': u'Vancouver',
'LineupURL': "41/vancouver-canucks",
'Long': 'Vancouver Canucks',
'NHL': 'Vancouver',
'Name': 'Canucks',
'Subreddit': '/r/canucks',
'TSN': 'Vancouver',
'TimeZone': 'PT'},
'Capitals': {
'Abbr': 'WSH',
'Arena': 'Verizon Center',
'ArenaPlace': 'Washington, DC, USA',
'City': u'Washington',
'LineupURL': "42/washington-capitals",
'Long': 'Washington Capitals',
'NHL': 'Washington',
'Name': 'Capitals',
'Subreddit': '/r/caps',
'TSN': 'Washington',
'TimeZone': 'ET'},
'Coyotes': {
'Abbr': 'ARI',
'Arena': 'Gila River Arena',
'ArenaPlace': 'Glendale, AZ, USA',
'City': u'Arizona',
'LineupURL': "35/arizona-coyotes",
'Long': 'Arizona Coyotes',
'NHL': 'Arizona',
'Name': 'Coyotes',
'Subreddit': '/r/coyotes',
'TSN': 'Arizona',
'TimeZone': 'MT'},
'Devils': {
'Abbr': 'NJD',
'Arena': 'Prudential Center',
'ArenaPlace': 'Newark, NJ, USA',
'City': u'New Jersey',
'LineupURL' : "30/new-jersey-devils",
'Long': 'New Jersey Devils',
'NHL': 'New Jersey',
'Name': 'Devils',
'Subreddit': '/r/devils',
'TSN': 'New Jersey',
'TimeZone': 'ET'},
'Ducks': {
'Abbr': 'ANA',
'Arena': 'Honda Center',
'ArenaPlace': 'Anaheim, CA, USA',
'City': u'Anaheim',
'LineupURL': "13/anaheim-ducks/",
'Long': 'Anaheim Ducks',
'NHL': 'Anaheim',
'Name': 'Ducks',
'Subreddit': '/r/anaheimducks',
'TSN': 'Anaheim',
'TimeZone': 'PT'},
'Flames': {
'Abbr': 'CGY',
'Arena': 'Scotiabank Saddledome',
'ArenaPlace': 'Calgary, AB, CAN',
'City': u'Calgary',
'LineupURL': "17/calgary-flames",
'Long': 'Calgary Flames',
'NHL': 'Calgary',
'Name': 'Flames',
'Subreddit': '/r/calgaryflames',
'TSN': 'Calgary',
'TimeZone': 'MT'},
'Flyers': {
'Abbr': 'PHI',
'Arena': 'Wells Fargo Center',
'ArenaPlace': 'Philadelphia, PA, USA',
'City': u'Philadelphia',
'LineupURL': "34/philadelphia-flyers",
'Long': 'Philadelphia Flyers',
'NHL': 'Philadelphia',
'Name': 'Flyers',
'Subreddit': '/r/flyers',
'TSN': 'Philadelphia',
'TimeZone': 'ET'},
'Hurricanes': {
'Abbr': 'CAR',
'Arena': 'PNC Arena',
'ArenaPlace': 'Raleigh, NC, USA',
'City': u'Carolina',
'LineupURL': "18/carolina-hurricanes",
'Long': 'Carolina Hurricanes',
'NHL': 'Carolina',
'Name': 'Hurricanes',
'Subreddit': '/r/canes',
'TSN': 'Carolina',
'TimeZone': 'ET'},
'Islanders': {
'Abbr': 'NYI',
'Arena': 'Nassau Veterans Memorial Coliseum',
'ArenaPlace': 'Long Island, NY, USA',
'City': u'NY Islanders',
'LineupURL': "31/new-york-islanders",
'Long': 'New York Islanders',
'NHL': 'NY Islanders',
'Name': 'Islanders',
'Subreddit': '/r/newyorkislanders',
'TSN': 'NY Islanders',
'TimeZone': 'ET'},
'Jets': {
'Abbr': 'WPG',
'Arena': 'MTS Centre',
'ArenaPlace': 'Winnipeg, MB, CAN',
'City': u'Winnipeg',
'LineupURL': "14/winnipeg-jets",
'Long': 'Winnipeg Jets',
'NHL': 'Winnipeg',
'Name': 'Jets',
'Subreddit': '/r/winnipegjets',
'TSN': 'Winnipeg',
'TimeZone': 'CT'},
'Kings': {
'Abbr': 'LAK',
'Arena': 'Staples Center',
'ArenaPlace': 'Los Angeles, CA, USA',
'City': u'Los Angeles',
'LineupURL': "26/los-angeles-kings",
'Long': 'Los Angeles Kings',
'NHL': 'Los Angeles',
'Name': 'Kings',
'Subreddit': '/r/losangeleskings',
'TSN': 'Los Angeles',
'TimeZone': 'PT'},
'Lightning': {
'Abbr': 'TBL',
'Arena': 'Amalie Arena',
'ArenaPlace': 'Tampa, FL, USA',
'City': u'Tampa Bay',
'LineupURL': "39/tampa-bay-lightning",
'Long': 'Tampa Bay Lightning',
'NHL': 'Tampa Bay',
'Name': 'Lightning',
'Subreddit': '/r/tampabaylightning',
'TSN': 'Tampa Bay',
'TimeZone': 'ET'},
'MapleLeafs': {
'Abbr': 'TOR',
'Arena': 'Air Canada Centre',
'ArenaPlace': 'Toronto, ON, CAN',
'City': u'Toronto',
'LineupURL': "40/toronto-maple-leafs",
'Long': 'Toronto Maple Leafs',
'NHL': 'Toronto',
'Name': 'MapleLeafs',
'Subreddit': '/r/leafs',
'TSN': 'Toronto',
'TimeZone': 'ET'},
'Oilers': {
'Abbr': 'EDM',
'Arena': 'Rexall Place',
'ArenaPlace': 'Edmonton, AB, CAN',
'City': u'Edmonton',
'LineupURL': "24/edmonton-oilers",
'Long': 'Edmonton Oilers',
'NHL': 'Edmonton',
'Name': 'Oilers',
'Subreddit': '/r/edmontonoilers',
'TSN': 'Edmonton',
'TimeZone': 'MT'},
'Panthers': {
'Abbr': 'FLA',
'Arena': 'BB&T Center',
'ArenaPlace': 'Sunrise, FL, USA',
'City': u'Florida',
'LineupURL': "25/florida-panthers",
'Long': 'Florida Panthers',
'NHL': 'Florida',
'Name': 'Panthers',
'Subreddit': '/r/floridapanthers',
'TSN': 'Florida',
'TimeZone': 'ET'},
'Penguins': {
'Abbr': 'PIT',
'Arena': 'Consol Energy Center',
'ArenaPlace': 'Pittsburgh, PA, USA',
'City': u'Pittsburgh',
'Long': 'Pittsburgh Penguins',
'LineupURL' : "36/pittsburgh-penguins",
'NHL': 'Pittsburgh',
'Name': 'Penguins',
'Subreddit': '/r/penguins',
'TSN': 'Pittsburgh',
'TimeZone': 'ET'},
'Predators': {
'Abbr': 'NSH',
'Arena': 'Bridgestone Arena',
'ArenaPlace': 'Nashville, TN, USA',
'City': u'Nashville',
'LineupURL': "29/nashville-predators",
'Long': 'Nashville Predators',
'NHL': 'Nashville',
'Name': 'Predators',
'Subreddit': '/r/predators',
'TSN': 'Nashville',
'TimeZone': 'CT'},
'Rangers': {
'Abbr': 'NYR',
'Arena': 'Madison Square Garden',
'ArenaPlace': 'New York, NY, USA',
'City': u'NY Rangers',
'LineupURL': "32/new-york-rangers",
'Long': 'New York Rangers',
'NHL': 'NY Rangers',
'Name': 'Rangers',
'Subreddit': '/r/rangers',
'TSN': 'NY Rangers',
'TimeZone': 'ET'},
'RedWings': {
'Abbr': 'DET',
'Arena': 'Joe Louis Arena',
'ArenaPlace': 'Detroit, MI, USA',
'City': u'Detroit',
'LineupURL' : "23/detroit-red-wings",
'Long': 'Detroit Red Wings',
'NHL': 'Detroit',
'Name': 'RedWings',
'Subreddit': '/r/detroitredwings',
'TSN': 'Detroit',
'TimeZone': 'ET'},
'Sabres': {
'Abbr': 'BUF',
'Arena': 'First Niagara Center',
'ArenaPlace': 'Buffalo, NY, USA',
'City': u'Buffalo',
'LineupURL': "16/buffalo-sabres/",
'Long': 'Buffalo Sabres',
'NHL': 'Buffalo',
'Name': 'Sabres',
'Subreddit': '/r/sabres',
'TSN': 'Buffalo',
'TimeZone': 'ET'},
'Senators': {
'Abbr': 'OTT',
'Arena': 'Canadian Tire Centre',
'ArenaPlace': 'Ottawa, ON, CAN',
'City': u'Ottawa',
'LineupURL': "33/ottawa-senators",
'Long': 'Ottawa Senators',
'NHL': 'Ottawa',
'Name': 'Senators',
'Subreddit': '/r/ottawasenators',
'TSN': 'Ottawa',
'TimeZone': 'ET'},
'Sharks': {
'Abbr': 'SJS',
'Arena': 'SAP Center',
'ArenaPlace': 'San Jose, CA, USA',
'City': u'San Jose',
'LineupURL': "37/san-jose-sharks",
'Long': 'San Jose Sharks',
'NHL': 'San Jose',
'Name': 'Sharks',
'Subreddit': '/r/sanjosesharks',
'TSN': 'San Jose',
'TimeZone': 'PT'},
'Stars': {
'Abbr': 'DAL',
'Arena': 'American Airlines Center',
'ArenaPlace': 'Dallas, TX, USA',
'City': u'Dallas',
'LineupURL': "22/dallas-stars",
'Long': 'Dallas Stars',
'NHL': 'Dallas',
'Name': 'Stars',
'Subreddit': '/r/dallasstars',
'TSN': 'Dallas',
'TimeZone': 'CT'},
'Wild': {
'Abbr': 'MIN',
'Arena': 'Xcel Energy Center',
'ArenaPlace': 'St Paul, MN, USA',
'City': u'Minnesota',
'LineupURL': "27/minnesota-wild",
'Long': 'Minnesota Wild',
'NHL': 'Minnesota',
'Name': 'Wild',
'Subreddit': '/r/wildhockey',
'TSN': 'Minnesota',
'TimeZone': 'CT'}}
## --------------------------------------------------------------------------------------
## Global Variables
## --------------------------------------------------------------------------------------
#Dictionary for holding team schedules [ date, visitor, home, time(et), network/results ]
schedule = {}
team_stats = {}
team_players = {}
team_goalies = {}
team_injuries = {}
today_games = []
GT_Settings = {}
lineups_forwards = {}
lineups_defense = {}
lineups_goalies = {}
## --------------------------------------------------------------------------------------
## Get Team Schedule
## --------------------------------------------------------------------------------------
def get_team_schedule(team_name):
print 'here'
schedule_file = cache_directory + "\\" + team_name + "-schedule.html"
if os.path.exists(schedule_file):
print "Reading cached team file for " + team_name + "...\r"
with open(schedule_file, "r") as cache_file:
page = cache_file.read()
else:
print "Downloading schedule for " + team_name + "...\r"
w = urllib2.urlopen('http://{}.nhl.com/club/schedule.htm'.format(team_name.lower()))
page = w.read()
w.close()
with open(schedule_file, "w") as cache_file:
cache_file.write(page)
soup = BeautifulSoup(page)
for data in soup.find_all('table', 'data'):
if not 'Preseason' in data.find('div', 'moduleHeader').text:
schedule[team_name.lower()] = []
for tr in data.findAll('tr', { 'class': [ 'rwEven', 'rwOdd' ]}):
temp_sched = [td.text.replace('\n','').replace('\t','').replace(u'\xa0','').strip() for td in tr.findAll('td')]
try:
temp_sched[0] = datetime.strptime(temp_sched[0],'%a, %d %b %Y')
except:
temp_sched[0] = datetime.strptime(temp_sched[0],'%a %b %d, %Y')
temp_sched.pop()
schedule[team_name.lower()].append(temp_sched)
## --------------------------------------------------------------------------------------
## Get Team Injuries
## --------------------------------------------------------------------------------------
def get_team_injuries():
if os.path.exists(InjuriesFile):
print "Reading cached injury data...\r"
with open(InjuriesFile, "r") as cache_file:
page = cache_file.read()
else:
print "Downloading injuries data...\r"
w = urllib2.urlopen(InjuryURL)
page = w.read()
w.close()
with open(InjuriesFile, "w") as cache_file:
cache_file.write(page)
soup = BeautifulSoup(page)
data = soup.find('table')
for tr in data.find_all('tr'):
if not "Status" in tr.text:
if tr.find('h3'):
teamname = tr.find('h3').text
print teamname
if not team_injuries.has_key(teamname):
team_injuries[teamname] = {}
elif tr.has_attr('class'):
if 'bg1' in tr.attrs['class'] or 'bg2' in tr.attrs['class']:
player = [td.text.replace(u'\xa0', ' ') for td in tr.find_all(['td', 'th'])]
print player
team_injuries[teamname][player[0]] = [player[x] for x in range(1,4)]
## --------------------------------------------------------------------------------------
## Get Team Injuries
## --------------------------------------------------------------------------------------
def get_team_stats():
if os.path.exists(StandingsFile):
print "Reading cached standings...\r"
with open(StandingsFile, "r") as cache_file:
page = cache_file.read()
else:
print "Downloading league standings...\r"
w = urllib2.urlopen("http://www.nhl.com/ice/teamstats.htm?fetchKey=20142ALLSAAAll&sort=points&viewName=summary")
page = w.read()
w.close()
with open(StandingsFile, "w") as cache_file:
cache_file.write(page)
soup = BeautifulSoup(page)
tables = soup.findAll('tbody')
for tr in tables[1].findAll('tr'):
temp_array = [td.text for td in tr.findAll('td')]
team = temp_array.pop(1)
team_stats[team.lower()] = temp_array
## --------------------------------------------------------------------------------------
## Get Team Players
## --------------------------------------------------------------------------------------
def get_team_players(team_name):
players_file = cache_directory + "\\" + team_name + "-players.html"
if os.path.exists(players_file):
print "Reading cached team file for " + team_name + "...\r"
with open(players_file, "r") as cache_file:
page = cache_file.read()
else:
print "Downloading players for " + team_name + "...\r"
w = urllib2.urlopen('http://{}.nhl.com/club/stats.htm'.format(team_name.lower()))
page = w.read()
w.close()
with open(players_file, "w") as cache_file:
cache_file.write(page)
soup = BeautifulSoup(page)
team_players[team_name.lower()] = []
team_goalies[team_name.lower()] = []
tables = soup.findAll('table', {'class' : 'data'})
for tr in tables[0].findAll('tr', { 'class' : ['rwEven', 'rwOdd']}):
temp_player = [td.text for td in tr.findAll('td')]
team_players[team_name.lower()].append(temp_player)
for tr in tables[1].findAll('tr', { 'class' : ['rwEven', 'rwOdd']}):
temp_goalie = [td.text for td in tr.findAll('td')]
team_goalies[team_name.lower()].append(temp_goalie)
## --------------------------------------------------------------------------------------
## Scrape today's schedule
## --------------------------------------------------------------------------------------
def get_today_schedule():
global today_games
global game_count
game_count = 0
if os.path.exists(ScheduleFile):
print "Reading cached schedule...\r"
with open(ScheduleFile, "r") as cache_file:
page = cache_file.read()
else:
print "Downloading today's schedule...\r"
w = urllib2.urlopen('http://www.nhl.com/ice/schedulebyday.htm?date=' + datetime.now().strftime("%m/%d/%Y"))
page = w.read()
w.close()
with open(ScheduleFile, "w") as cache_file:
cache_file.write(page)
soup = BeautifulSoup(page)
today_games = []
#Check to see if the schedule is for today
contentBlock = soup.find("div", { "class" : "contentBlock"})
h3 = contentBlock.find("h3").text
page_date = time.strptime(''.join(h3.splitlines()), "%A %B %d, %Y")
if page_date == time.strptime(datetime.now().strftime("%m/%d/%Y"), "%m/%d/%Y"):
#The only <tbody> tag in the page is the schedule table
data_table = soup.find('tbody')
for tr in data_table.findAll('tr'):
#Look for upcoming games
if tr.find('div', { "class" : "skedStartTimeLocal" }):
local_time = tr.find('div', { "class" : "skedStartTimeLocal" }).text
lst = [td.text for td in tr]
#Fix Montreal's name
home_team = lst[1].replace(u'\xe9',u'e')
away_team = lst[0].replace(u'\xe9',u'e')
network = lst[3]
# home team, away team, local time, poster, comment object, est datetime
game_count += 1
today_games.append({ "game_num" : game_count,
"home_team" : home_team,
"away_team" : away_team,
"local_time" : local_time,
"network" : network
})
## --------------------------------------------------------------------------------------
## Check team lines
## --------------------------------------------------------------------------------------
def get_team_lines(team_name):
lines_file = cache_directory + "\\" + team_name + "-lines.html"
if os.path.exists(lines_file):
print "Reading cached team file for " + team_name + "...\r"
with open(lines_file, "r") as cache_file:
page = cache_file.read()
else:
print "Downloading players for " + team_name + "...\r"
w = urllib2.urlopen(TeamLineUpBaseURL + team_dicts[team_name]['LineupURL'])
page = w.read()
w.close()
with open(lines_file, "w") as cache_file:
cache_file.write(page)
soup = BeautifulSoup(page)
player_list = soup.find_all('table', { 'class' : 'playerlist' })
forwards = []
defense = []
goalies = []
## Forwards
for tr in player_list[0].find_all('tr'):
forwards.append([td.text.replace('\n','') for td in tr.find_all('td') if len(td)])
for tr in player_list[1].find_all('tr'):
if not tr.find('th'):
defense.append([td.text.replace('\n','') for td in tr.find_all('td') if len(td)])
for tr in player_list[4].find_all('tr'):
if not tr.find('th'):
for td in tr.find_all('td'):
goalies.append(td.text.replace('\n',''))
lineups_forwards[team_name] = forwards
lineups_defense[team_name] = defense
lineups_goalies[team_name] = goalies
## --------------------------------------------------------------------------------------
## Check file dates
## --------------------------------------------------------------------------------------
def check_filedates():
print "Checking cached data...\r"
t1 = time.localtime()
if os.path.exists(ScheduleFile):
t2 = os.path.getmtime(ScheduleFile)
if ((time.mktime(t1) - t2) / 60) > 60:
os.remove(ScheduleFile)
print "Deleting schedule file...\r"
elif debug == 1:
print "Schedule file life in minutes: " + str((time.mktime(t1) - t2) / 60) + "\r"
if os.path.exists(AllPlayersFile):
t2 = os.path.getmtime(AllPlayersFile)
if ((time.mktime(t1) - t2) / 60) > 60:
os.remove(AllPlayersFile)
print "Deleting players file...\r"
elif debug == 1:
print "Players file life in minutes: " + str((time.mktime(t1) - t2) / 60) + "\r"
if os.path.exists(AllGoaliesFile):
t2 = os.path.getmtime(AllGoaliesFile)
if ((time.mktime(t1) - t2) / 60) > 60:
os.remove(AllGoaliesFile)
print "Deleting goalies file...\r"
elif debug == 1:
print "Goalies file life in minutes: " + str((time.mktime(t1) - t2) / 60) + "\r"
if os.path.exists(StandingsFile):
t2 = os.path.getmtime(StandingsFile)
if ((time.mktime(t1) - t2) / 60) > 60:
os.remove(StandingsFile)
print "Deleting standings file...\r"
elif debug == 1:
print "Standings file life in minutes: " + str((time.mktime(t1) - t2) / 60) + "\r"
if os.path.exists(InjuriesFile):
t2 = os.path.getmtime(InjuriesFile)
if ((time.mktime(t1) - t2) / 60) > 60:
os.remove(InjuriesFile)
print "Deleting injuries file..."
elif debug == 1:
print "Injuries file life in minutes: " + str((time.mktime(t1) - t2) / 60) + "\r"
for x in range(30):
file_check = '%s\%s-lines.html' % (cache_directory,TeamNames[x])
if os.path.exists(file_check):
t2 = os.path.getmtime(file_check)
if ((time.mktime(t1) - t2) / 60) > 60:
os.remove(file_check)
print "Deleting %s lines file..." % (TeamNames[x])
elif debug == 1:
print "%s lines file life in minutes: %d" % (TeamNames[x],(time.mktime(t1) - t2) / 60)
## --------------------------------------------------------------------------------------
## List files in a folder
## --------------------------------------------------------------------------------------
def ListFiles(sPath,sExt):
# returns a list of names (with extension, without full path) of all files
# in folder sPath
lsFiles = []
for sName in os.listdir(sPath):
if os.path.isfile(os.path.join(sPath, sName)) and sName[0-(len(sExt)):].lower() == sExt.lower():
lsFiles.append(sName)
return lsFiles
## --------------------------------------------------------------------------------------
## Build Thread File
## --------------------------------------------------------------------------------------
def build_game_thread():
print 'Hi'
## --------------------------------------------------------------------------------------
## Mainline code
## --------------------------------------------------------------------------------------
def build_table():
for team in team_dicts:
print "\r\n|Field|Data|"
print ":--:|:--:"
print "|City|" + team['TeamCities'] + "|"
for k,v in team.iter_items():
print k
if k is not 'TeamCities':
print "|" + k + "|" + v + "|"
print "GameTimePy v1.0.14.1\r"
print "by sentry07 (/r/GameTime)\r\n"
this_directory = os.path.dirname(os.path.abspath(__file__)) + "\\"
cache_directory = this_directory + "Data"
output_directory = this_directory + "Generated"
ScheduleFile = cache_directory + "\schedule.html"
AllPlayersFile = cache_directory + "\AllPlayers.html"
AllGoaliesFile = cache_directory + "\AllGoalies.html"
StandingsFile = cache_directory + "\Standings.html"
InjuriesFile = cache_directory + "\Injuries.html"
if not os.path.exists(cache_directory):
os.makedirs(cache_directory)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# check_filedates()
get_team_stats()
get_today_schedule()
print "\r\n"
print "Select template file to use:"
TemplateFiles = ListFiles(this_directory,'txt')
for idx, _file in enumerate(TemplateFiles):
print "[%d] %s" % (idx, _file)
template_num = int(raw_input("> "))
if template_num <= len(TemplateFiles):
print "Using %s for game thread." % (TemplateFiles[template_num])
if game_count > 0:
print "\r\nToday's games:\r"
for game in today_games:
print "[" + str(game['game_num']) + "] " + game['away_team'] + " @ " + game['home_team'] + " : " + game['local_time'] + "\r"
print "[0] Exit\r"
game_num = int(raw_input("> "))
if game_num <= game_count and game_num > 0:
print "Please wait...\r"
else:
print "\r\nNo games today.\r"