Пример #1
0
def get_score_for_team(sport_type, team_id):
    url = ESPN_API_PREFIX + Sport.get_resource_url(
        sport_type) + f"/teams/{team_id}"
    params = {
        "region": "us",
        "lang": "en",
        "contentorigin": "espn",
        "limit": "99"
    }
    r = requests.get(url=url, params=params)
    data = r.json()
    event = data["team"]["nextEvent"][0]
    params["event"] = event["id"]

    # now using the event_id to find the current score related info
    # scoreboard_url = ESPN_API_PREFIX + Sport.get_resource_url(sport_type) + f"/scoreboard"
    event_url = ESPN_API_PREFIX + Sport.get_resource_url(
        sport_type) + f"/summary"
    event_r = requests.get(url=event_url, params=params)
    event_data = event_r.json()
    team1_data = event_data["header"]["competitions"][0]["competitors"][0]
    team2_data = event_data["header"]["competitions"][0]["competitors"][1]
    if team1_data["homeAway"] == "home":
        home_data = team1_data
    else:
        home_data = team2_data
    home_team = Team.get_team(sport_type,
                              home_data["id"],
                              score=home_data["score"])
    return home_team.get_current_score()
Пример #2
0
def get_team_list(sport_type, exclude=[]):
    """
    Takes in SportType Enum and gets a list of teams associated with it.
    :param sport_type: Should be of type SportType Enum, found within the Sport class in service/espn/sports.py
    :param exclude: list of teams objects to exclude from list
    :return: list of Team objects associated with the sport_type
    """
    url = ESPN_API_PREFIX + Sport.get_resource_url(sport_type) + "/teams"
    params = {
        "region": "us",
        "lang": "en",
        "contentorigin": "espn",
        "limit": "99"
    }
    r = requests.get(url=url, params=params)
    data = r.json()
    team_list = [
        team["team"] for team in data["sports"][0]["leagues"][0]["teams"]
    ]
    team_objects_list = []
    excluded_teams = [team.id for team in exclude if team.sport == sport_type]
    for team in team_list:
        if team["id"] not in excluded_teams:
            team_objects_list.append(
                Team(team["id"], team["displayName"], team["abbreviation"],
                     sport_type, team["logos"][0]["href"]))
    return team_objects_list
Пример #3
0
def favorites():
    if not current_user.is_authenticated:
        return redirect(url_for('auth.login'))
    if not current_user.has_access:
        return redirect(url_for('views.account'))
    if request.method == "POST":
        new_favorites = request.form['current_favorites']
        new_favorites = new_favorites.split(",")
        favorite_team_objects = []
        for favorite in new_favorites:
            split_favorite_string = favorite.split("-")
            league = Sport.get_sport_type_by_value(split_favorite_string[0])
            if league:
                team = get_team(league, split_favorite_string[1])
                if team:
                    favorite_team_objects.append(team)
        current_user.update_favorites(favorite_team_objects)
    user_favorites = current_user.get_favorites()
    user_favorite_team_ids = set([team.id for team in user_favorites])
    other_teams = get_all_sports_teams(user_favorite_team_ids)
    return render_template('views/favorites.html',
                           user=current_user,
                           favorites=user_favorites,
                           other_teams=other_teams,
                           default_sport=Sport.SportType.NFL)
Пример #4
0
def get_espn_event_data(espn_event_id, sport_type, league=None):
    params = {"region": "us",
              "lang": "en",
              "contentorigin": "espn",
              "limit": "99"}
    event_id = espn_event_id
    params["event"] = event_id
    # now using the event_id to find the current score related info
    # scoreboard_url = ESPN_API_PREFIX + Sport.get_resource_url(sport_type) + f"/scoreboard"
    if sport_type == Sport.SportType.SOCCER:
        event_url = ESPN_API_PREFIX + Sport.get_resource_url(sport_type) + f"/{league}/summary"
    else:
        event_url = ESPN_API_PREFIX + Sport.get_resource_url(sport_type) + f"/summary"
    event_r = requests.get(url=event_url, params=params)
    event_data = event_r.json()
    return event_data
Пример #5
0
def get_espn_event_info(sport, event_id):
    if request.method == "GET" and current_user.is_authenticated:
        sport_enum = Sport.get_sport_type_by_value(sport)
        event_data = get_espn_event_data(event_id, sport_enum)
        event = parse_event_data(sport_enum, event_id, event_data)
        if event is None:
            return {}
        else:
            return event.to_dict()
    elif request.method == "POST":
        data = request.get_json()
        if data.get("secret_key") != os.environ.get('SECRET_KEY'):
            return "Not Authorized", 401
        sport_enum = Sport.get_sport_type_by_value(sport)
        event_data = get_espn_event_data(event_id, sport_enum)
        event = parse_event_data(sport_enum, event_id, event_data)
        if event is None:
            return {}
        else:
            return event.to_dict()
    else:
        return "Bad request", 400
Пример #6
0
 def current_event_id(self):
     url = ESPN_API_PREFIX + Sport.get_resource_url(self.sport_type) + f"/teams/{self.espn_id}"
     params = {"region": "us",
               "lang": "en",
               "contentorigin": "espn",
               "limit": "99"}
     r = requests.get(url=url, params=params)
     data = r.json()
     league_abbreviation = None
     if self.sport_type == Sport.SportType.SOCCER:
         league_abbreviation = data["team"]["defaultLeague"]["midsizeName"]
         new_url = ESPN_API_PREFIX + f"/soccer/{league_abbreviation}/teams/{self.espn_id}"
         new_request = requests.get(url=new_url, params=params)
         data = new_request.json()
     next_events = data["team"]["nextEvent"]
     if len(next_events) == 0:
         return None, None
     else:
         event = next_events[0]
     event_id = event["id"]
     return event_id, league_abbreviation
Пример #7
0
 def get_team(cls, league, team_id, score=None):
     url = ESPN_API_PREFIX + Sport.get_resource_url(league) + "/teams"
     params = {
         "region": "us",
         "lang": "en",
         "contentorigin": "espn",
         "limit": "99"
     }
     r = requests.get(url=url, params=params)
     data = r.json()
     teams = [
         team["team"] for team in data["sports"][0]["leagues"][0]["teams"]
     ]
     for team in teams:
         if team.get("id") == str(team_id):
             return Team(team["id"],
                         team["displayName"],
                         team["abbreviation"],
                         league,
                         team["logos"][0]["href"],
                         score=score)
     return None
Пример #8
0
    def get_current_score(self):
        url = ESPN_API_PREFIX + Sport.get_resource_url(
            self.sport) + f"/teams/{self.id}"
        params = {
            "region": "us",
            "lang": "en",
            "contentorigin": "espn",
            "limit": "99"
        }
        r = requests.get(url=url, params=params)
        data = r.json()
        event = data["team"]["nextEvent"][0]
        event_id = event["id"]
        params["event"] = event_id

        # now using the event_id to find the current score related info
        # scoreboard_url = ESPN_API_PREFIX + Sport.get_resource_url(sport_type) + f"/scoreboard"
        event_url = ESPN_API_PREFIX + Sport.get_resource_url(
            self.sport) + f"/summary"
        event_r = requests.get(url=event_url, params=params)
        event_data = event_r.json()
        team1_data = event_data["header"]["competitions"][0]["competitors"][0]
        team2_data = event_data["header"]["competitions"][0]["competitors"][1]
        if team1_data["homeAway"] == "home":
            home_data = team1_data
            away_data = team2_data
        else:
            home_data = team2_data
            away_data = team1_data
        status_data = event_data["header"]["competitions"][0]["status"]
        status = status_data["type"]["name"]
        away_team = Team.get_team(self.sport, away_data["id"])
        home_team = Team.get_team(self.sport, home_data["id"])
        away_score = away_data.get("score")
        home_score = home_data.get("score")
        if self.sport == Sport.SportType.MLB:
            if status == "STATUS_IN_PROGRESS":
                inning_string = status_data["type"]["detail"]
            else:
                inning_string = "FINAL"
            return MLBEvent(event_id, away_team, away_score, home_team,
                            home_score, inning_string, status)
        elif self.sport == Sport.SportType.NFL:
            if status == "STATUS_IN_PROGRESS":
                period = status_data["period"]
                clock = status_data["displayClock"]
                play = event_data["drives"]["current"]["plays"][-1]["end"]
                down = play.get("shortDownDistanceText")
                yardline = play.get("possessionText")
                possession = play["team"]["id"]
            else:
                period = None
                clock = None
                down = None
                yardline = None
                possession = None
            return NFLEvent(event_id, away_team, away_score, home_team,
                            home_score, period, clock, status, down, yardline,
                            possession)
        elif self.sport == Sport.SportType.NBA:
            if status == "STATUS_IN_PROGRESS":
                period = status_data["period"]
                clock = status_data["displayClock"]
            else:
                period = None
                clock = None
            return NBAEvent(event_id, away_team, away_score, home_team,
                            home_score, period, clock, status)
        elif self.sport == Sport.SportType.NHL:
            if status == "STATUS_IN_PROGRESS":
                period = status_data["period"]
                clock = status_data["displayClock"]
            else:
                period = None
                clock = None
            return NHLEvent(event_id, away_team, away_score, home_team,
                            home_score, period, clock, status)