Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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 __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
Exemplo n.º 5
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)
Exemplo n.º 6
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)
Exemplo n.º 7
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))
Exemplo n.º 8
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)
Exemplo n.º 9
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
    '''
Exemplo n.º 10
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)
Exemplo n.º 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')
#!/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)


Exemplo n.º 13
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)