Beispiel #1
0
def get_yahoo_user_info():
    yahoo_helper = YahooHelper()
    if not 'yahoo_token' in session.keys():
        return redirect(
            url_for('yahoo_oauth', next=url_for('get_yahoo_user_info')
                    or None))
    params = {'use_login': '******', 'game_key': 'nfl'}
    resp, content = get_yahoo_api_data('users/' + 'games/leagues',
                                       session['yahoo_token'][0],
                                       session['yahoo_token'][1],
                                       extras=params)
    if resp.status == 401:
        return redirect(
            url_for('yahoo_oauth', next=url_for('get_yahoo_user_info')
                    or None))
    data, resp = content, resp
    #yahoo_helper.import_json_data(content, get='user')
    #processed_data = yahoo_helper.return_data()
    return render_template(
        'yahoo.html',
        what_data='user',
        #league_id = processed_data[0],
        #user_guid = processed_data[1],
        data=data,
        resp=resp)
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 #3
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 #4
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 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
            )
Beispiel #6
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)
Beispiel #7
0
def yahoo_get_league():
    '''get own fantasy team roster. need to add these extra fields
    into to postgresql database.
    goal: be able to query fantasy_league_id -> fantasy_team_id -> fantasy_players list'''

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

    nfl_game_key = 'nfl'
    my_league_id = '1173612'
    my_league_key = nfl_game_key + '.l.' + my_league_id
    my_league_key = '314.l.1173612'

    resp, content = get_yahoo_api_data('league/' + my_league_key + '/metadata',
                                       session['yahoo_token'][0],
                                       session['yahoo_token'][1])
    if resp.status == 401:
        return redirect(
            url_for('yahoo_oauth', next=url_for('yahoo_get_league') or None))

    #YAHOO HELPER goes here to process content (processed json)

    yahoo_helper = YahooHelper()
    yahoo_helper.import_json_data(content, get="leagues")
    processed_data = yahoo_helper.return_data()
    # note: still need to get team number in league
    return render_template('yahoo.html',
                           what_data='league',
                           resp=resp,
                           data=content,
                           league_id=processed_data[0],
                           start_date=processed_data[1],
                           end_date=processed_data[2],
                           league_url=processed_data[4],
                           num_teams=processed_data[3],
                           user_id=processed_data[6])
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 yahoo_get_league():
    '''get own fantasy team roster. need to add these extra fields
    into to postgresql database.
    goal: be able to query fantasy_league_id -> fantasy_team_id -> fantasy_players list'''
    
    if not 'yahoo_token' in session.keys():
        return redirect(url_for('yahoo_oauth', next=url_for('yahoo_get_league') or None))
    
    nfl_game_key = 'nfl'
    my_league_id = '1173612'
    my_league_key = nfl_game_key + '.l.' + my_league_id
    my_league_key = '314.l.1173612'

    resp, content = get_yahoo_api_data('league/' + my_league_key + '/metadata',
            session['yahoo_token'][0], session['yahoo_token'][1])
    if resp.status == 401:
        return redirect(url_for('yahoo_oauth', next=url_for('yahoo_get_league') or None))

    #YAHOO HELPER goes here to process content (processed json)

    
    yahoo_helper = YahooHelper()
    yahoo_helper.import_json_data(content, get="leagues")
    processed_data=yahoo_helper.return_data()
    # note: still need to get team number in league
    return render_template('yahoo.html',
            what_data = 'league',
            resp = resp,
            data=content,
            league_id = processed_data[0],
            start_date = processed_data[1],
            end_date = processed_data[2],
            league_url = processed_data[4],
            num_teams = processed_data[3],
            user_id=processed_data[6]
            )
Beispiel #10
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 #11
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')
Beispiel #12
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
    '''
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')
Beispiel #14
0
def yahoo_get_players():
    #get own fantasy team players within a league
    if not 'yahoo_token' in session.keys():
        return redirect(
            url_for('yahoo_oauth', next=url_for('yahoo_get_players') or None))

    game_key = 'nfl'
    league_id = '1173612'
    team_id = '8'
    team_key_format = game_key + '.l.' + league_id + '.t.' + team_id

    #my_league_key = nfl_game_key + '.l.' + my_league_id
    my_league_key = '314.l.1173612'
    league_key = 'nfl.l.1173612'
    team_key = '314.l.1173612.t.8'
    temp = dict()

    # THIS SHOULDNT BE HARD CODED
    param = {'week': '12'}

    resp, content = get_yahoo_api_data('team/' + team_key + '/roster/players',
                                       session['yahoo_token'][0],
                                       session['yahoo_token'][1],
                                       extras=param)
    if resp.status == 401:
        return redirect(
            url_for('yahoo_oauth', next=url_for('yahoo_get_players') or None))

    # call yahoo api HELPER
    yahoo_api_helper = YahooHelper()
    yahoo_api_helper.import_json_data(content, get='players')
    yahoo_api_helper.return_data()

    # this gets team data from collection, may just only need one API call.
    #full_content = content['fantasy_content']['team'][0]

    #last ['14'] parameter represents the player
    #full_content = content['fantasy_content']['team'][1]['roster']['0']['players']['14']

    full_content = content['fantasy_content']['team'][1]['roster']['0'][
        'players']['14']
    player_data = content['fantasy_content']['team'][1]['roster']['0'][
        'players']['1']

    temp = player_data['player'][0][15]

    #iteritems() ->
    #iterkeys()
    #itervalues()
    data_wanted = ["u'player_key"]  #, "u'player_id"]

    isolated = list()
    temp_list = list()
    temp_list2 = list()
    temp_value = player_data['player'][0]
    for value in temp_value:
        temp_list.append(type(value))
        #for value in itertools.islice(value,4,5):
        #    isolated = value
        if type(value) == dict:
            for attr in data_wanted:
                if value.has_key(u'player_key'):
                    isolated = value[u'player_key']
                pass
            for key, value in value.iteritems():
                temp_list2.append(key)
                #isolated.append(value)

    #else:
    #    temp_list = 'isn\'t a dict'

    roster_position = player_data['player'][1]['selected_position'][1][
        'position']
    yahoo_player_key = player_data['player'][0][0]['player_key']
    yahoo_player_id = player_data['player'][0][1]['player_id']
    yahoo_full_name = player_data['player'][0][2]['name']['full']
    yahoo_first_name = player_data['player'][0][2]['name']['first']
    yahoo_players_team = player_data['player'][0][5]
    #yahoo_last_name = player_data['player'][0][2]['name']['last']
    yahoo_last_name = player_data['player'][0][2]['name']
    #yahoo_players_bye_week = player_data['player'][0][7]['bye_weeks']['week']
    yahoo_players_bye_week = player_data['player'][0][7]
    #yahoo_players_team_abbr = player_data['player'][0][6]['editorial_team_abbr']
    yahoo_players_team_abbr = player_data['player'][0][6]
    #yahoo_players_number = player_data['player'][0][8]['uniform_number']
    yahoo_players_number = player_data['player'][0][8]
    #yahoo_players_position = player_data['player'][0][9]['display_position']
    yahoo_players_position = player_data['player'][0][9]

    #position = player_data['player'][1][]

    return render_template('yahoo.html',
                           what_data='players',
                           isolated=isolated,
                           temp_list=temp_list,
                           temp_list2=temp_list2,
                           temp=temp,
                           resp=resp,
                           data=content,
                           player_data=player_data,
                           full_content=content,
                           roster_position=roster_position,
                           yahoo_player_key=yahoo_player_key,
                           yahoo_player_id=yahoo_player_id,
                           yahoo_full_name=yahoo_full_name,
                           yahoo_first_name=yahoo_first_name,
                           yahoo_last_name=yahoo_last_name,
                           yahoo_players_team=yahoo_players_team,
                           yahoo_players_team_abbr=yahoo_players_team_abbr,
                           yahoo_player_bye_week=yahoo_players_bye_week,
                           yahoo_players_number=yahoo_players_number,
                           yahoo_players_position=yahoo_players_position)
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')
def yahoo_get_players():
    #get own fantasy team players within a league
    if not 'yahoo_token' in session.keys():
        return redirect(url_for('yahoo_oauth', next=url_for('yahoo_get_players') or None))
    
    game_key = 'nfl'
    league_id = '1173612'
    team_id = '8'
    team_key_format = game_key + '.l.' + league_id + '.t.' + team_id

    #my_league_key = nfl_game_key + '.l.' + my_league_id
    my_league_key = '314.l.1173612'
    league_key = 'nfl.l.1173612'
    team_key = '314.l.1173612.t.8'
    temp = dict()
    
    # THIS SHOULDNT BE HARD CODED
    param = {'week':'12'}
    
    resp, content = get_yahoo_api_data('team/' + team_key + '/roster/players',
            session['yahoo_token'][0], session['yahoo_token'][1], extras=param)
    if resp.status == 401:
        return redirect(url_for('yahoo_oauth', next=url_for('yahoo_get_players') or None))
    
    # call yahoo api HELPER
    yahoo_api_helper=YahooHelper()
    yahoo_api_helper.import_json_data(content, get='players')
    yahoo_api_helper.return_data()

    # this gets team data from collection, may just only need one API call.
    #full_content = content['fantasy_content']['team'][0]
    
    #last ['14'] parameter represents the player
    #full_content = content['fantasy_content']['team'][1]['roster']['0']['players']['14']
    
    full_content = content['fantasy_content']['team'][1]['roster']['0']['players']['14']
    player_data = content['fantasy_content']['team'][1]['roster']['0']['players']['1']
    
    temp = player_data['player'][0][15]

    #iteritems() -> 
    #iterkeys()
    #itervalues()
    data_wanted = ["u'player_key"]#, "u'player_id"]
    
    isolated = list()
    temp_list = list()
    temp_list2 = list()
    temp_value = player_data['player'][0]
    for value in temp_value:
        temp_list.append(type(value))
        #for value in itertools.islice(value,4,5):
        #    isolated = value
        if type(value) == dict:
            for attr in data_wanted: 
                if value.has_key(u'player_key'):
                    isolated = value[u'player_key']
                pass
            for key, value in value.iteritems():
                temp_list2.append(key)
                #isolated.append(value)

    #else:
    #    temp_list = 'isn\'t a dict'
    
    roster_position = player_data['player'][1]['selected_position'][1]['position']
    yahoo_player_key = player_data['player'][0][0]['player_key']
    yahoo_player_id = player_data['player'][0][1]['player_id']
    yahoo_full_name = player_data['player'][0][2]['name']['full']
    yahoo_first_name = player_data['player'][0][2]['name']['first']
    yahoo_players_team = player_data['player'][0][5]
    #yahoo_last_name = player_data['player'][0][2]['name']['last']
    yahoo_last_name = player_data['player'][0][2]['name']
    #yahoo_players_bye_week = player_data['player'][0][7]['bye_weeks']['week']
    yahoo_players_bye_week = player_data['player'][0][7]
    #yahoo_players_team_abbr = player_data['player'][0][6]['editorial_team_abbr']
    yahoo_players_team_abbr = player_data['player'][0][6]
    #yahoo_players_number = player_data['player'][0][8]['uniform_number']
    yahoo_players_number = player_data['player'][0][8]
    #yahoo_players_position = player_data['player'][0][9]['display_position']
    yahoo_players_position = player_data['player'][0][9]

    #position = player_data['player'][1][]

    
    return render_template('yahoo.html',
            what_data = 'players',
            isolated = isolated,
            temp_list = temp_list,
            temp_list2 = temp_list2,
            temp = temp,
            resp = resp,
            data=content,
            player_data = player_data,
            full_content = content,
            roster_position = roster_position,
            yahoo_player_key = yahoo_player_key,
            yahoo_player_id = yahoo_player_id,
            yahoo_full_name = yahoo_full_name,
            yahoo_first_name = yahoo_first_name,
            yahoo_last_name = yahoo_last_name,
            yahoo_players_team = yahoo_players_team,
            yahoo_players_team_abbr = yahoo_players_team_abbr,
            yahoo_player_bye_week = yahoo_players_bye_week,
            yahoo_players_number = yahoo_players_number,
            yahoo_players_position = yahoo_players_position
            )
Beispiel #17
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)
class TestCase(unittest.TestCase):

    def setUp(self):
        self.yahoo_helper = YahooHelper()
        pass
    
    def tearDown(self):
        pass
    
    def test_yahoo_api_helper_reset(self):
        """test to make sure that object resets itself when it is called
        again to import data"""
        self.assertEqual(self.yahoo_helper.data, None)
        imported_json = json.load(open('json_team_data.txt'))
        self.yahoo_helper.import_json_data(imported_json, get='user')
        self.assertEqual(self.yahoo_helper.data, None)
        

    def test_reuse_module(self):
        """test that when yahoo_api_helper is reused without instantation, that
        all data from older request is cleared. self.__init__() should do this."""
        # this seems stupid
        
        self.yahoo_helper.import_json_data(data, arg='')
        returned_data = self.yahoo_helper.return_data()
        
        pass

    def test_json_import(self):
        """test that data imported in to this module is an actual JSON
        object."""
        
        self.assertEqual(self.yahoo_helper.data, None)
        data = open('json_team_data.txt')
        jsond = json.load(data)
        self.yahoo_helper.import_json_data(jsond, get='teams')
        self.assertEqual(self.yahoo_helper.get, 'teams')

        #self.assertEqual(self.yahoo_helper.data, jsond)

    def test_json_import_exception(self):
        """test that import_json_data throws an exception when datatype is incorrect"""
        data = 'test data'
        self.yahoo_helper.import_json_data(data, get='teams')
        self.assertEqual(self.yahoo_helper.data, 'test data')
        self.assertRaises(Exception, self.yahoo_helper.import_json_data, 'not accepted' )
        self.assertRaises(Exception, data=self.yahoo_helper.return_data() )
        #json_data = "asdf"
        #self.assertRaises(Exception, self.yahoo_helper.import_json_data(data, get='zz'))

    def test_parse_user_info(self):
        """ 
        test that this correctly returns a list object of yahoo
        fantasy league id's.
        """
        pass


    def test_league_id_count(self):
        """ 
        test that the _parse_user_data method correctly determines
        the occurences of u'league_key'. this value is used to determine
        the amount of yahoo fantasy leagues the user is a part of.
        """
        pass


    def test_more_than_one_league(self):
        """
        test _parse_user_data is able to handle the occurence of more
        than one fantasy league per user. method should simply append
        all fantasy league_id's to a list which is later returned

        """


    def test_league_id_exception(self):
        """
        test that _parse_user_data correctly throws an exception when:
        1. the team count happens to be 0 or a negative number.
        2. there is no parsed data using regex to parse league_key
        
        """
        pass

    def test_parse_yahoo_league_id(self):
        """
        _parse_yahoo_league_id should clean up league_id's returned from
        other methods. it should remove all uneeded information. return value
        should be yahoo_league_id with no extra characters.
        
        """

        pass
Beispiel #19
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')
 def setUp(self):
     self.yahoo_helper = YahooHelper()
     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)