Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basketball Add Player News #632

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

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 @@
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 @@
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 @@
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 @@
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 @@
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