Skip to content

Commit

Permalink
Merge pull request #348 from cwendt94/remove_password_login
Browse files Browse the repository at this point in the history
Remove username and password
  • Loading branch information
cwendt94 authored Jul 31, 2022
2 parents 4a37b81 + bda5ad7 commit 1c54b0d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 81 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 1 addition & 3 deletions espn_api/base_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, )
Expand Down
4 changes: 2 additions & 2 deletions espn_api/baseball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions espn_api/basketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions espn_api/football/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions espn_api/hockey/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
64 changes: 33 additions & 31 deletions espn_api/requests/espn_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
# }
4 changes: 2 additions & 2 deletions espn_api/wbasketball/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
74 changes: 40 additions & 34 deletions tests/espn_requests/test_espn_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
# 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')

0 comments on commit 1c54b0d

Please sign in to comment.