def load_schedule(week, force=False): if Game.gql('WHERE week = :1', week).count() > 0 and not force: logging.warning('Schedule for week %s is already loaded', week) x = _load_url('http://football.myfantasyleague.com/2012/export?TYPE=nflSchedule&W=%d' % week, type='xml') if int(x.get('week', 0)) != week: logging.warning('Could not load schedule for week %d', week) return games = [] for game in x.matchup: date = datetime.fromtimestamp(int(game.get('kickoff'))) date = date.replace(tzinfo=timezone.utc).astimezone(timezone.Pacific).replace(tzinfo=None) deadline = datetime(date.year, date.month, date.day, 23, 0) - timedelta(days=1) assert game.team[0].get('isHome') == '0' visiting = teams.id(game.team[0].get('id')) home = teams.id(game.team[1].get('id')) g = Game(week=week, home=home, visiting=visiting, date=date, deadline=deadline) logging.info('Adding game %s vs %s on %s, deadline %s', teams.shortname(visiting), teams.shortname(home), date, deadline) g.put() games.append(g) return games
def load_old_scores(week): x = _load_url('http://football.myfantasyleague.com/2012/export?TYPE=nflSchedule&W=%d' % week, type='xml') scores = {} for game in x.matchup: visiting_name = teams.shortname(teams.id(game.team[0].get('id'))) home_name = teams.shortname(teams.id(game.team[1].get('id'))) visiting_score = int(game.team[0].get('score')) home_score = int(game.team[1].get('score')) logging.debug('Found score for old game %s vs %s', home_name, visiting_name) scores[home_name] = (home_score, visiting_score) return scores
def _extract_team_power_rank(row): rank_elem = row.find_class('pr-rank') if not rank_elem: return (None, None) # extra element at bottom of table for explanation is not a rank rank = int(rank_elem[0].text_content()) team_name = [x[0] for x in row.iterlinks()][1].text_content() return (teams.id(team_name), rank)
def _set_rankings(team): team_id = teams.shortname(teams.id(team.get('id'))) rankings = TeamRanking.get_or_insert(team_id) rankings.rush_defense_rank = int(team.get('rushDefenseRank')) rankings.rush_offense_rank = int(team.get('rushOffenseRank')) rankings.pass_defense_rank = int(team.get('passDefenseRank')) rankings.pass_offense_rank = int(team.get('passOffenseRank')) rankings.put()
def update_spreads(): week = weeks.current() changed_games = [] x = _load_url('http://www.sportsbook.ag/rss/live-nfl-football.rss', type='xml') for game in x.channel.item: team_names = _team_re.search(str(game.title)).group(1, 2) visiting_spread = _find_spread(team_names[0], game.description) home_spread = _find_spread(team_names[1], game.description) g = Game.gql('WHERE week = :1 AND home = :2 AND visiting = :3', week, teams.id(team_names[1]), teams.id(team_names[0])).get() if not g: continue if visiting_spread > 0: g.favorite = g.home elif home_spread > 0: g.favorite = g.visiting g.spread = abs(home_spread) changed_games.append(g) logging.info('%s vs %s, Favorite: %s, spread %f', teams.shortname(g.visiting), teams.shortname(g.home), teams.shortname(g.favorite), g.spread) db.put(changed_games)
def read_static_schedule(): schedule = defaultdict(list) f = open('data/schedule.txt', 'r') week = 0 date = None for line in f: line = line.strip() if line.startswith('WEEK'): week = int(line.split()[1]) elif line.startswith('-'): date = datetime.strptime(line[1:], '%A, %b. %d') elif line: try: data = line.split(',') t = datetime.strptime(data[2].strip(), '%I:%M%p') visiting = teams.id(data[0]) home = teams.id(data[1].strip()) dt = datetime(2012, date.month, date.day, t.hour, t.minute) schedule[week].append((dt, home, visiting)) except: logging.error('problem on line: %s', line) raise return schedule
def get(self): data = {} for t in games.team_rankings(): team_name = str(t.key().name()) data[teams.id(team_name)] = { 'team': team_name, 'wins': t.wins, 'losses': t.losses, 'power_rank': t.power_rank, 'rush_defense': t.rush_defense_rank, 'rush_offense': t.rush_offense_rank, 'pass_defense': t.pass_defense_rank, 'pass_offense': t.pass_offense_rank, } view.render_json(self, data)