def get_roto_rss():
    """same idea as yahoo rss feed"""
    rss_parser = RSS_Parser()
    db_helper = Db_Helper()
    roto_rss_dict = rss_parser.return_roto_rss()
    db_helper.load_roto_rss_dict(roto_rss_dict)
    roto_rss_feed = Roto_RSS_Data.query.order_by(desc(Roto_RSS_Data.timestamp)).all()
    return render_template('rotoworld_rss_feed.html', rss_feed=roto_rss_feed)
def get_roto_player():
    """used for player updates"""
    rss_parser = RSS_Parser()
    db_helper = Db_Helper()
    roto_player_dict = rss_parser.return_roto_players()
    db_helper.load_roto_players_dict(roto_player_dict)
    roto_player_feed = Roto_RSS_Player_Data.query.order_by(desc(Roto_RSS_Player_Data.timestamp)).all()
    return render_template('rotoworld_players_feed.html', rss_feed=roto_player_feed)
Beispiel #3
0
def get_roto_rss():
    """same idea as yahoo rss feed"""
    rss_parser = RSS_Parser()
    db_helper = Db_Helper()
    roto_rss_dict = rss_parser.return_roto_rss()
    db_helper.load_roto_rss_dict(roto_rss_dict)
    roto_rss_feed = Roto_RSS_Data.query.order_by(desc(
        Roto_RSS_Data.timestamp)).all()
    return render_template('rotoworld_rss_feed.html', rss_feed=roto_rss_feed)
Beispiel #4
0
def get_roto_player():
    """used for player updates"""
    rss_parser = RSS_Parser()
    db_helper = Db_Helper()
    roto_player_dict = rss_parser.return_roto_players()
    db_helper.load_roto_players_dict(roto_player_dict)
    roto_player_feed = Roto_RSS_Player_Data.query.order_by(
        desc(Roto_RSS_Player_Data.timestamp)).all()
    return render_template('rotoworld_players_feed.html',
                           rss_feed=roto_player_feed)
 def __init__(self):
     self.token_1, self.token_2 = None, None
     self.yahoo_league_key = None
     self.yahoo_team_key = None
     self.yahoo_player_key = None
     self.data = None
     self.resp = None
     self.params = {}
     self.db_helper = Db_Helper()
     self.current_week = self.db_helper.return_current_week()
     self.api_call_count = 0
     pass
def get_yahoo_rss():
    """this test function parses RSS data from yahoo nfl rss and then
    sends the data to yahoo_db_helper where it checks if the current article
    is already in the database. if's already in the database, it does nothing.
    if it doesn't exist, it adds content to database.
    then, we make a db query to pull latest rss feeds
    """
    rss_parser = RSS_Parser()
    db_helper = Db_Helper()
    yahoo_rss_dict = rss_parser.return_yahoo_rss()
    db_helper.load_yahoo_rss_dict(yahoo_rss_dict)
    yahoo_rss_feed = Yahoo_RSS_Data.query.order_by(desc(Yahoo_RSS_Data.timestamp)).all()
    
    return render_template('yahoo_rss_feed.html', rss_feed=yahoo_rss_feed)
Beispiel #7
0
def new_league_info(game_key, yahoo_league_id):
    """parameters necessary for parsing a specific league should be 
    passed in. then API calls are executed. this function should
    populate database with scores for player, scores for team, and 
    all teams in ONE league.    
    """
    full_league_id = str(game_key) + '.l.' + str(yahoo_league_id)
    # assumption is that user is already yahoo-authenticated
    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    db_helper = Db_Helper()

    yahoo_caller.load_oauth_tokens(session['yahoo_token'][0],
                                   session['yahoo_token'][1])
    yahoo_caller.load_yahoo_league_key(full_league_id)
    resp, content = yahoo_caller.get_team_info()
    yahoo_parser.import_json_data(content, get='teams')
    # right now, the db insertion is done inside yahoo_parser
    team_data = yahoo_parser.return_data()
    num_teams = db_helper.return_num_teams(yahoo_league_id)
    # the fantasy league
    f_league = F_League.query.filter_by(
        user_id=session['user_id'],
        game_key=game_key,
        yahoo_league_id=yahoo_league_id).first()

    if f_league is None:
        raise Exception('error, somehow this league doesnt exist')
    week_list = f_league.get_weeks()
    # get all players in league
    for week in week_list:
        entire_league_players = yahoo_caller.get_all_players_data(
            week, full_league_id, num_teams)
        for players_per_week in entire_league_players:
            yahoo_parser.import_json_data(players_per_week, get='players_v2')
            processed_data = yahoo_parser.return_data()
            db_helper.brians_import_players_data_v2(processed_data)
    # get all team stats
    for week in week_list:
        resp, content = yahoo_caller.get_team_stats(week)
        yahoo_parser.import_json_data(content, get='team_stats')
        processed_data = yahoo_parser.return_data()
        db_helper.brians_import_team_stats(processed_data)
    # get individual player stats for every week
    for week in week_list:
        scores_for_player = yahoo_caller.get_weekly_stats_for_all_players_in_league(
            week, full_league_id, num_teams)
        for player_pts_for_week in scores_for_player:
            yahoo_parser.import_json_data(player_pts_for_week,
                                          get='weekly_stats')
            parsed_data = yahoo_parser.return_data()
            db_helper.import_player_stats(parsed_data)

    return render_template('load_league_success.html')
Beispiel #8
0
def get_yahoo_rss():
    """this test function parses RSS data from yahoo nfl rss and then
    sends the data to yahoo_db_helper where it checks if the current article
    is already in the database. if's already in the database, it does nothing.
    if it doesn't exist, it adds content to database.
    then, we make a db query to pull latest rss feeds
    """
    rss_parser = RSS_Parser()
    db_helper = Db_Helper()
    yahoo_rss_dict = rss_parser.return_yahoo_rss()
    db_helper.load_yahoo_rss_dict(yahoo_rss_dict)
    yahoo_rss_feed = Yahoo_RSS_Data.query.order_by(
        desc(Yahoo_RSS_Data.timestamp)).all()

    return render_template('yahoo_rss_feed.html', rss_feed=yahoo_rss_feed)
def new_league_info(game_key, yahoo_league_id):
    """parameters necessary for parsing a specific league should be 
    passed in. then API calls are executed. this function should
    populate database with scores for player, scores for team, and 
    all teams in ONE league.    
    """
    full_league_id = str(game_key) + '.l.' + str(yahoo_league_id)
    # assumption is that user is already yahoo-authenticated
    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    db_helper = Db_Helper()
    
    yahoo_caller.load_oauth_tokens(session['yahoo_token'][0], session['yahoo_token'][1])
    yahoo_caller.load_yahoo_league_key(full_league_id)
    resp, content = yahoo_caller.get_team_info()
    yahoo_parser.import_json_data(content, get='teams')
    # right now, the db insertion is done inside yahoo_parser
    team_data = yahoo_parser.return_data()
    num_teams = db_helper.return_num_teams(yahoo_league_id)
    # the fantasy league
    f_league = F_League.query.filter_by(
            user_id = session['user_id'],
            game_key = game_key,
            yahoo_league_id = yahoo_league_id).first()

    if f_league is None:
        raise Exception('error, somehow this league doesnt exist')
    week_list = f_league.get_weeks()
    # get all players in league
    for week in week_list:
        entire_league_players = yahoo_caller.get_all_players_data(week, full_league_id, num_teams)
        for players_per_week in entire_league_players:
            yahoo_parser.import_json_data(players_per_week, get='players_v2')
            processed_data = yahoo_parser.return_data()
            db_helper.brians_import_players_data_v2(processed_data)
    # get all team stats
    for week in week_list:
        resp, content = yahoo_caller.get_team_stats(week)
        yahoo_parser.import_json_data(content, get='team_stats')
        processed_data = yahoo_parser.return_data()              
        db_helper.brians_import_team_stats(processed_data)
    # get individual player stats for every week
    for week in week_list:
        scores_for_player = yahoo_caller.get_weekly_stats_for_all_players_in_league(
                week, full_league_id, num_teams)
        for player_pts_for_week in scores_for_player:
            yahoo_parser.import_json_data(player_pts_for_week, get='weekly_stats')
            parsed_data = yahoo_parser.return_data()
            db_helper.import_player_stats(parsed_data)

    return render_template('load_league_success.html')
Beispiel #10
0
def get_stats():
    """get weekly fantasy scores by team"""
    yahoo_helper = YahooHelper()
    yahoo_api_caller = YahooAPICaller()
    yahoo_db_helper = Db_Helper()

    if not 'yahoo_token' in session.keys():
        return redirect(
            url_for('yahoo_oauth', next=url_for('get_stats') or None))

    ### !!!!!!!
    league_key = 'nfl.l.1173612'
    ### there should be a call to the database for league key here.

    token_1, token_2 = session['yahoo_token'][0], session['yahoo_token'][1]
    #yahoo_api_caller.load_keys(token_1=token_1, token_2=token_2)

    params = {'week': '9'}
    ## for current week
    resp, content = get_yahoo_api_data('league/' + league_key + '/scoreboard',
                                       session['yahoo_token'][0],
                                       session['yahoo_token'][1],
                                       extras=params)
    if resp.status == 401:
        return redirect(
            url_for('yahoo_oauth', next=url_for('get_stats') or None))

    yahoo_helper.import_json_data(content, get='team_stats')
    processed_data = yahoo_helper.return_data()  #<== stats_dict returned here
    yahoo_db_helper.brians_import_team_stats(
        processed_data)  #<== stats_dict being parsed

    #
    # SHOULD MAKE DB CALLS HERE TO PULL DATA NOW.
    #

    return render_template('yahoo.html',
                           what_data='stats',
                           resp=resp,
                           processed_data=processed_data,
                           content=content)
 def __init__(self):
     self.token_1, self.token_2 = None, None
     self.yahoo_league_key = None
     self.yahoo_team_key = None
     self.yahoo_player_key = None
     self.data = None
     self.resp = None
     self.params = {}
     self.db_helper = Db_Helper()
     self.current_week = self.db_helper.return_current_week()
     self.api_call_count = 0
     pass
def yahoo_get_team():
    '''get own fantasy team roster'''
    if not 'yahoo_token' in session.keys():
        return redirect(url_for('yahoo_oauth', next=url_for('yahoo_get_team') or None))
    
    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    yahoo_db_helper = Db_Helper()
    league_id = yahoo_db_helper.return_league_id(session['user_id'])

    league_key = '175.l.' + str(league_id)
    #params = { 'use_login':'******', 'game_key':'nfl' }

    resp, content = get_yahoo_api_data('league/' + league_key + '/teams',
            session['yahoo_token'][0], session['yahoo_token'][1], extras=params)
    if resp.status == 401:
        return redirect(url_for('yahoo_oauth', next=url_for('yahoo_get_team') or None))
    
    processed_data = content
    data = ""
    return render_template('yahoo.html',
            what_data = 'test',
            resp = resp,
            processed_data = processed_data,
            data = data
            )

    # CALL YAHOO API HELPER 
    # done but won't be able to display on the page the way im doing it. Compile yahoo_api_helper file on its own to debug.
    
    #yahoo_helper=YahooHelper()
    #yahoo_helper.import_json_data(content, get="teams")
    #yahoo_helper.return_data()
    
    # ['fantasy_content']['league'][1]['teams'][TEAM NUM]
    # to get data for all teams, need to get list of teams, then loop over [TEAM NUM] element
    


    '''
Beispiel #13
0
def yahoo_get_team():
    '''get own fantasy team roster'''
    if not 'yahoo_token' in session.keys():
        return redirect(
            url_for('yahoo_oauth', next=url_for('yahoo_get_team') or None))

    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    yahoo_db_helper = Db_Helper()
    league_id = yahoo_db_helper.return_league_id(session['user_id'])

    league_key = '175.l.' + str(league_id)
    #params = { 'use_login':'******', 'game_key':'nfl' }

    resp, content = get_yahoo_api_data('league/' + league_key + '/teams',
                                       session['yahoo_token'][0],
                                       session['yahoo_token'][1],
                                       extras=params)
    if resp.status == 401:
        return redirect(
            url_for('yahoo_oauth', next=url_for('yahoo_get_team') or None))

    processed_data = content
    data = ""
    return render_template('yahoo.html',
                           what_data='test',
                           resp=resp,
                           processed_data=processed_data,
                           data=data)

    # CALL YAHOO API HELPER
    # done but won't be able to display on the page the way im doing it. Compile yahoo_api_helper file on its own to debug.

    #yahoo_helper=YahooHelper()
    #yahoo_helper.import_json_data(content, get="teams")
    #yahoo_helper.return_data()

    # ['fantasy_content']['league'][1]['teams'][TEAM NUM]
    # to get data for all teams, need to get list of teams, then loop over [TEAM NUM] element
    '''
Beispiel #14
0
def get_player_stats():
    yahoo_helper = YahooHelper()
    yahoo_api_caller = YahooAPICaller()

    if not 'yahoo_token' in session.keys():
        return redirect(
            url_for('yahoo_oauth', next=url_for('get_player_stats') or None))

    team_key = '314.l.1173612.t.8'

    token_1, token_2 = session['yahoo_token'][0], session['yahoo_token'][1]

    # load keys to yahoo_api_caller
    yahoo_api_caller.load_oauth_tokens(token_1, token_2)
    # load the team_key parameter
    yahoo_api_caller.load_yahoo_team_key(team_key)
    resp, content = yahoo_api_caller.get_weekly_stats_for_player()

    if resp.status == 401:
        return redirect(
            url_for('yahoo_oauth', next=url_for('get_player_stats') or None))

    yahoo_helper.import_json_data(content, get='weekly_stats')
    processed_content = yahoo_helper.return_data()
    yahoo_db_helper = Db_Helper()
    yahoo_db_helper.import_player_stats(processed_content)

    #resp, content = get_yahoo_api_data('team/' + team_key + '/roster/players/stats',
    #        session['yahoo_token'][0], session['yahoo_token'][1], extras=params)

    #if resp.status == 401:
    #    return redirect(url_for('yahoo_oauth', next=url_for('get_player_stats') or None))

    return render_template('yahoo.html',
                           what_data='player_stats',
                           resp=resp,
                           processed_content=processed_content,
                           content=content)
def get_player_stats():
    yahoo_helper = YahooHelper()
    yahoo_api_caller = YahooAPICaller()
    
    if not 'yahoo_token' in session.keys():
        return redirect(url_for('yahoo_oauth', next=url_for('get_player_stats') or None))
    
    team_key ='314.l.1173612.t.8'
    
    token_1, token_2 = session['yahoo_token'][0], session['yahoo_token'][1]
    
    # load keys to yahoo_api_caller
    yahoo_api_caller.load_oauth_tokens(token_1, token_2)
    # load the team_key parameter
    yahoo_api_caller.load_yahoo_team_key(team_key)
    resp, content = yahoo_api_caller.get_weekly_stats_for_player()
    
    if resp.status == 401:
        return redirect(url_for('yahoo_oauth', next=url_for('get_player_stats') or None))


    yahoo_helper.import_json_data(content, get='weekly_stats')
    processed_content = yahoo_helper.return_data()
    yahoo_db_helper=Db_Helper()
    yahoo_db_helper.import_player_stats(processed_content)

    #resp, content = get_yahoo_api_data('team/' + team_key + '/roster/players/stats',
    #        session['yahoo_token'][0], session['yahoo_token'][1], extras=params)

    #if resp.status == 401:
    #    return redirect(url_for('yahoo_oauth', next=url_for('get_player_stats') or None))
    
    return render_template('yahoo.html',
            what_data = 'player_stats',
            resp = resp,
            processed_content = processed_content,
            content = content
            )
def new_user_info():
    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    yahoo_db_helper = Db_Helper()

    if not 'yahoo_token' in session.keys():
        return redirect(url_for('yahoo_oauth', next=url_for('new_user_info') or None))
    yahoo_caller.load_oauth_tokens(session['yahoo_token'][0], session['yahoo_token'][1])
    
    resp, content = yahoo_caller.get_user_info()
    yahoo_parser.import_json_data(content, get='user')
    user_guid = yahoo_parser.return_data()
    yahoo_parser.import_json_data(content, get='user_v2')
    list_of_user_dicts = yahoo_parser.return_data()
    
    yahoo_db_helper.brians_import_user_info(
            user_id = session['user_id'],
            user_guid = user_guid)
    # list of n-fantasy_leagues here
    yahoo_db_helper.brians_import_user_info_v2(
            user_id = session['user_id'],
            list_of_user_dicts = list_of_user_dicts)
    return render_template('load_user_success.html')
def get_stats():
    """get weekly fantasy scores by team"""
    yahoo_helper = YahooHelper()
    yahoo_api_caller = YahooAPICaller()
    yahoo_db_helper = Db_Helper()
    
    if not 'yahoo_token' in session.keys():
        return redirect(url_for('yahoo_oauth', next=url_for('get_stats') or None))
    
    ### !!!!!!!
    league_key = 'nfl.l.1173612'
    ### there should be a call to the database for league key here.

    token_1, token_2 = session['yahoo_token'][0], session['yahoo_token'][1]
    #yahoo_api_caller.load_keys(token_1=token_1, token_2=token_2)
    
    params = {'week':'9' }
    ## for current week
    resp, content = get_yahoo_api_data('league/' + league_key + '/scoreboard',
            session['yahoo_token'][0], session['yahoo_token'][1], extras=params)
    if resp.status == 401:
        return redirect(url_for('yahoo_oauth', next=url_for('get_stats') or None))

    yahoo_helper.import_json_data(content, get='team_stats')
    processed_data = yahoo_helper.return_data()       #<== stats_dict returned here
    yahoo_db_helper.brians_import_team_stats(processed_data) #<== stats_dict being parsed

    #
    # SHOULD MAKE DB CALLS HERE TO PULL DATA NOW.
    #

    return render_template('yahoo.html',
            what_data = 'stats',
            resp = resp,
            processed_data = processed_data,
            content = content
            )
Beispiel #18
0
def get_update_status():
    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    db_helper = Db_Helper()
    #status = db_helper.get_player_status_updates(user_id = 1)
    #nfl_player_id_list = list()
    status = NFL_Player_Status_Update.query.all()

    #for status in status:
    #    nfl_player_id_list.append(status)

    #print status
    #print status
    #if status is None:
    #    return 'none'
    return render_template('player_status.html', status=status)
Beispiel #19
0
def get_team_info(league_id):

    f_league = F_League.query.filter_by(league_id=league_id).first()
    game_key = f_league.game_key
    yahoo_league_id = f_league.yahoo_league_id

    full_league_id = str(game_key) + '.l.' + str(yahoo_league_id)
    # assumption is that user is already yahoo-authenticated
    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    db_helper = Db_Helper()

    yahoo_caller.load_oauth_tokens(session['yahoo_token'][0],
                                   session['yahoo_token'][1])
    yahoo_caller.load_yahoo_league_key(full_league_id)
    resp, content = yahoo_caller.get_team_info()
    yahoo_parser.import_json_data(content, get='teams')
    # right now, the db insertion is done inside yahoo_parser
    team_data = yahoo_parser.return_data()
    return redirect(url_for('show_yahoo_teams', league_id=league_id))
Beispiel #20
0
def new_user_info():
    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    yahoo_db_helper = Db_Helper()

    if not 'yahoo_token' in session.keys():
        return redirect(
            url_for('yahoo_oauth', next=url_for('new_user_info') or None))
    yahoo_caller.load_oauth_tokens(session['yahoo_token'][0],
                                   session['yahoo_token'][1])

    resp, content = yahoo_caller.get_user_info()
    yahoo_parser.import_json_data(content, get='user')
    user_guid = yahoo_parser.return_data()
    yahoo_parser.import_json_data(content, get='user_v2')
    list_of_user_dicts = yahoo_parser.return_data()

    yahoo_db_helper.brians_import_user_info(user_id=session['user_id'],
                                            user_guid=user_guid)
    # list of n-fantasy_leagues here
    yahoo_db_helper.brians_import_user_info_v2(
        user_id=session['user_id'], list_of_user_dicts=list_of_user_dicts)
    return render_template('load_user_success.html')
class YahooAPICaller(object):
    """makes API calls to yahoo"""
    def __init__(self):
        self.token_1, self.token_2 = None, None
        self.yahoo_league_key = None
        self.yahoo_team_key = None
        self.yahoo_player_key = None
        self.data = None
        self.resp = None
        self.params = {}
        self.db_helper = Db_Helper()
        self.current_week = self.db_helper.return_current_week()
        self.api_call_count = 0
        pass

    def inc_api_counter(self):
        self.api_call_count += 1

    def return_api_call_count(self):
        return self.api_call_count

    def load_oauth_tokens(self, token_1, token_2):
        self.token_1, self.token_2 = token_1, token_2
        pass

    def load_yahoo_league_key(self, yahoo_league_key):
        self.yahoo_league_key = yahoo_league_key
        pass

    def load_yahoo_team_key(self, yahoo_team_key):
        self.yahoo_team_key = yahoo_team_key
        pass

    def load_yahoo_player_key(self, yahoo_player_key):
        self.yahoo_player_key = yahoo_player_key
        pass

    def get_weekly_stats_for_player(self):
        return self._get_player_fantasy_stats_for_week()

    def get_weekly_stats_for_all_players_in_league(self, week, league_key,
                                                   num_teams):
        """bulk API call for entire league. get all player stats in a league by
        looping over team_key + week. team_key is built using league_key + num_teams """
        # need to pass in:
        # league_key, num_teams
        #num_teams = 10
        #league_key = '314.l.1173612'
        return self._get_all_fantasy_player_stats_for_week(
            week, league_key, num_teams)

    def get_user_info(self):
        return self._internal_get_user_info()

    def get_league_info(self):
        return self._internal_get_league_info()

    def get_team_info(self):
        return self._internal_get_team_info()

    def get_team_stats(self, week):
        return self._internal_get_team_stats(week)

    def get_players_data(self, week, team_key):
        return self._internal_get_players_data(week, team_key)

    def get_all_players_data(self, week, league_key, num_teams):
        """bulk API call for entire league. gets all players in a league by
        looping over team_key + week. the team_key is built using league_key + num_teams """
        #num_teams = 10
        #league_key = '314.l.1173612'
        return self._internal_get_all_players_data(week, league_key, num_teams)

    def _internal_get_team_stats(self, week):
        assert self.token_1 is not None
        assert self.token_2 is not None
        assert self.yahoo_league_key is not None
        # the week parameter should be passed in or caculated by the function.
        params = {'week': week}
        resp, content = get_yahoo_api_data('league/' + self.yahoo_league_key +
                                           '/scoreboard',
                                           self.token_1,
                                           self.token_2,
                                           extras=params)
        print '%s API_CALL: GET_TEAM_STATS. LEAGUE_KEY:%s WEEK:%s' % (
            resp['date'], self.yahoo_league_key, week)
        print resp.reason, '\n'
        self.inc_api_counter()
        return resp, content

    def _internal_get_team_info(self):
        assert self.token_1 is not None
        assert self.token_2 is not None
        assert self.yahoo_league_key is not None
        resp, content = get_yahoo_api_data(
            'league/' + self.yahoo_league_key + '/teams', self.token_1,
            self.token_2)
        print '%s API_CALL: GET_TEAM_INFO. LEAGUE_KEY %s' % (
            resp['date'], self.yahoo_league_key)
        print resp.reason, '\n'
        self.inc_api_counter()
        return resp, content

    def _internal_get_league_info(self):
        assert self.token_1 is not None
        assert self.token_2 is not None
        assert self.yahoo_league_key is not None
        resp, content = get_yahoo_api_data(
            'league/' + self.yahoo_league_key + '/metadata', self.token_1,
            self.token_2)
        print '%s API_CALL: GET_LEAGUE_INFO. LEAGUE_KEY %s' % (
            resp['date'], self.yahoo_league_key)
        print resp.reason, '\n'
        self.inc_api_counter()
        return resp, content

    def _internal_get_user_info(self):
        assert self.token_1 is not None
        assert self.token_2 is not None
        params = {'use_login': '******', 'game_key': 'nfl'}
        resp, content = get_yahoo_api_data('users/' + 'games/leagues',
                                           self.token_1,
                                           self.token_2,
                                           extras=params)
        print '%s API_CALL: GET_USER_INFO.' % (resp['date'])
        print resp.reason, '\n'
        print resp
        self.inc_api_counter()
        return resp, content

    def _internal_get_players_data(self, week, team_key):
        assert self.token_1 is not None
        assert self.token_2 is not None
        param = {'week': week}
        resp, content = get_yahoo_api_data('team/' + team_key +
                                           '/roster/players',
                                           self.token_1,
                                           self.token_2,
                                           extras=param)
        print "\n"
        print '%s API_CALL: GET_PLAYERS_DATA. TEAM_KEY:%s WEEK:%s' % (
            resp['date'], team_key, week)
        print resp.reason
        self.inc_api_counter()
        return resp, content

    def _internal_get_all_players_data(self, week, league_key, num_teams):
        assert self.token_1 is not None
        assert self.token_2 is not None
        assert league_key is not None

        # repeat code, can make method seperate
        def build_team_keys(league_key, num_teams):
            list_of_team_keys = list()
            for i in range(0, num_teams):
                list_of_team_keys.append(league_key + '.t.' + str(i + 1))
            return list_of_team_keys

        def do_the_call(list_of_team_keys, week):
            list_of_returned_content = list()
            week = str(week)
            params = {'week': week}
            for team_key in list_of_team_keys:
                resp, content = get_yahoo_api_data('team/' + team_key +
                                                   '/roster/players',
                                                   self.token_1,
                                                   self.token_2,
                                                   extras=params)
                print "\n"
                print '%s API_CALL: GET_ALL_PLAYERS_DATA. TEAM_KEY:%s WEEK:%s' % (
                    resp['date'], team_key, week)
                print resp.reason
                self.inc_api_counter()
                list_of_returned_content.append(content)
            return list_of_returned_content

        list_of_keys = build_team_keys(league_key, num_teams)
        list_of_returned_content = do_the_call(list_of_keys, week)
        return list_of_returned_content

    def _get_all_fantasy_player_stats_for_week(self, week, league_key,
                                               num_teams):
        """as param, need ALL the team_keys. essentially does what
        _get_fantasy_player_stats_for_week does except it does it calls
        it for ALL the team_keys.
        """
        def build_team_keys(league_key, num_teams):
            list_of_team_keys = list()
            for i in range(0, num_teams):
                # build each team_key from league_key and num_teams
                # i+1 is used because teams start at 1 not 0.
                list_of_team_keys.append(league_key + '.t.' + str(i + 1))
            return list_of_team_keys

        def do_the_callz(list_of_team_keys, week):
            list_of_returned_content = list()
            week = str(week)
            params = {'type': 'week', 'week': week}
            # take each team_key and make API call.
            for team_key in list_of_team_keys:
                resp, content = get_yahoo_api_data("team/" + team_key +
                                                   "/roster/players/stats",
                                                   self.token_1,
                                                   self.token_2,
                                                   extras=params)
                print '%s API_CALL: GET_PLAYER_STATS_FOR_WEEK. TEAM_KEY:%s WEEK:%s' % (
                    resp['date'], team_key, week)
                print resp.reason, "\n"
                self.inc_api_counter()
                # append each returned JSON to a list.
                list_of_returned_content.append(content)
            return list_of_returned_content

        # more stuff that should be passed in to function.
        #week = self.current_week

        assert league_key == self.yahoo_league_key
        list_of_keys = build_team_keys(league_key, num_teams)
        list_of_returned_content = do_the_callz(list_of_keys, week)
        return list_of_returned_content

    def _get_player_fantasy_stats_for_week(self):
        """takes team key and week as a parameter and returns matchup points
        scored for that week, team
        
        should pass in weeks arg from views?
        """
        # this stuff is only here to get it working
        self.params = {'type': 'week', 'week': '8'}
        team_key = team_key = '314.l.1173612.t.8'
        #^^^
        resp, content = get_yahoo_api_data('team/' + team_key +
                                           '/roster/players/stats',
                                           self.token_1,
                                           self.token_2,
                                           extras=self.params)
        print 'API_CALL: GET_PLAYER_STATS_FOR_WEEK. team_key:%s' % (team_key)
        # this should never happen anyways
        # take care of this condition in views.
        #if resp.status != 200:
        #    raise Exception("resp.status != 200")
        self.inc_api_counter()
        return resp, content

    def _get_player_stats_for_season(self):
        pass
def do_everything():
    """this function is executed for new users. this will populate the database
    with all necessary information.
    user_data -> league_data -> team_data -> team_stats -> ind_player_stats"""
    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    yahoo_db_helper = Db_Helper()
    # start timer that times API calls
    api_call_start_time = time.time()

    if not 'yahoo_token' in session.keys():
        return redirect(url_for('yahoo_oauth', next=url_for('do_everything') or None))
    
    # load oauth tokens to yahoo
    yahoo_caller.load_oauth_tokens(session['yahoo_token'][0], session['yahoo_token'][1])
    
    # get_user()
    resp, content = yahoo_caller.get_user_info()
    yahoo_parser.import_json_data(content, get='user')
    user_guid = yahoo_parser.return_data()
    yahoo_league_id = ''
    list_of_user_dicts = yahoo_parser.import_json_data(content, get='user_v2')
    
    yahoo_db_helper.import_list_of_user_dicts(list_of_user_dicts)

    yahoo_db_helper.brians_import_user_info(
            user_id = session['user_id'], 
            user_guid = user_guid)

    # this does what parse_league currently does
    yahoo_db_helper.brian_import_user_info_v2(
            user_id = session['user_id'],
            yahoo_league_id = yahoo_league_id,
            start_date = start_date,
            end_date = end_date,
            num_teams = num_teams,
            league_url = league_url,
            league_name = league_name,
            game_key = game_key,
            season_year = season_year
            )
    
    # load league_id for parser
    yahoo_caller.load_yahoo_league_key(yahoo_league_id)
    
    #get_league()
    resp, content = yahoo_caller.get_league_info()
    yahoo_parser.import_json_data(content, get='leagues')
    f_league_dict = yahoo_parser.return_data()
    yahoo_db_helper.brians_import_league_info(f_league_dict)

    #get_team()
    resp, content = yahoo_caller.get_team_info()
    yahoo_parser.import_json_data(content, get='teams')
    processed_data = yahoo_parser.return_data()
    
    #yahoo_db_helper.brians_import_team_info(processed_data)
    #^ will need to rewrite to return a dictionary, as of now,
    # the db insert calls will stay inside yahoo_parser.
    
    nfl_league_key = yahoo_league_id
    league_id = yahoo_db_helper.return_league_id(session['user_id'])
    num_teams = yahoo_db_helper.return_num_teams(league_id)
    print "num_teams: %s" % num_teams
    print "nfl_league_key: %s" % nfl_league_key
    print "league_id: %s" % league_id


    def get_weeks():
        league = F_League.query.filter_by(user_id = session['user_id']).first()
        week = Week_Lookup.query.all()
        week_list = list()
        for w in week:
            if (w.start_date <= date.today()) and (w.start_date >= league.start_date):
                week_list.append(w.week_num)
        return week_list
    week_list = get_weeks()

    ## GET_PLAYERS() starts here
    #used temp to fix update_players
    
    #params : league_key, week, num_teams
    for week in week_list:
        # list of fantasy_players in entire league by week
        players_for_entire_league = yahoo_caller.get_all_players_data(week, nfl_league_key, num_teams)
        # loop over list of fantasy_players JSON data
        # parse the JSON and add them to database
        for players_per_week in players_for_entire_league:
            yahoo_parser.import_json_data(players_per_week, get='players_v2')
            processed_data = yahoo_parser.return_data()
            yahoo_db_helper.brians_import_players_data_v2(processed_data)
    ## GET_PLAYERS() ends here

    #get_team_stats()
    #^^^ will need to write a function to determine how many weeks the user has played.
    # for example if user started league in week 1 and it is currently week 10.
    # the function will need to call get_team_stats for EACH week.
    # right now, it is only handling one week.
    for week in week_list:
        resp, content = yahoo_caller.get_team_stats(week)
        yahoo_parser.import_json_data(content, get='team_stats')
        processed_data = yahoo_parser.return_data()              #<== stats_dict returned here
        yahoo_db_helper.brians_import_team_stats(processed_data) #<== stats_dict being parsed
    #^^ magical insert to DB happens here
    
    for week in week_list:
        scores_for_player = yahoo_caller.get_weekly_stats_for_all_players_in_league(
                week, nfl_league_key, num_teams)
        for player_pts_for_week in scores_for_player:
            yahoo_parser.import_json_data(player_pts_for_week, get='weekly_stats')
            parsed_data = yahoo_parser.return_data()
            yahoo_db_helper.import_player_stats(parsed_data)
            #parsed_scores.append(parsed_data)

    parsed_content=""
    
    api_call_end_time = time.time()
    api_call_time = api_call_end_time - api_call_start_time
    num_api_calls = yahoo_caller.return_api_call_count()
    
    resp = "API CALLS took: %s seconds." % api_call_time
    content = "API CALLS MADE: %s" % num_api_calls


    return render_template('yahoo.html',
            what_data = 'player_stats',
            resp = resp,
            content = content,
            processed_content = parsed_content)
Beispiel #23
0
def do_everything():
    """this function is executed for new users. this will populate the database
    with all necessary information.
    user_data -> league_data -> team_data -> team_stats -> ind_player_stats"""
    yahoo_parser = YahooHelper()
    yahoo_caller = YahooAPICaller()
    yahoo_db_helper = Db_Helper()
    # start timer that times API calls
    api_call_start_time = time.time()

    if not 'yahoo_token' in session.keys():
        return redirect(
            url_for('yahoo_oauth', next=url_for('do_everything') or None))

    # load oauth tokens to yahoo
    yahoo_caller.load_oauth_tokens(session['yahoo_token'][0],
                                   session['yahoo_token'][1])

    # get_user()
    resp, content = yahoo_caller.get_user_info()
    yahoo_parser.import_json_data(content, get='user')
    user_guid = yahoo_parser.return_data()
    yahoo_league_id = ''
    list_of_user_dicts = yahoo_parser.import_json_data(content, get='user_v2')

    yahoo_db_helper.import_list_of_user_dicts(list_of_user_dicts)

    yahoo_db_helper.brians_import_user_info(user_id=session['user_id'],
                                            user_guid=user_guid)

    # this does what parse_league currently does
    yahoo_db_helper.brian_import_user_info_v2(user_id=session['user_id'],
                                              yahoo_league_id=yahoo_league_id,
                                              start_date=start_date,
                                              end_date=end_date,
                                              num_teams=num_teams,
                                              league_url=league_url,
                                              league_name=league_name,
                                              game_key=game_key,
                                              season_year=season_year)

    # load league_id for parser
    yahoo_caller.load_yahoo_league_key(yahoo_league_id)

    #get_league()
    resp, content = yahoo_caller.get_league_info()
    yahoo_parser.import_json_data(content, get='leagues')
    f_league_dict = yahoo_parser.return_data()
    yahoo_db_helper.brians_import_league_info(f_league_dict)

    #get_team()
    resp, content = yahoo_caller.get_team_info()
    yahoo_parser.import_json_data(content, get='teams')
    processed_data = yahoo_parser.return_data()

    #yahoo_db_helper.brians_import_team_info(processed_data)
    #^ will need to rewrite to return a dictionary, as of now,
    # the db insert calls will stay inside yahoo_parser.

    nfl_league_key = yahoo_league_id
    league_id = yahoo_db_helper.return_league_id(session['user_id'])
    num_teams = yahoo_db_helper.return_num_teams(league_id)
    print "num_teams: %s" % num_teams
    print "nfl_league_key: %s" % nfl_league_key
    print "league_id: %s" % league_id

    def get_weeks():
        league = F_League.query.filter_by(user_id=session['user_id']).first()
        week = Week_Lookup.query.all()
        week_list = list()
        for w in week:
            if (w.start_date <= date.today()) and (w.start_date >=
                                                   league.start_date):
                week_list.append(w.week_num)
        return week_list

    week_list = get_weeks()

    ## GET_PLAYERS() starts here
    #used temp to fix update_players

    #params : league_key, week, num_teams
    for week in week_list:
        # list of fantasy_players in entire league by week
        players_for_entire_league = yahoo_caller.get_all_players_data(
            week, nfl_league_key, num_teams)
        # loop over list of fantasy_players JSON data
        # parse the JSON and add them to database
        for players_per_week in players_for_entire_league:
            yahoo_parser.import_json_data(players_per_week, get='players_v2')
            processed_data = yahoo_parser.return_data()
            yahoo_db_helper.brians_import_players_data_v2(processed_data)
    ## GET_PLAYERS() ends here

    #get_team_stats()
    #^^^ will need to write a function to determine how many weeks the user has played.
    # for example if user started league in week 1 and it is currently week 10.
    # the function will need to call get_team_stats for EACH week.
    # right now, it is only handling one week.
    for week in week_list:
        resp, content = yahoo_caller.get_team_stats(week)
        yahoo_parser.import_json_data(content, get='team_stats')
        processed_data = yahoo_parser.return_data(
        )  #<== stats_dict returned here
        yahoo_db_helper.brians_import_team_stats(
            processed_data)  #<== stats_dict being parsed
    #^^ magical insert to DB happens here

    for week in week_list:
        scores_for_player = yahoo_caller.get_weekly_stats_for_all_players_in_league(
            week, nfl_league_key, num_teams)
        for player_pts_for_week in scores_for_player:
            yahoo_parser.import_json_data(player_pts_for_week,
                                          get='weekly_stats')
            parsed_data = yahoo_parser.return_data()
            yahoo_db_helper.import_player_stats(parsed_data)
            #parsed_scores.append(parsed_data)

    parsed_content = ""

    api_call_end_time = time.time()
    api_call_time = api_call_end_time - api_call_start_time
    num_api_calls = yahoo_caller.return_api_call_count()

    resp = "API CALLS took: %s seconds." % api_call_time
    content = "API CALLS MADE: %s" % num_api_calls

    return render_template('yahoo.html',
                           what_data='player_stats',
                           resp=resp,
                           content=content,
                           processed_content=parsed_content)
#!/home/brian/Envs/fantasyfootballtracker/bin/python
from app.yahoo_db_helper import Db_Helper
from app.rss_parser import RSS_Parser
""" this script is ran by linux cronjob.
Need to add logging functionality to record timestamp of updates & errors.
currently, for ubuntu systems, cron jobs are logged in /var/log/cron*

postgresql: /var/log/postgresql/postgresql-9.1-main.log

"""

rss_parser = RSS_Parser()
db_helper = Db_Helper()

## get dictionaries of new parsed RSS feed.
yahoo_rss_dict = rss_parser.return_yahoo_rss()
roto_rss_dict = rss_parser.return_roto_rss()
roto_player_dict = rss_parser.return_roto_players()

# insert dictionaries to database if there are new feeds.
db_helper.load_yahoo_rss_dict(yahoo_rss_dict)
db_helper.load_roto_rss_dict(roto_rss_dict)
db_helper.load_roto_players_dict(roto_player_dict)


class YahooAPICaller(object):
    """makes API calls to yahoo"""
    
    def __init__(self):
        self.token_1, self.token_2 = None, None
        self.yahoo_league_key = None
        self.yahoo_team_key = None
        self.yahoo_player_key = None
        self.data = None
        self.resp = None
        self.params = {}
        self.db_helper = Db_Helper()
        self.current_week = self.db_helper.return_current_week()
        self.api_call_count = 0
        pass
    
    def inc_api_counter(self):
        self.api_call_count += 1

    def return_api_call_count(self):
        return self.api_call_count

    def load_oauth_tokens(self, token_1, token_2):
        self.token_1, self.token_2 = token_1, token_2
        pass

    def load_yahoo_league_key(self, yahoo_league_key):
        self.yahoo_league_key = yahoo_league_key
        pass

    def load_yahoo_team_key(self, yahoo_team_key):
        self.yahoo_team_key = yahoo_team_key
        pass

    def load_yahoo_player_key(self, yahoo_player_key):
        self.yahoo_player_key = yahoo_player_key
        pass
    
    def get_weekly_stats_for_player(self):
        return self._get_player_fantasy_stats_for_week()

    def get_weekly_stats_for_all_players_in_league(self, week, league_key, num_teams):
        """bulk API call for entire league. get all player stats in a league by
        looping over team_key + week. team_key is built using league_key + num_teams """
        # need to pass in:
        # league_key, num_teams
        #num_teams = 10
        #league_key = '314.l.1173612'
        return self._get_all_fantasy_player_stats_for_week(week, league_key, num_teams)

    def get_user_info(self):
        return self._internal_get_user_info()
    
    def get_league_info(self):
        return self._internal_get_league_info()
    
    def get_team_info(self):
        return self._internal_get_team_info()

    def get_team_stats(self, week):
        return self._internal_get_team_stats(week)

    def get_players_data(self, week, team_key):
        return self._internal_get_players_data(week, team_key)

    def get_all_players_data(self, week, league_key, num_teams):
        """bulk API call for entire league. gets all players in a league by
        looping over team_key + week. the team_key is built using league_key + num_teams """
        #num_teams = 10
        #league_key = '314.l.1173612'
        return self._internal_get_all_players_data(week, league_key, num_teams)


    def _internal_get_team_stats(self, week):
        assert self.token_1 is not None
        assert self.token_2 is not None
        assert self.yahoo_league_key is not None
        # the week parameter should be passed in or caculated by the function.
        params = {'week': week }
        resp, content = get_yahoo_api_data('league/' + self.yahoo_league_key + '/scoreboard',
                self.token_1, self.token_2 ,extras=params)
        print '%s API_CALL: GET_TEAM_STATS. LEAGUE_KEY:%s WEEK:%s' % (resp['date'], self.yahoo_league_key, week)
        print resp.reason, '\n'
        self.inc_api_counter()
        return resp, content

    def _internal_get_team_info(self):
        assert self.token_1 is not None
        assert self.token_2 is not None
        assert self.yahoo_league_key is not None
        resp, content = get_yahoo_api_data('league/' + self.yahoo_league_key + '/teams',
                self.token_1, self.token_2)
        print '%s API_CALL: GET_TEAM_INFO. LEAGUE_KEY %s' % (resp['date'], self.yahoo_league_key)
        print resp.reason, '\n'
        self.inc_api_counter()
        return resp, content
    
    def _internal_get_league_info(self):
        assert self.token_1 is not None
        assert self.token_2 is not None
        assert self.yahoo_league_key is not None
        resp, content = get_yahoo_api_data('league/' + self.yahoo_league_key + '/metadata',
                self.token_1, self.token_2)
        print '%s API_CALL: GET_LEAGUE_INFO. LEAGUE_KEY %s' % (resp['date'], self.yahoo_league_key)
        print resp.reason, '\n'
        self.inc_api_counter()
        return resp, content

    def _internal_get_user_info(self):
        assert self.token_1 is not None
        assert self.token_2 is not None
        params = { 'use_login':'******', 'game_key':'nfl' }
        resp, content = get_yahoo_api_data('users/' + 'games/leagues',
                self.token_1, self.token_2, extras=params)
        print '%s API_CALL: GET_USER_INFO.' % (resp['date'])
        print resp.reason, '\n'
        print resp
        self.inc_api_counter()
        return resp, content

    def _internal_get_players_data(self, week, team_key):
        assert self.token_1 is not None
        assert self.token_2 is not None
        param = {'week': week }
        resp, content = get_yahoo_api_data('team/' + team_key + '/roster/players',
                self.token_1, self.token_2, extras=param)
        print "\n"
        print '%s API_CALL: GET_PLAYERS_DATA. TEAM_KEY:%s WEEK:%s' % (resp['date'], team_key, week)
        print resp.reason
        self.inc_api_counter()
        return resp, content

    def _internal_get_all_players_data(self, week, league_key, num_teams):
        assert self.token_1 is not None
        assert self.token_2 is not None
        assert league_key is not None

        # repeat code, can make method seperate
        def build_team_keys(league_key, num_teams):
            list_of_team_keys = list()
            for i in range(0, num_teams):
                list_of_team_keys.append(league_key + '.t.' + str(i+1))
            return list_of_team_keys

        def do_the_call(list_of_team_keys, week):
            list_of_returned_content = list()
            week = str(week)
            params = { 'week': week }
            for team_key in list_of_team_keys:
                resp, content = get_yahoo_api_data('team/' + team_key + '/roster/players',
                self.token_1, self.token_2, extras=params)
                print "\n"
                print '%s API_CALL: GET_ALL_PLAYERS_DATA. TEAM_KEY:%s WEEK:%s' % (resp['date'], team_key, week)
                print resp.reason
                self.inc_api_counter()
                list_of_returned_content.append(content)
            return list_of_returned_content
        
        list_of_keys = build_team_keys(league_key, num_teams)
        list_of_returned_content = do_the_call(list_of_keys, week)
        return list_of_returned_content

        
    def _get_all_fantasy_player_stats_for_week(self, week, league_key, num_teams):
        """as param, need ALL the team_keys. essentially does what
        _get_fantasy_player_stats_for_week does except it does it calls
        it for ALL the team_keys.
        """
        def build_team_keys(league_key, num_teams):
            list_of_team_keys = list()
            for i in range(0, num_teams):
                # build each team_key from league_key and num_teams
                # i+1 is used because teams start at 1 not 0.
                list_of_team_keys.append(league_key + '.t.' + str(i+1))
            return list_of_team_keys

        def do_the_callz(list_of_team_keys, week):
            list_of_returned_content = list()
            week = str(week)
            params = {'type':'week', 'week':week}
            # take each team_key and make API call.
            for team_key in list_of_team_keys:
                resp, content = get_yahoo_api_data("team/" + team_key + "/roster/players/stats",
                        self.token_1, self.token_2, extras=params)
                print '%s API_CALL: GET_PLAYER_STATS_FOR_WEEK. TEAM_KEY:%s WEEK:%s' % (resp['date'], team_key, week)
                print resp.reason, "\n"
                self.inc_api_counter()
                # append each returned JSON to a list.
                list_of_returned_content.append(content)
            return list_of_returned_content
        
        # more stuff that should be passed in to function.
        #week = self.current_week

        assert league_key == self.yahoo_league_key
        list_of_keys = build_team_keys(league_key, num_teams)
        list_of_returned_content = do_the_callz(list_of_keys, week)
        return list_of_returned_content

    
    def _get_player_fantasy_stats_for_week(self):
        """takes team key and week as a parameter and returns matchup points
        scored for that week, team
        
        should pass in weeks arg from views?
        """
        # this stuff is only here to get it working
        self.params = { 'type': 'week', 'week':'8' }
        team_key = team_key = '314.l.1173612.t.8'
        #^^^
        resp, content = get_yahoo_api_data('team/' + team_key + '/roster/players/stats',
                self.token_1, self.token_2, extras=self.params)
        print 'API_CALL: GET_PLAYER_STATS_FOR_WEEK. team_key:%s' % (team_key)
        # this should never happen anyways
        # take care of this condition in views.
        #if resp.status != 200:
        #    raise Exception("resp.status != 200")
        self.inc_api_counter()
        return resp, content        
        
    def _get_player_stats_for_season(self):
        pass