Skip to content

Commit

Permalink
Feature/Add player news
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDiebold committed Feb 2, 2025
1 parent 107cc52 commit f0637bd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
11 changes: 8 additions & 3 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def box_scores(self, matchup_period: int = None, scoring_period: int = None, mat
matchup.away_team = team
return box_data

def player_info(self, name: str = None, playerId: Union[int, list] = None) -> Union[Player, List[Player]]:
def player_info(self, name: str = None, playerId: Union[int, list] = None, include_news = False) -> Union[Player, List[Player]]:
''' Returns Player class if name found '''

if name:
Expand All @@ -209,7 +209,12 @@ def player_info(self, name: str = None, playerId: Union[int, list] = None) -> Un

pro_schedule = self._get_all_pro_schedule()

if include_news:
news = {}
for id in playerId:
news[id] = self.espn_request.get_player_news(id)

Check warning on line 215 in espn_api/basketball/league.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L213-L215

Added lines #L213 - L215 were not covered by tests

if len(data['players']) == 1:
return Player(data['players'][0], self.year, pro_schedule)
return Player(data['players'][0], self.year, pro_schedule, news=news.get(playerId[0], []) if include_news else None)
if len(data['players']) > 1:
return [Player(player, self.year, pro_schedule) for player in data['players']]
return [Player(player, self.year, pro_schedule, news=news.get(player['id'], []) if include_news else None) for player in data['players']]

Check warning on line 220 in espn_api/basketball/league.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/league.py#L220

Added line #L220 was not covered by tests
14 changes: 12 additions & 2 deletions espn_api/basketball/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Player(object):
'''Player are part of team'''
def __init__(self, data, year, pro_team_schedule = None):
def __init__(self, data, year, pro_team_schedule = None, news = None):
self.name = json_parsing(data, 'fullName')
self.playerId = json_parsing(data, 'id')
self.year = year
Expand All @@ -18,6 +18,7 @@ def __init__(self, data, year, pro_team_schedule = None):
self.posRank = json_parsing(data, 'positionalRanking')
self.stats = {}
self.schedule = {}
self.news = {}

if pro_team_schedule:
pro_team_id = json_parsing(data, 'proTeamId')
Expand All @@ -27,7 +28,16 @@ def __init__(self, data, year, pro_team_schedule = None):
team = game['awayProTeamId'] if game['awayProTeamId'] != pro_team_id else game['homeProTeamId']
self.schedule[key] = { 'team': PRO_TEAM_MAP[team], 'date': datetime.fromtimestamp(game['date']/1000.0) }


if news:
news_feed = news.get("news", {}).get("feed", [])
self.news = [

Check warning on line 33 in espn_api/basketball/player.py

View check run for this annotation

Codecov / codecov/patch

espn_api/basketball/player.py#L32-L33

Added lines #L32 - L33 were not covered by tests
{
"published": item.get("published", ""),
"headline": item.get("headline", ""),
"story": item.get("story", "")
}
for item in news_feed
]

# add available stats

Expand Down
1 change: 1 addition & 0 deletions espn_api/requests/constant.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FANTASY_BASE_ENDPOINT = 'https://lm-api-reads.fantasy.espn.com/apis/v3/games/'
NEWS_BASE_ENDPOINT = 'https://site.api.espn.com/apis/fantasy/v3/games/'
FANTASY_SPORTS = {
'nfl' : 'ffl',
'nba' : 'fba',
Expand Down
17 changes: 16 additions & 1 deletion espn_api/requests/espn_requests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests
import json
from .constant import FANTASY_BASE_ENDPOINT, FANTASY_SPORTS
from .constant import FANTASY_BASE_ENDPOINT, NEWS_BASE_ENDPOINT, FANTASY_SPORTS
from ..utils.logger import Logger
from typing import List

Expand All @@ -24,6 +24,7 @@ def __init__(self, sport: str, year: int, league_id: int, cookies: dict = None,
self.year = year
self.league_id = league_id
self.ENDPOINT = FANTASY_BASE_ENDPOINT + FANTASY_SPORTS[sport] + '/seasons/' + str(self.year)
self.NEWS_ENDPOINT = NEWS_BASE_ENDPOINT + FANTASY_SPORTS[sport] + '/news/' + 'players'
self.cookies = cookies
self.logger = logger

Expand Down Expand Up @@ -86,6 +87,14 @@ def get(self, params: dict = None, headers: dict = None, extend: str = ''):
if self.logger:
self.logger.log_request(endpoint=endpoint, params=params, headers=headers, response=r.json())
return r.json()

def news_get(self, params: dict = None, headers: dict = None, extend: str = ''):
endpoint = self.NEWS_ENDPOINT + extend
r = requests.get(endpoint, params=params, headers=headers, cookies=self.cookies)

Check warning on line 93 in espn_api/requests/espn_requests.py

View check run for this annotation

Codecov / codecov/patch

espn_api/requests/espn_requests.py#L92-L93

Added lines #L92 - L93 were not covered by tests

if self.logger:
self.logger.log_request(endpoint=endpoint, params=params, headers=headers, response=r.json())
return r.json()

Check warning on line 97 in espn_api/requests/espn_requests.py

View check run for this annotation

Codecov / codecov/patch

espn_api/requests/espn_requests.py#L95-L97

Added lines #L95 - L97 were not covered by tests

def get_league(self):
'''Gets all of the leagues initial data (teams, roster, matchups, settings)'''
Expand Down Expand Up @@ -152,6 +161,12 @@ def get_player_card(self, playerIds: List[int], max_scoring_period: int, additio
data = self.league_get(params=params, headers=headers)
return data

def get_player_news(self, playerId):
'''Gets the player news'''
params = {'playerId': playerId}
data = self.news_get(params=params)
return data

Check warning on line 168 in espn_api/requests/espn_requests.py

View check run for this annotation

Codecov / codecov/patch

espn_api/requests/espn_requests.py#L166-L168

Added lines #L166 - L168 were not covered by tests

# Username and password no longer works using their API without using google recaptcha
# Possibly revisit in future if anything changes

Expand Down

0 comments on commit f0637bd

Please sign in to comment.