Created
February 21, 2013 13:25
-
-
Save notthatbreezy/5004712 to your computer and use it in GitHub Desktop.
Simple script to see if Andrew Bynum played last night for the Philadelphia 76ers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
from lxml.html import parse, make_links_absolute | |
from StringIO import StringIO | |
import re | |
from datetime import date, timedelta | |
# Helper Function to Scrape Pages # | |
def get_html_tree(url): | |
html = urllib2.urlopen(url).read() | |
abs_links = make_links_absolute(html, url) | |
return parse(StringIO(abs_links)) | |
def find_date(url): | |
"""Finds date in URLs with 8 digit format of YYYYMMDD""" | |
m = re.search(r'(\d{4})(\d{2})(\d{2})', url) | |
return date(int(m.group(1)), int(m.group(2)), int(m.group(3))) | |
def game_yesterday(date): | |
if date < date.today() - timedelta(1): | |
return None | |
else: | |
return 1 | |
def did_bynum_play(boxscore_tree): | |
"""Goes through inactive players looking for Bynum""" | |
bynum_check = None | |
statuses = [player.text_content() for player in boxscore_tree.xpath('///ul[@id="nbaGIPlyrStatus"]')] | |
for status in statuses: | |
if re.search("Bynum", status): | |
bynum_check = 1 | |
continue | |
if bynum_check: | |
print "Bynum did not play!" | |
else: | |
print "Bynum might have played (or this program is broken)." | |
if __name__ == '__main__': | |
# Sixers Homepage (Find most Recent Box Score) # | |
home = 'http://www.nba.com/sixers/' | |
home_tree = get_html_tree(home) | |
# Find most recent box-score # | |
link_tag = home_tree.xpath('///a[@class="boxscore"]')[0].iterlinks() | |
boxscore_link = [elem[2] for elem in link_tag][0] | |
# Check if game was played yesterday # | |
gamedate = find_date(boxscore_link) | |
game_check = game_yesterday(gamedate) | |
if game_check == None: | |
print "No game for Bynum to play in yesterday!" | |
raise SystemExit | |
# Go to last Box Score # | |
boxscore = get_html_tree(boxscore_link) | |
# See if Bynum Played # | |
did_bynum_play(boxscore) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment