Example #1
0
def main():
    mybot = Updater(get_key(), request_kwargs=PROXY)
    dp = mybot.dispatcher
    dp.add_handler(CommandHandler("start", greet_user))
    dp.add_handler(CommandHandler("calc", calc))
    mybot.start_polling()
    mybot.idle()
def create_obj_ids(testing, obj):
    """
        gets SportRadar's season and team IDs
    :param testing: if you are testing or debugging
    :param obj: team or season string
    :return: dictionary of SportRadar {IDs: name} (note: the IDs are not ints)
    """
    if testing:
        if obj == 'team':
            it = ET.iterparse(
                "/Users/Atharva/Documents/Github/NBA_Analysis/teams-sample.xml"
            )
        else:
            it = ET.iterparse(
                "/Users/Atharva/Documents/Github/NBA_Analysis/seasons-sample.xml"
            )
    else:
        if obj == 'team':
            response_body = generate_xml(
                'http://api.sportradar.us/nba/trial/v7/en/seasons/2018'
                '/REG/standings.xml?api_key=' + api_key.get_key(),
                [None, 1])  # 1 is insignificant
        else:
            response_body = generate_xml(
                'http://api.sportradar.us/nba/trial/v7/en/league/'
                'seasons.xml?api_key=' + api_key.get_key(), [None, None])

        it = ET.iterparse(str(response_body))

    root = remove_xsd(it)
    obj_dict = dict()

    for item in root.iter(obj):
        if obj == 'team':
            obj_dict[item.attrib['id']] = item.attrib['name']
        else:
            obj_dict[item.attrib['id']] = item.attrib['year']

    return obj_dict
Example #3
0
 def get_alerts(types):
     url = "http://realtime.mbta.com/developer/api/v2/alerts?api_key={0}&include_access_alerts=false&format=json".format(
         api_key.get_key())
     try:
         result = urllib2.urlopen(url, timeout=60)
         full_data = result.read()
         full_data_json = json.loads(full_data)
         delay_data = [obj for obj in full_data_json["alerts"] if "cause_name" in obj]
         if types is not None:
             delay_data = [obj for obj in delay_data if obj["cause_name"] in types]
         return delay_data
     except urllib2.HTTPError:
         return
Example #4
0
 def get_key(self):
     return get_key(self.api_key)
def compile_stats(testing):
    """
        sets up the required fields / peripheral information and runs the program. Example API call:
    http://api.sportradar.us/nba/trial/v7/en/seasons/2018/REG/teams/583eca2f-fb46-11e1-82cb-f4ce4684ea4c
    /statistics.xml?api_key=[KEY]
    :param testing: if you are testing or debugging
    :return:
    """
    season_ids = create_obj_ids(testing, 'season')
    # print(season_ids.values())
    # print(season_ids.keys())
    time.sleep(1)
    team_ids = create_obj_ids(testing, 'team')

    team_feeds = ['team_records', 'total', 'average']
    player_feeds = ['player', 'total', 'average']

    sql_functions.create_db(testing)
    sql_functions.populate_seasons_table(testing, season_ids)

    if testing:
        it = ET.iterparse(
            "/Users/Atharva/Documents/Github/NBA_Analysis/teams-sample.xml")

        season_id = '47c9979e-5c3f-453d-ac75-734d17412e3f'
        team_id = '583eca2f-fb46-11e1-82cb-f4ce4684ea4c'
        season_and_team_ids = [season_id, team_id]

        df_generator(testing, it, team_feeds, None, 'teams')

        it = ET.iterparse(
            "C:/Users/Atharva/Documents/GitHub/NBA_Analysis/Raw_XML_Files/team/fb89a852-3f68-4505-85b0"
            "-19b428b261d5_team_583ecfa8-fb46-11e1-82cb-f4ce4684ea4c.xml")
        df_generator(testing, it, team_feeds, season_and_team_ids, 'stats')
        df_generator(testing, it, player_feeds, season_and_team_ids, 'stats')
    else:
        ind = 0
        for season_id in season_ids:

            # have limited, no, or unusable data
            if season_ids[season_id] == '2012' or season_ids[
                    season_id] == '2019':
                continue

            list_season_ids = list(season_ids)
            index_mod = (list_season_ids.index(season_id)) % 3

            if index_mod == 0 or index_mod == 1:
                continue
            # have to check if this works. Was throwing errors earlier with the PRE season, but that might have been
            # resolved by the other fixes made. Need to remove the ==1 above to see if it works.
            # elif index_mod == 1:
            #     season_type = "PST"
            else:
                season_type = 'REG'

            # gets season info
            time.sleep(1)  # API allows 1 call/second
            api_call = generate_xml(
                'http://api.sportradar.us/nba/trial/v7/en/seasons/' +
                str(season_ids[season_id]) + '/' + season_type +
                '/standings.xml?api_key=' + api_key.get_key(),
                [season_id, None])

            it = ET.iterparse(api_call)
            df_generator(testing, it, team_feeds, None, 'teams')

            # iterate through each team and get team/player info
            ind2 = 0
            for team in team_ids:
                season_and_team_ids = [season_id, team]

                # all player info, TeamTotal, TeamAverage
                time.sleep(1)  # API allows 1 call/second

                api_call = generate_xml(
                    'http://api.sportradar.us/nba/trial/v7/en/seasons/' +
                    str(season_ids[season_id]) + '/' + season_type +
                    '/teams/' + team + '/statistics.xml?api_key=' +
                    api_key.get_key(), season_and_team_ids)

                it = ET.iterparse(api_call)
                df_generator(testing, it, team_feeds, season_and_team_ids,
                             'stats')
                df_generator(testing, it, player_feeds, season_and_team_ids,
                             'stats')

                ind2 += 1
                print("teams:", ind2)

            print("seasons", season_ids[season_id])
            ind += 1