Skip to content

Commit 8ab4fa1

Browse files
committed
Style and warning fixes
1 parent 60f43cf commit 8ab4fa1

3 files changed

Lines changed: 43 additions & 41 deletions

File tree

tests/test_api.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def session():
3333
session.login(username, password)
3434
return session
3535

36-
def test_artist(session):
36+
def test_get_artist(session):
3737
artist_id = 16147
3838
artist = session.get_artist(artist_id)
3939
assert artist.id == artist_id
@@ -55,7 +55,7 @@ def test_get_artist_videos(session):
5555
videos = session.get_artist_videos(3502112)
5656
assert any([v.name == 'Call on Me' for v in videos])
5757

58-
def test_album(session):
58+
def test_get_album(session):
5959
album_id = 17927863
6060
album = session.get_album(album_id)
6161
assert album.id == album_id
@@ -77,7 +77,6 @@ def test_get_album_tracks(session):
7777
assert tracks[-1].track_num == 13
7878
assert tracks[-1].duration == 210
7979

80-
8180
def test_get_album_videos(session):
8281
videos = session.get_album_videos(108046179)
8382
assert videos[0].name == 'Formation (Choreography Version)'
@@ -112,22 +111,25 @@ def test_search(session):
112111
def test_artist_picture(session):
113112
artist = session.get_artist(16147)
114113
assert requests.get(artist.picture(640,640)).status_code == 200
114+
assert requests.get(tidalapi.models.Artist.image.fget(artist, 640, 640)).status_code == 200
115115

116116
def test_album_picture(session):
117117
album = session.get_album(17925106)
118118
assert requests.get(album.picture(640, 640)).status_code == 200
119+
assert requests.get(tidalapi.models.Album.image.fget(album, 640, 640)).status_code == 200
119120

120121
def test_playlist_picture(session):
121122
playlist = session.get_playlist('33136f5a-d93a-4469-9353-8365897aaf94')
122123
assert requests.get(playlist.picture(750, 750)).status_code == 200
124+
assert requests.get(tidalapi.models.Playlist.image.fget(playlist, 750, 750)).status_code == 200
123125

124126
def test_get_track_url(session):
125127
track = session.get_track(108043415)
126-
newurl = session.get_track_url(track.id)
128+
session.get_track_url(track.id)
127129

128130
def test_get_video_url(session):
129131
video = session.get_video(108046194)
130-
newurl = session.get_video_url(video.id)
132+
session.get_video_url(video.id)
131133

132134
def test_load_session(session):
133135
"""

tidalapi/__init__.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from __future__ import unicode_literals
20+
from collections import namedtuple
21+
from enum import Enum
2022

2123
import datetime
2224
import json
2325
import logging
2426
import requests
25-
from collections import namedtuple
2627
from .models import Artist, Album, Track, Video, Playlist, SearchResult, Category, Role
2728
try:
2829
from urlparse import urljoin
@@ -32,23 +33,21 @@
3233

3334
log = logging.getLogger(__name__)
3435

35-
Api = namedtuple('API', ['location', 'token'])
36-
3736

38-
class Quality(object):
37+
class Quality(Enum):
3938
lossless = 'LOSSLESS'
4039
high = 'HIGH'
4140
low = 'LOW'
4241

43-
class videoQuality(object):
42+
class VideoQuality(Enum):
4443
high = 'HIGH'
4544
medium = 'MEDIUM'
4645
low = 'LOW'
4746

4847
class Config(object):
49-
def __init__(self, quality=Quality.high, videoQuality=videoQuality.high):
48+
def __init__(self, quality=Quality.high, video_quality=VideoQuality.high):
5049
self.quality = quality
51-
self.videoQuality = videoQuality
50+
self.video_quality = video_quality
5251
self.api_location = 'https://api.tidalhifi.com/v1/'
5352
self.api_token = 'kgsOOmYk3zShYrNP'
5453

@@ -77,13 +76,13 @@ def login(self, username, password):
7776
'username': username,
7877
'password': password,
7978
}
80-
r = requests.post(url, data=payload, params=params)
79+
request = requests.post(url, data=payload, params=params)
8180

82-
if not (r.ok):
83-
print(r.text)
84-
r.raise_for_status()
81+
if not request.ok:
82+
print(request.text)
83+
request.raise_for_status()
8584

86-
body = r.json()
85+
body = request.json()
8786
self.session_id = body['sessionId']
8887
self.country_code = body['countryCode']
8988
self.user = User(self, id=body['userId'])
@@ -105,12 +104,12 @@ def request(self, method, path, params=None, data=None):
105104
if params:
106105
request_params.update(params)
107106
url = urljoin(self._config.api_location, path)
108-
r = requests.request(method, url, params=request_params, data=data)
109-
log.debug("request: %s" % r.request.url)
110-
r.raise_for_status()
111-
if r.content:
112-
log.debug("response: %s" % json.dumps(r.json(), indent=4))
113-
return r
107+
request = requests.request(method, url, params=request_params, data=data)
108+
log.debug("request: %s", request.request.url)
109+
request.raise_for_status()
110+
if request.content:
111+
log.debug("response: %s", json.dumps(request.json(), indent=4))
112+
return request
114113

115114
def get_user(self, user_id):
116115
return self._map_request('users/%s' % user_id, ret='user')
@@ -198,7 +197,7 @@ def get_track(self, track_id):
198197
return self._map_request('tracks/%s' % track_id, ret='track')
199198

200199
def get_video(self, video_id):
201-
return self._map_request('videos/%s' % video_id, ret = 'video')
200+
return self._map_request('videos/%s' % video_id, ret='video')
202201

203202
def _map_request(self, url, params=None, ret=None):
204203
json_obj = self.request('GET', url, params).json()
@@ -221,16 +220,18 @@ def _map_request(self, url, params=None, ret=None):
221220
items = json_obj.get('items')
222221
if items is None:
223222
return parse(json_obj)
224-
elif len(items) > 0 and 'item' in items[0]:
223+
if len(items) > 0 and 'item' in items[0]:
225224
return list(map(parse, [item['item'] for item in items]))
226-
else:
227-
return list(map(parse, items))
225+
return list(map(parse, items))
228226

229-
def _get_items(self, url, ret=None):
227+
def _get_items(self, url, ret=None, offset=0):
228+
params = {
229+
'offset': offset,
230+
'limit': 100
231+
}
230232
remaining = 100
231-
offset = 0
232-
while remaining is 100:
233-
items = self._map_request(url, params={'offset': offset, 'limit': 100}, ret=ret)
233+
while remaining == 100:
234+
items = self._map_request(url, params=params, ret=ret)
234235
remaining = len(items)
235236
return items
236237

@@ -245,11 +246,11 @@ def get_track_url(self, track_id):
245246
def get_video_url(self, video_id):
246247
params = {
247248
'urlusagemode': 'STREAM',
248-
'videoquality': self._config.videoQuality,
249+
'videoquality': self._config.video_quality,
249250
'assetpresentation': 'FULL'
250251
}
251-
r = self.request('GET', 'videos/%s/urlpostpaywall' % video_id, params)
252-
return r.json()['urls'][0]
252+
request = self.request('GET', 'videos/%s/urlpostpaywall' % video_id, params)
253+
return request.json()['urls'][0]
253254

254255
def search(self, field, value):
255256
params = {
@@ -268,7 +269,7 @@ def search(self, field, value):
268269
def _parse_artist(json_obj):
269270
roles = []
270271
for role in json_obj.get('artistTypes', [json_obj.get('type')]):
271-
roles.append(Role(role))
272+
roles.append(Role(role))
272273

273274
return Artist(id=json_obj['id'], name=json_obj['name'], roles=roles, role=roles[0])
274275

@@ -324,7 +325,7 @@ def _parse_media(json_obj):
324325
artist = _parse_artist(json_obj['artist'])
325326
artists = _parse_artists(json_obj['artists'])
326327
album = None
327-
if (json_obj['album']):
328+
if json_obj['album']:
328329
album = _parse_album(json_obj['album'], artist, artists)
329330

330331
kwargs = {
@@ -343,8 +344,7 @@ def _parse_media(json_obj):
343344

344345
if kwargs['type'] == 'Music Video':
345346
return Video(**kwargs)
346-
else:
347-
return Track(**kwargs)
347+
return Track(**kwargs)
348348

349349
def _parse_genres(json_obj):
350350
image = "http://resources.wimpmusic.com/images/%s/460x306.jpg" \
@@ -392,8 +392,8 @@ def playlists(self):
392392
return self._session._map_request(self._base_url + '/playlists', ret='playlists')
393393

394394
def tracks(self):
395-
r = self._session.request('GET', self._base_url + '/tracks')
396-
return [_parse_media(item['item']) for item in r.json()['items']]
395+
request = self._session.request('GET', self._base_url + '/tracks')
396+
return [_parse_media(item['item']) for item in request.json()['items']]
397397

398398

399399
class User(object):

tidalapi/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Artist(Model):
6262

6363
@property
6464
def image(self, width=1280, height=1280):
65-
return IMG_URL.format(width=height, height=height, id=self.id, id_type='artistid')
65+
return IMG_URL.format(width=width, height=height, id=self.id, id_type='artistid')
6666

6767
def picture(self, width, height):
6868
"""

0 commit comments

Comments
 (0)