Skip to content

Commit

Permalink
Merge pull request #39 from redsudo/develop
Browse files Browse the repository at this point in the history
Bug fixes (auth, playlist pagination, file name length)
  • Loading branch information
redsudo authored May 7, 2019
2 parents 44320b5 + b142f10 commit 2134950
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
2 changes: 2 additions & 0 deletions redsea/mediadownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def download_media(self, track_info, quality, album_info=None):
**self._normalise_info(track_info, album_info, True)))
track_file = self.opts['track_format'].format(
**self._normalise_info(track_info, album_info))
if len(track_file) > 255: # trim filename to be under OS limit (and account for file extension)
track_file = track_file[:250 - len(track_file)]
_mkdir_p(album_location)
# Make multi disc directories
if album_info['numberOfVolumes'] > 1:
Expand Down
66 changes: 35 additions & 31 deletions redsea/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,41 @@ def new_session(self):
Returns True if successful
'''

print('LOGIN: Enter your Tidal username and password:\n')
username = input('Username: ')
password = getpass.getpass('Password: ')

name = ''
while name == '':
name = input('What would you like to call this new session? ')
if not name == '':
if name in self.sessions:
confirm = input('A session with name "{}" already exists. Overwrite [y/N]? '.format(name))
while True:
print('LOGIN: Enter your Tidal username and password:\n')
username = input('Username: ')
password = getpass.getpass('Password: ')

name = ''
while name == '':
name = input('What would you like to call this new session? ')
if not name == '':
if name in self.sessions:
confirm = input('A session with name "{}" already exists. Overwrite [y/N]? '.format(name))
if confirm.upper() == 'Y':
super().remove(name)
else:
name = ''
continue
else:
confirm = input('Invalid entry! Would you like to cancel [y/N]? ')
if confirm.upper() == 'Y':
super().remove(name)
else:
name = ''
print('Operation cancelled.')
return False

try:
super().new_session(name, username, password)
break
except TidalRequestError as e:
if str(e).startswith('3001'):
print('\nUSERNAME OR PASSWORD INCORRECT. Please try again.\n\n')
continue
except AssertionError as e:
if 'invalid sessionId' in str(e):
print(e)
confirm = input('Would you like to try again [Y/n]? ')
if not confirm.upper() == 'N':
continue
else:
confirm = input('Invalid entry! Would you like to cancel [y/N]? ')
if confirm.upper() == 'Y':
print('Operation cancelled.')
return False

try:
super().new_session(name, username, password)
except TidalRequestError as e:
if str(e).startswith('3001'):
print('\nUSERNAME OR PASSWORD INCORRECT. Please try again.\n\n')
self.new_session()
except AssertionError as e:
if 'invalid sessionId' in str(e):
print(e)
confirm = input('Would you like to try again [Y/n]? ')
if not confirm.upper() == 'N':
self.new_session()

print('Session saved!')
if not self.default == name:
Expand Down Expand Up @@ -102,6 +104,7 @@ def remove_session(self):
name = input('Type the full name of the session you would like to remove: ')
if not name == '':
super().remove(name)
print('Session "{}" has been removed.'.format(name))
else:
confirm = input('Invalid entry! Would you like to cancel [y/N]? ')
if confirm.upper() == 'Y':
Expand Down Expand Up @@ -162,6 +165,7 @@ def reauth(self):
print('LOGIN: Enter your Tidal password for account {}:\n'.format(session.username))
password = getpass.getpass('Password: ')
session.auth(password)
self._save()

print('Session "{}" has been successfully reauthed.'.format(name))
return
Expand Down
22 changes: 20 additions & 2 deletions redsea/tidal_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,28 @@ def get_stream_url(self, track_id, quality):
{'soundQuality': quality})

def get_playlist_items(self, playlist_id):
return self._get('playlists/' + playlist_id + '/items', {
result = self._get('playlists/' + playlist_id + '/items', {
'offset': 0,
'limit': 100
})

if (result['totalNumberOfItems'] <= 100):
return result

offset = len(result['items'])
while True:
buf = self._get('playlists/' + playlist_id + '/items', {
'offset': offset,
'limit': 100
})
offset += len(buf['items'])
result['items'] += buf['items']

if offset >= result['totalNumberOfItems']:
break

return result

def get_album_tracks(self, album_id):
return self._get('albums/' + str(album_id) + '/tracks')

Expand Down Expand Up @@ -141,8 +158,9 @@ def valid(self):
'''
Checks if session is still valid and returns True/False
'''

r = requests.get(self.TIDAL_API_BASE + 'users/' + str(self.user_id),
params={'sessionId': self.session_id}).json()
headers={'X-Tidal-SessionId': self.session_id}).json()

if 'status' in r and not r['status'] == 200:
return False
Expand Down

0 comments on commit 2134950

Please sign in to comment.