-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path__init__.py
More file actions
634 lines (527 loc) · 19.9 KB
/
__init__.py
File metadata and controls
634 lines (527 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/usr/bin/python
"""A python interface to search iTunes Store"""
import os
import urllib
import numbers
from six.moves import urllib
import re
try:
import simplejson as json
except ImportError:
import json
try:
from hashlib import md5
except ImportError:
from md5 import md5
__name__ = 'pyitunes'
__doc__ = 'A python interface to search iTunes Store'
__author__ = 'Oscar Celma'
__version__ = '0.2'
__license__ = 'GPL'
__maintainer__ = 'Oscar Celma'
__email__ = 'ocelma@bmat.com'
__status__ = 'Beta'
API_VERSION = '2' # iTunes API version
COUNTRY = 'US' # ISO Country Store
HOST_NAME = 'http://itunes.apple.com/'
__cache_enabled = False # Enable cache? if set to True, make sure that __cache_dir exists! (e.g. $ mkdir ./cache)
__cache_dir = './cache' # Set cache directory
def clean_json(data):
if isinstance(data,bytes):
data =data.decode()
return data.replace('\\\\', r'//').replace(r"\'", '\"').replace(r'\"', '').replace(r'\u','')
class ServiceException(Exception):
"""Exception related to the web service."""
def __init__(self, type, message):
self._type = type
self._message = message
def __str__(self):
return self._type + ': ' + self._message
def get_message(self):
return self._message
def get_type(self):
return self._type
class _Request(object):
"""Representing an abstract web service operation."""
def __init__(self, method_name, params):
self.params = params
self.method = method_name
def _download_response(self):
"""Returns a response"""
data = []
for name in self.params.keys():
value = self.params[name]
if isinstance(value, numbers.Integral) or isinstance(value, float) :
value = str(value)
try:
data.append('='.join((name, urllib.parse.quote_plus(value.replace('&', '&').encode('utf8')))))
except UnicodeDecodeError:
data.append('='.join((name, urllib.parse.quote_plus(value.replace('&', '&')))))
data = '&'.join(data)
url = HOST_NAME
parsed_url = urllib.parse.urlparse(url)
if not parsed_url.scheme:
url = "http://" + url
url += self.method + '?'
url += data
#print url
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
return response.read()
def execute(self, cacheable=False):
try:
if is_caching_enabled() and cacheable:
response = self._get_cached_response()
else:
response = self._download_response()
response = clean_json(response)
return json.loads(response)
except urllib.error.HTTPError as e:
raise self._get_error(e.fp.read())
def _get_cache_key(self):
"""Cache key"""
keys = self.params.keys()[:]
keys.sort()
string = self.method
for name in keys:
string += name
if isinstance(self.params[name], int) or isinstance(self.params[name], float):
self.params[name] = str(self.params[name])
string += self.params[name]
return get_md5(string)
def _is_cached(self):
"""Returns True if the request is available in the cache."""
return os.path.exists(os.path.join(_get_cache_dir(), self._get_cache_key()))
def _get_cached_response(self):
"""Returns a file object of the cached response."""
if not self._is_cached():
response = self._download_response()
response_file = open(os.path.join(_get_cache_dir(), self._get_cache_key()), "w")
response_file.write(response)
response_file.close()
return open(os.path.join(_get_cache_dir(), self._get_cache_key()), "r").read()
def _get_error(self, text):
return ServiceException(type='Error', message=text)
raise
# Webservice BASE OBJECT
class _BaseObject(object):
"""An abstract webservices object."""
def __init__(self, method):
self._method = method
self._search_terms = dict()
def _request(self, method_name=None, params = None, cacheable = False):
if not method_name:
method_name = self._method
if not params:
params = self._get_params()
return _Request(method_name, params).execute(cacheable)
def _get_params(self):
params = {}
for key in self._search_terms.keys():
params[key] = self._search_terms[key]
return params
def get(self):
self._json_results = self._request(cacheable=is_caching_enabled())
if 'errorMessage' in self._json_results:
raise ServiceException(type='Error', message=self._json_results['errorMessage'])
self._num_results = self._json_results['resultCount']
l = []
for json in self._json_results['results']:
type = None
if 'wrapperType' in json:
type = json['wrapperType']
elif 'kind' in json:
type = json['kind']
if type == 'artist':
id = json['artistId']
item = Artist(id)
elif type == 'collection':
id = json['collectionId']
item = Album(id)
elif type == 'track':
id = json['trackId']
item = Track(id)
elif type == 'audiobook':
id = json['collectionId']
item = Audiobook(id)
elif type == 'software':
id = json['trackId']
item = Software(id)
else:
if 'collectionId' in json:
id = json['collectionId']
elif 'artistId' in json:
id = json['artistId']
item = Item(id)
item._set(json)
l.append(item)
return l
# SEARCH
class Search(_BaseObject):
""" Search iTunes Store """
def __init__(self, query, country=COUNTRY, media='all', entity=None,
attribute=None, offset=0, limit=50, order=None,
lang='en_us', version=API_VERSION, explicit='Yes'):
"""
@param order: The results are returned in this order. Possible values
are 'rank' or 'popular.'
@param offset: Return search results starting at this offset. Useful
because there is a cap of 500 results per query.
@param limit: Return no more than this many results. Regardless of what
you specify, iTunes will never return more than 500 results.
"""
_BaseObject.__init__(self, 'search')
self._search_terms = dict()
self._search_terms['term'] = query
self._search_terms['country'] = country # ISO Country code for iTunes Store
self._search_terms['media'] = media # The media type you want to search for
if entity:
self._search_terms['entity'] = entity # The type of results you want returned, relative to the specified media type
if attribute:
self._search_terms['attribute'] = attribute # The attribute you want to search for in the stores, relative to the specified media type
self._search_terms['limit'] = limit # Results limit
if offset > 0:
self._search_terms['offset'] = offset
if order is not None:
self._search_terms['order'] = order
self._search_terms['lang'] = lang # The language, English or Japanese, you want to use when returning search results
self._search_terms['version'] = version # The search result key version you want to receive back from your search
self._search_terms['explicit'] = explicit # A flag indicating whether or not you want to include explicit content in your search results
self._json_results = None
self._num_results = None
def num_results(self):
return self._num_results
# LOOKUP
class Lookup(_BaseObject):
""" Loookup """
def __init__(self, id, entity=None, country=None, limit=50):
_BaseObject.__init__(self, 'lookup')
self.id = id
self._search_terms['id'] = id
if entity:
self._search_terms['entity'] = entity # The type of results you want returned, relative to the specified media type
if country:
self._search_terms['country'] = country # Retrieve localized version of item data
self._search_terms['limit'] = limit # Results limit
# RESULT ITEM
class Item(object):
""" Item result class """
def __init__(self, id):
self.id = id
self.name = None
self.url = None
# JSON SETTERs
def _set(self, json):
self.json = json
#print json
if 'kind' in json:
self.type = json['kind']
else:
self.type = json['wrapperType']
# Item information
self._set_genre(json)
self._set_release(json)
self._set_country(json)
self._set_artwork(json)
self._set_url(json)
def _set_genre(self, json):
self.genre = json.get('primaryGenreName', None)
def _set_release(self, json):
self.release_date = None
if 'releaseDate' in json and json['releaseDate']:
self.release_date = json['releaseDate'].split('T')[0]
def _set_country(self, json):
self.country_store = json.get('country', None)
def _set_artwork(self, json):
self.artwork = dict()
if 'artworkUrl30' in json:
self.artwork['30'] = json['artworkUrl30']
if 'artworkUrl60' in json:
self.artwork['60'] = json['artworkUrl60']
if 'artworkUrl100' in json:
self.artwork['100'] = json['artworkUrl100']
if 'artworkUrl512' in json:
self.artwork['512'] = json['artworkUrl512']
if 'artworkUrl1100' in json:
self.artwork['1100'] = json['artworkUrl1100']
def _set_url(self, json):
self.url = None
if 'trackViewUrl' in json:
self.url = json['trackViewUrl']
elif 'collectionViewUrl' in json:
self.url = json['collectionViewUrl']
elif 'artistViewUrl' in json:
self.url = json['artistViewUrl']
# REPR, EQ, NEQ
def __repr__(self):
if not self.name:
if 'collectionName' in self.json:
self._set_name(self.json['collectionName'])
elif 'artistName' in self.json:
self._set_name(self.json['artistName'])
return self.name
def __eq__(self, other):
return self.id == other.id
def __ne__(self, other):
return self.id != other.id
def _set_name(self, name):
self.name = name
# GETTERs
def get_id(self):
if not self.id:
if 'collectionId' in self.json:
self.id = self.json['collectionId']
elif 'artistId' in self.json:
self.id = self.json['artistId']
return self.id
def get_name(self):
""" Returns the Item's name """
return self.__repr__()
def get_url(self):
""" Returns the iTunes Store URL of the Item """
return self.url
def get_genre(self):
""" Returns the primary genre of the Item """
return self.genre
def get_release_date(self):
""" Returns the release date of the Item """
return self.release_date
def get_artwork(self):
""" Returns the artwork (a dict) of the item """
return self.artwork
def get_tracks(self, limit=500):
""" Returns the tracks of the Item """
if self.type == 'song':
return self
items = Lookup(id=self.id, entity='song', limit=limit).get()
if not items:
raise ServiceException(type='Error', message='Nothing found!')
return items[1:]
def get_albums(self, limit=200):
""" Returns the albums of the Item """
if self.type == 'collection':
return self
if self.type == 'song':
return self.get_album()
items = Lookup(id=self.id, entity='album', limit=limit).get()[1:]
if not items:
raise ServiceException(type='Error', message='Nothing found!')
return items[1:]
def get_album(self):
""" Returns the album of the Item """
if self.type == 'collection':
return self
items = Lookup(id=self.id, entity='album', limit=1).get()
if not items or len(items) == 1:
raise ServiceException(type='Error', message='Nothing found!')
return items[1]
# ARTIST
class Artist(Item):
""" Artist class """
def __init__(self, id):
Item.__init__(self, id)
def _set(self, json):
super(Artist, self)._set(json)
self.name = json['artistName']
self.amg_id = json.get('amgArtistId', None)
self.url = json.get('artistViewUrl', json.get('artistLinkUrl', None))
# GETTERs
def get_amg_id(self):
return self.amg_id
# ALBUM
class Album(Item):
""" Album class """
def __init__(self, id):
Item.__init__(self, id)
def _set(self, json):
super(Album, self)._set(json)
# Collection information
self.name = json['collectionName']
self.url = json.get('collectionViewUrl', None)
self.amg_id = json.get('amgAlbumId', None)
self.price = round(json.get('collectionPrice', 0) or 0, 4)
self.price_currency = json['currency']
self.track_count = json['trackCount']
self.copyright = json.get('copyright', None)
self._set_artist(json)
def _set_artist(self, json):
self.artist = None
if json.get('artistId'):
id = json['artistId']
self.artist = Artist(id)
self.artist._set(json)
# GETTERs
def get_amg_id(self):
return self.amg_id
def get_copyright(self):
return self.copyright
def get_price(self):
return self.price
def get_track_count(self):
return self.track_count
def get_artist(self):
return self.artist
# TRACK
class Track(Item):
""" Track class """
def __init__(self, id):
Item.__init__(self, id)
def _set(self, json):
super(Track, self)._set(json)
# Track information
self.name = json['trackName']
self.url = json.get('trackViewUrl', None)
self.preview_url = json.get('previewUrl', None)
self.price = None
if 'trackPrice' in json and json['trackPrice'] is not None:
self.price = round(json['trackPrice'], 4)
self.number = json.get('trackNumber', None)
self.duration = None
if 'trackTimeMillis' in json and json['trackTimeMillis'] is not None:
self.duration = round(json.get('trackTimeMillis', 0.0)/1000.0, 2)
try:
self._set_artist(json)
except KeyError:
self.artist = None
try:
self._set_album(json)
except KeyError:
self.album = None
def _set_artist(self, json):
self.artist = None
if json.get('artistId'):
id = json['artistId']
self.artist = Artist(id)
self.artist._set(json)
def _set_album(self, json):
if 'collectionId' in json:
id = json['collectionId']
self.album = Album(id)
self.album._set(json)
# GETTERs
def get_preview_url(self):
return self.preview_url
def get_disc_number(self):
return self.number
def get_duration(self):
return self.duration
def get_artist(self):
return self.artist
def get_price(self):
return self.price
# Audiobook
class Audiobook(Album):
""" Audiobook class """
def __init__(self, id):
Album.__init__(self, id)
# Software
class Software(Track):
""" Audiobook class """
def __init__(self, id):
Track.__init__(self, id)
def _set(self, json):
super(Software, self)._set(json)
self._set_version(json)
self._set_price(json)
self._set_description(json)
self._set_screenshots(json)
self._set_genres(json)
self._set_seller_url(json)
self._set_languages(json)
self._set_avg_rating(json)
self._set_num_ratings(json)
def _set_version(self, json):
self.version = json.get('version', None)
def _set_price(self, json):
self.price = json.get('price', None)
def _set_description(self, json):
self.description = json.get('description', None)
def _set_screenshots(self, json):
self.screenshots = json.get('screenshotUrls', None)
def _set_genres(self, json):
self.genres = json.get('genres', None)
def _set_seller_url(self, json):
self.seller_url = json.get('sellerUrl', None)
def _set_languages(self, json):
self.languages = json.get('languageCodesISO2A', None)
def _set_avg_rating(self, json, only_current_version=False):
if only_current_version:
self.avg_rating = json.get('averageUserRatingForCurrentVersion', None)
else:
self.avg_rating = json.get('averageUserRating', None)
def _set_num_ratings(self, json, only_current_version=False):
if only_current_version:
self.num_ratings = json.get('userRatingCountForCurrentVersion', None)
else:
self.num_ratings = json.get('userRatingCount', None)
# GETTERs
def get_version(self):
return self.version
def get_description(self):
return self.description
def get_screenshots(self):
return self.screenshots
def get_genres(self):
return self.genres
def get_seller_url(self):
return self.seller_url
def get_languages(self):
return self.languages
def get_avg_rating(self):
return self.avg_rating
def get_num_ratings(self):
return self.num_ratings
# CACHE
def enable_caching(cache_dir = None):
global __cache_dir
global __cache_enabled
if cache_dir == None:
import tempfile
__cache_dir = tempfile.mkdtemp()
else:
if not os.path.exists(cache_dir):
os.mkdir(cache_dir)
__cache_dir = cache_dir
__cache_enabled = True
def disable_caching():
global __cache_enabled
__cache_enabled = False
def is_caching_enabled():
"""Returns True if caching is enabled."""
global __cache_enabled
return __cache_enabled
def _get_cache_dir():
"""Returns the directory in which cache files are saved."""
global __cache_dir
global __cache_enabled
return __cache_dir
def get_md5(text):
"""Returns the md5 hash of a string."""
hash = md5()
try:
hash.update(text.encode('utf8'))
except UnicodeDecodeError:
hash.update(text)
return hash.hexdigest()
#SEARCHES
def search_track(query, limit=100, offset=0, order=None, store=COUNTRY):
return Search(query=query, media='music', entity='song',
offset=offset, limit=limit, order=order, country=store).get()
def search_album(query, limit=100, offset=0, order=None, store=COUNTRY):
return Search(query=query, media='music', entity='album',
limit=limit, offset=offset, order=order, country=store).get()
def search_artist(query, limit=100, offset=0, order=None, store=COUNTRY):
return Search(query=query, media='music', entity='musicArtist',
limit=limit, offset=offset, order=order, country=store).get()
def search_app(query, limit=100, offset=0, order=None, store=COUNTRY):
return Search(query=query, media='software', limit=limit,
offset=offset, order=order, country=store).get()
def search(query, media='all', limit=100, offset=0, order=None, store=COUNTRY):
return Search(query=query, media=media, limit=limit,
offset=offset, order=order, country=store).get()
# LOOKUP
def lookup(id, entity=None, country=None):
items = Lookup(id, entity=entity, country=country).get()
if not items:
raise ServiceException(type='Error', message='Nothing found!')
return items[0]