示例#1
0
 def load_teams(cls, league_id):
     """
     Load teams from yahoo into db
     """
     print 'Loading teams...'
     query_str = "select * from fantasysports.teams where league_key='%s'" % league_id
     teams = QueryManager.decode_query(cls.query_manager.run_yql_query(query_str, retry=False)).get('query').get('results').get('team')
     query_str = "select standings.teams.team.team_key, standings.teams.team.team_standings from fantasysports.leagues.standings where league_key='%s'" % (league_id)
     results = QueryManager.decode_query(cls.query_manager.run_yql_query(query_str, retry=False)).get('query').get('results').get('league')
     standings = { 
             item.get('standings').get('teams').get('team').get('team_key'):
             item.get('standings').get('teams').get('team').get('team_standings')
             for item in results
             }
     league = League.objects.get(league_key=league_id)
     for raw_team in teams:
         team_key = raw_team.get('team_key')
         team, created = cls.objects.get_or_create(team_key=team_key, defaults={'league': league})
         if not created:
             print 'Team key already exists, reloading team data %s' % team_key
         else:
             print 'Creating team %s' % team_key
         team.team_key = team_key
         name = ''
         managers = raw_team.get('managers').get('manager')
         if isinstance(managers, list):
             join_str = ' + '
             name = join_str.join([ item.get('nickname') for item in managers ])
         else:
             name = managers.get('nickname')
         team.name = raw_team.get('name')
         team.manager_name = name
         team.league = league
         outcome_totals = standings.get(team_key).get('outcome_totals')
         team.wins   = int(outcome_totals.get('wins'))
         team.losses = int(outcome_totals.get('losses'))
         team.ties   = int(outcome_totals.get('ties'))
         team.rank   = int(standings.get(team_key).get('rank'))
         team.save()
示例#2
0
 def load_league(cls, league_id, **kwargs):
     """
     Load league data from yahoo into db
     """
     league, created = cls.objects.get_or_create(league_key=league_id)
     if not created:
         print 'League key already exists, reloading league data %s' % league_id
     else:
         print 'Creating league %s' % league_id
     query_str = "select * from fantasysports.leagues where league_key='%s'" % league_id
     results = QueryManager.decode_query(cls.query_manager.run_yql_query(query_str)).get('query').get('results').get('league')
     league.league_key = results.get('league_key')
     league.name = results.get('name')
     league.scoring_type = results.get('scoring_type')
     league.start_week = int(results.get('start_week'))
     league.end_week = int(results.get('end_week'))
     league.current_week = int(results.get('current_week'))
     league.save()
示例#3
0
 def get_player(cls, player_id, **kwargs):
     """
     Proxy to get_or_create that will handle loading in player data if
     creation is needed
     """
     player, created = cls.objects.get_or_create(player_key=player_id)
     if created:
         try:
             query_str = "select * from fantasysports.players where player_key='%s'" % player_id
             results = QueryManager.decode_query(cls.query_manager.run_yql_query(query_str, retry=False)).get('query').get('results').get('player')
             player._raw_info = results
             player.load_db()
             player.save()
             print "%s created" % player_id
         except KeyboardInterrupt:
             player.delete()
             raise KeyboardInterrupt
     return player
示例#4
0
    def find_all(cls, **kwargs):
        """
        Since Yahoo is a bitch, go scrub the db and find which player ids
        actually exist
        """

        start = kwargs.get('start', 0)
        end = kwargs.get('end', 26000)
        filename = kwargs.get('filename', 'player.log')
        game = kwargs.get('game', 'nfl')
        with open(filename, 'w') as output:
            for player_id in range(start, end):
                if player_id < 10000:
                    player_id = '%s.p.%04d' % (game, player_id)
                else:
                    player_id = '%s.p.%d' % (game, player_id)
                query_str = "select * from fantasysports.players where player_key='%s'" % player_id
                results = QueryManager.decode_query(cls.query_manager.run_yql_query(query_str, retry=False)).get('query').get('results')
                if results:
                    results = results.get('player')
                    print '%s: True' % player_id
                    new_player = Player()
                    new_player._raw_info = results
                    if new_player.eligible_player(**kwargs) or kwargs.get('eligible', False):
                        output.write('%s\n' % player_id)
                        cls.store_raw_info(player_id, results, **kwargs)
                        if not cls.objects.filter(player_key=player_id).exists():
                            new_player.load_db()
                            new_player.save()
                        else:
                            print "Player %s exists in database" % player_id
                    else:
                        print "Player %s ineligible for league" % player_id
                else:
                    print '%s: False' % player_id
                output.flush()
示例#5
0
            'force': bool(args.force),
            }

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fantasy.settings")
    from analyze.models.player import Player
    from analyze.models.league import League, NFLMatchup
    from analyze.models.team import Team, Roster

    if args.find:
        Player.query_manager.set_sleep(int(args.sleep))
        kwargs['eligible'] = args.eligible
        Player.find_all(**kwargs)
    elif args.raw:
        import re
        id_reg = re.compile('(\d+)$')
        query = QueryManager(sleep=args.sleep)
        with open(args.filename, 'r') as source:
            player_list = []
            for player_id in source:
                player_id = player_id.strip()
                if (int(args.start) <= int(id_reg.search(player_id).group(0)) and
                        int(args.end) >= int(id_reg.search(player_id).group(0))):
                    player_list.append(player_id)
            query_str = "select * from fantasysports.players where player_key in (value)"
            for result in query.batch_query(query_str, player_list):
                player_id = result.get('player_key')
                print player_id
                Player.store_raw_info(player_id, result, **kwargs)
    elif args.eligible:
        p = Player()
        with open(args.filename, 'r') as fread: