diff --git a/README.md b/README.md index 29877b2d..89fb3445 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,7 @@ ![](https://github.com/cwendt94/espn-api/workflows/Espn%20API%20Integration%20Test/badge.svg) [![codecov](https://codecov.io/gh/cwendt94/espn-api/branch/master/graphs/badge.svg)](https://codecov.io/gh/cwendt94/espn-api) [![Join the chat at https://gitter.im/ff-espn-api/community](https://badges.gitter.im/ff-espn-api/community.svg)](https://gitter.im/ff-espn-api/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![PyPI version](https://badge.fury.io/py/espn-api.svg)](https://badge.fury.io/py/espn-api) ## ESPN API -## [NOTICE] Username and password will be removed soon. You can access your private league using SWID and ESPN_S2. -This package uses ESPN's Fantasy API to extract data from any public or private league for **Fantasy Football and Basketball (Hockey and Baseball in development)**. +This package uses ESPN's Fantasy API to extract data from any public or private league for **Fantasy Football and Basketball (NHL, MLB, and WNBA are in development)**. Please feel free to make suggestions, bug reports, and pull request for features or fixes! This package was inspired and based off of [rbarton65/espnff](https://github.com/rbarton65/espnff). diff --git a/espn_api/base_league.py b/espn_api/base_league.py index 16277064..a0690b06 100644 --- a/espn_api/base_league.py +++ b/espn_api/base_league.py @@ -7,7 +7,7 @@ class BaseLeague(ABC): '''Creates a League instance for Public/Private ESPN league''' - def __init__(self, league_id: int, year: int, sport: str, espn_s2=None, swid=None, username=None, password=None, debug=False): + def __init__(self, league_id: int, year: int, sport: str, espn_s2=None, swid=None, debug=False): self.logger = Logger(name=f'{sport} league', debug=debug) self.league_id = league_id self.year = year @@ -22,8 +22,6 @@ def __init__(self, league_id: int, year: int, sport: str, espn_s2=None, swid=Non 'SWID': swid } self.espn_request = EspnFantasyRequests(sport=sport, year=year, league_id=league_id, cookies=cookies, logger=self.logger) - if username and password: - self.espn_request.authentication(username, password) def __repr__(self): return 'League(%s, %s)' % (self.league_id, self.year, ) diff --git a/espn_api/baseball/league.py b/espn_api/baseball/league.py index 35cf3f1d..7ddda8ee 100644 --- a/espn_api/baseball/league.py +++ b/espn_api/baseball/league.py @@ -18,8 +18,8 @@ class League(BaseLeague): ScoreTypes = {'H2H_CATEGORY': H2HCategoryBoxScore, 'H2H_POINTS': H2HPointsBoxScore} - def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, username=None, password=None, fetch_league=True, debug=False): - super().__init__(league_id=league_id, year=year, sport='mlb', espn_s2=espn_s2, swid=swid, username=username, password=password, debug=debug) + def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_league=True, debug=False): + super().__init__(league_id=league_id, year=year, sport='mlb', espn_s2=espn_s2, swid=swid, debug=debug) self._set_scoring_class = lambda scoring_type: League.ScoreTypes.get(scoring_type, BoxScore) diff --git a/espn_api/basketball/league.py b/espn_api/basketball/league.py index 96406e89..ebb8f94e 100644 --- a/espn_api/basketball/league.py +++ b/espn_api/basketball/league.py @@ -16,8 +16,8 @@ class League(BaseLeague): '''Creates a League instance for Public/Private ESPN league''' - def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, username=None, password=None, fetch_league=True, debug=False): - super().__init__(league_id=league_id, year=year, sport='nba', espn_s2=espn_s2, swid=swid, username=username, password=password, debug=debug) + def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_league=True, debug=False): + super().__init__(league_id=league_id, year=year, sport='nba', espn_s2=espn_s2, swid=swid, debug=debug) if fetch_league: self.fetch_league() diff --git a/espn_api/football/league.py b/espn_api/football/league.py index 2b473af8..76e56bf6 100644 --- a/espn_api/football/league.py +++ b/espn_api/football/league.py @@ -16,8 +16,8 @@ class League(BaseLeague): '''Creates a League instance for Public/Private ESPN league''' - def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, username=None, password=None, fetch_league=True, debug=False): - super().__init__(league_id=league_id, year=year, sport='nfl', espn_s2=espn_s2, swid=swid, username=username, password=password, debug=debug) + def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_league=True, debug=False): + super().__init__(league_id=league_id, year=year, sport='nfl', espn_s2=espn_s2, swid=swid, debug=debug) if fetch_league: self.fetch_league() diff --git a/espn_api/hockey/league.py b/espn_api/hockey/league.py index 51267f44..0fe84535 100644 --- a/espn_api/hockey/league.py +++ b/espn_api/hockey/league.py @@ -14,9 +14,8 @@ class League(BaseLeague): '''Creates a League instance for Public/Private ESPN league''' - def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, username=None, password=None, fetch_league=True, debug=False): - super().__init__(league_id=league_id, year=year, sport='nhl', espn_s2=espn_s2, swid=swid, username=username, - password=password, debug=debug) + def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_league=True, debug=False): + super().__init__(league_id=league_id, year=year, sport='nhl', espn_s2=espn_s2, swid=swid, debug=debug) if fetch_league: self.fetch_league() diff --git a/espn_api/requests/espn_requests.py b/espn_api/requests/espn_requests.py index 7e53dbf6..b45b4446 100644 --- a/espn_api/requests/espn_requests.py +++ b/espn_api/requests/espn_requests.py @@ -99,34 +99,36 @@ def get_league_draft(self): } data = self.league_get(params=params) return data - - def authentication(self, username: str, password: str): - url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' - url_login = 'https://ha.registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/guest/login?langPref=en-US' - - # Make request to get the API-Key - headers = {'Content-Type': 'application/json'} - response = requests.post(url_api_key, headers=headers) - if response.status_code != 200 or 'api-key' not in response.headers: - print('Unable to access API-Key') - print('Retry the authentication or continuing without private league access') - return - api_key = response.headers['api-key'] - - # Utilize API-Key and login information to get the swid and s2 keys - headers['authorization'] = 'APIKEY ' + api_key - payload = {'loginValue': username, 'password': password} - response = requests.post(url_login, headers=headers, json=payload) - if response.status_code != 200: - print('Authentication unsuccessful - check username and password input') - print('Retry the authentication or continuing without private league access') - return - data = response.json() - if data['error'] is not None: - print('Authentication unsuccessful - error:' + str(data['error'])) - print('Retry the authentication or continuing without private league access') - return - self.cookies = { - "espn_s2": data['data']['s2'], - "swid": data['data']['profile']['swid'] - } + # Username and password no longer works using their API without using google recaptcha + # Possibly revisit in future if anything changes + + # def authentication(self, username: str, password: str): + # url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' + # url_login = 'https://ha.registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/guest/login?langPref=en-US' + + # # Make request to get the API-Key + # headers = {'Content-Type': 'application/json'} + # response = requests.post(url_api_key, headers=headers) + # if response.status_code != 200 or 'api-key' not in response.headers: + # print('Unable to access API-Key') + # print('Retry the authentication or continuing without private league access') + # return + # api_key = response.headers['api-key'] + + # # Utilize API-Key and login information to get the swid and s2 keys + # headers['authorization'] = 'APIKEY ' + api_key + # payload = {'loginValue': username, 'password': password} + # response = requests.post(url_login, headers=headers, json=payload) + # if response.status_code != 200: + # print('Authentication unsuccessful - check username and password input') + # print('Retry the authentication or continuing without private league access') + # return + # data = response.json() + # if data['error'] is not None: + # print('Authentication unsuccessful - error:' + str(data['error'])) + # print('Retry the authentication or continuing without private league access') + # return + # self.cookies = { + # "espn_s2": data['data']['s2'], + # "swid": data['data']['profile']['swid'] + # } diff --git a/espn_api/wbasketball/league.py b/espn_api/wbasketball/league.py index f0d19238..2fdf6beb 100644 --- a/espn_api/wbasketball/league.py +++ b/espn_api/wbasketball/league.py @@ -15,8 +15,8 @@ class League(BaseLeague): '''Creates a League instance for Public/Private ESPN league''' - def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, username=None, password=None, fetch_league=True, debug=False): - super().__init__(league_id=league_id, year=year, sport='wnba', espn_s2=espn_s2, swid=swid, username=username, password=password, debug=debug) + def __init__(self, league_id: int, year: int, espn_s2=None, swid=None, fetch_league=True, debug=False): + super().__init__(league_id=league_id, year=year, sport='wnba', espn_s2=espn_s2, swid=swid, debug=debug) if fetch_league: self._fetch_league() diff --git a/tests/espn_requests/test_espn_requests.py b/tests/espn_requests/test_espn_requests.py index c98ff1c2..a73eb8c8 100644 --- a/tests/espn_requests/test_espn_requests.py +++ b/tests/espn_requests/test_espn_requests.py @@ -7,45 +7,51 @@ class EspnRequestsTest(TestCase): @requests_mock.Mocker() @mock.patch('sys.stdout', new_callable=io.StringIO) - def test_authentication_api_fail(self, mock_request, mock_stdout): + def test_stub(self, mock_request, mock_stdout): url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' mock_request.post(url_api_key, status_code=400) - request = EspnFantasyRequests(sport='nfl', league_id=1234, year=2019) - request.authentication(username='user', password='pass') - self.assertEqual(mock_stdout.getvalue(), 'Unable to access API-Key\nRetry the authentication or continuing without private league access\n') + + # @requests_mock.Mocker() + # @mock.patch('sys.stdout', new_callable=io.StringIO) + # def test_authentication_api_fail(self, mock_request, mock_stdout): + # url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' + # mock_request.post(url_api_key, status_code=400) + # request = EspnFantasyRequests(sport='nfl', league_id=1234, year=2019) + # request.authentication(username='user', password='pass') + # self.assertEqual(mock_stdout.getvalue(), 'Unable to access API-Key\nRetry the authentication or continuing without private league access\n') - @requests_mock.Mocker() - @mock.patch('sys.stdout', new_callable=io.StringIO) - def test_authentication_login_fail(self, mock_request, mock_stdout): - url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' - url_login = 'https://ha.registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/guest/login?langPref=en-US' - mock_request.post(url_api_key, headers={'api-key':'None'}, status_code=200) - mock_request.post(url_login, status_code=400, json={'eror': 'error'}) + # @requests_mock.Mocker() + # @mock.patch('sys.stdout', new_callable=io.StringIO) + # def test_authentication_login_fail(self, mock_request, mock_stdout): + # url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' + # url_login = 'https://ha.registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/guest/login?langPref=en-US' + # mock_request.post(url_api_key, headers={'api-key':'None'}, status_code=200) + # mock_request.post(url_login, status_code=400, json={'eror': 'error'}) - request = EspnFantasyRequests(sport='nfl', league_id=1234, year=2019) - request.authentication(username='user', password='pass') - self.assertEqual(mock_stdout.getvalue(), 'Authentication unsuccessful - check username and password input\nRetry the authentication or continuing without private league access\n') + # request = EspnFantasyRequests(sport='nfl', league_id=1234, year=2019) + # request.authentication(username='user', password='pass') + # self.assertEqual(mock_stdout.getvalue(), 'Authentication unsuccessful - check username and password input\nRetry the authentication or continuing without private league access\n') - @requests_mock.Mocker() - @mock.patch('sys.stdout', new_callable=io.StringIO) - def test_authentication_login_error(self, mock_request, mock_stdout): - url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' - url_login = 'https://ha.registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/guest/login?langPref=en-US' - mock_request.post(url_api_key, headers={'api-key':'None'}, status_code=200) - mock_request.post(url_login, status_code=200, json={'error': {}}) + # @requests_mock.Mocker() + # @mock.patch('sys.stdout', new_callable=io.StringIO) + # def test_authentication_login_error(self, mock_request, mock_stdout): + # url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' + # url_login = 'https://ha.registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/guest/login?langPref=en-US' + # mock_request.post(url_api_key, headers={'api-key':'None'}, status_code=200) + # mock_request.post(url_login, status_code=200, json={'error': {}}) - request = EspnFantasyRequests(sport='nfl', league_id=1234, year=2019) - request.authentication(username='user', password='pass') - self.assertEqual(mock_stdout.getvalue(), 'Authentication unsuccessful - error:{}\nRetry the authentication or continuing without private league access\n') + # request = EspnFantasyRequests(sport='nfl', league_id=1234, year=2019) + # request.authentication(username='user', password='pass') + # self.assertEqual(mock_stdout.getvalue(), 'Authentication unsuccessful - error:{}\nRetry the authentication or continuing without private league access\n') - @requests_mock.Mocker() - def test_authentication_pass(self, mock_request): - url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' - url_login = 'https://ha.registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/guest/login?langPref=en-US' - mock_request.post(url_api_key, headers={'api-key':'None'}, status_code=200) - mock_request.post(url_login, status_code=200, json={'error': None,'data': {'s2': 'cookie1', 'profile': {'swid': 'cookie2'}}}) + # @requests_mock.Mocker() + # def test_authentication_pass(self, mock_request): + # url_api_key = 'https://registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/api-key?langPref=en-US' + # url_login = 'https://ha.registerdisney.go.com/jgc/v5/client/ESPN-FANTASYLM-PROD/guest/login?langPref=en-US' + # mock_request.post(url_api_key, headers={'api-key':'None'}, status_code=200) + # mock_request.post(url_login, status_code=200, json={'error': None,'data': {'s2': 'cookie1', 'profile': {'swid': 'cookie2'}}}) - request = EspnFantasyRequests(sport='nfl', league_id=1234, year=2019) - request.authentication(username='user', password='pass') - self.assertEqual(request.cookies['espn_s2'], 'cookie1') - self.assertEqual(request.cookies['swid'], 'cookie2') \ No newline at end of file + # request = EspnFantasyRequests(sport='nfl', league_id=1234, year=2019) + # request.authentication(username='user', password='pass') + # self.assertEqual(request.cookies['espn_s2'], 'cookie1') + # self.assertEqual(request.cookies['swid'], 'cookie2') \ No newline at end of file