diff --git a/itunes/__init__.py b/itunes/__init__.py index 4145b78..b4265a9 100644 --- a/itunes/__init__.py +++ b/itunes/__init__.py @@ -1,8 +1,9 @@ #!/usr/bin/python """A python interface to search iTunes Store""" import os -import urllib2, urllib -import urlparse +import urllib +import numbers +from six.moves import urllib import re try: import simplejson as json @@ -32,6 +33,8 @@ def clean_json(data): + if isinstance(data,bytes): + data =data.decode() return data.replace('\\\\', r'//').replace(r"\'", '\"').replace(r'\"', '').replace(r'\u','') @@ -63,24 +66,24 @@ def _download_response(self): data = [] for name in self.params.keys(): value = self.params[name] - if isinstance(value, int) or isinstance(value, float) or isinstance(value, long): + if isinstance(value, numbers.Integral) or isinstance(value, float) : value = str(value) try: - data.append('='.join((name, urllib.quote_plus(value.replace('&', '&').encode('utf8'))))) + data.append('='.join((name, urllib.parse.quote_plus(value.replace('&', '&').encode('utf8'))))) except UnicodeDecodeError: - data.append('='.join((name, urllib.quote_plus(value.replace('&', '&'))))) + data.append('='.join((name, urllib.parse.quote_plus(value.replace('&', '&'))))) data = '&'.join(data) url = HOST_NAME - parsed_url = urlparse.urlparse(url) + parsed_url = urllib.parse.urlparse(url) if not parsed_url.scheme: url = "http://" + url url += self.method + '?' url += data #print url - request = urllib2.Request(url) - response = urllib2.urlopen(request) + request = urllib.request.Request(url) + response = urllib.request.urlopen(request) return response.read() def execute(self, cacheable=False): @@ -91,7 +94,7 @@ def execute(self, cacheable=False): response = self._download_response() response = clean_json(response) return json.loads(response) - except urllib2.HTTPError, e: + except urllib.error.HTTPError as e: raise self._get_error(e.fp.read()) def _get_cache_key(self): @@ -146,15 +149,15 @@ def _get_params(self): def get(self): self._json_results = self._request(cacheable=is_caching_enabled()) - if self._json_results.has_key('errorMessage'): + 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 json.has_key('wrapperType'): + if 'wrapperType' in json: type = json['wrapperType'] - elif json.has_key('kind'): + elif 'kind' in json: type = json['kind'] if type == 'artist': @@ -173,9 +176,9 @@ def get(self): id = json['trackId'] item = Software(id) else: - if json.has_key('collectionId'): + if 'collectionId' in json: id = json['collectionId'] - elif json.has_key('artistId'): + elif 'artistId' in json: id = json['artistId'] item = Item(id) item._set(json) @@ -252,7 +255,7 @@ def __init__(self, id): def _set(self, json): self.json = json #print json - if json.has_key('kind'): + if 'kind' in json: self.type = json['kind'] else: self.type = json['wrapperType'] @@ -268,7 +271,7 @@ def _set_genre(self, json): def _set_release(self, json): self.release_date = None - if json.has_key('releaseDate') and json['releaseDate']: + if 'releaseDate' in json and json['releaseDate']: self.release_date = json['releaseDate'].split('T')[0] def _set_country(self, json): @@ -276,34 +279,34 @@ def _set_country(self, json): def _set_artwork(self, json): self.artwork = dict() - if json.has_key('artworkUrl30'): + if 'artworkUrl30' in json: self.artwork['30'] = json['artworkUrl30'] - if json.has_key('artworkUrl60'): + if 'artworkUrl60' in json: self.artwork['60'] = json['artworkUrl60'] - if json.has_key('artworkUrl100'): + if 'artworkUrl100' in json: self.artwork['100'] = json['artworkUrl100'] - if json.has_key('artworkUrl512'): + if 'artworkUrl512' in json: self.artwork['512'] = json['artworkUrl512'] - if json.has_key('artworkUrl1100'): + if 'artworkUrl1100' in json: self.artwork['1100'] = json['artworkUrl1100'] def _set_url(self, json): self.url = None - if json.has_key('trackViewUrl'): + if 'trackViewUrl' in json: self.url = json['trackViewUrl'] - elif json.has_key('collectionViewUrl'): + elif 'collectionViewUrl' in json: self.url = json['collectionViewUrl'] - elif json.has_key('artistViewUrl'): + elif 'artistViewUrl' in json: self.url = json['artistViewUrl'] # REPR, EQ, NEQ def __repr__(self): if not self.name: - if self.json.has_key('collectionName'): + if 'collectionName' in self.json: self._set_name(self.json['collectionName']) - elif self.json.has_key('artistName'): + elif 'artistName' in self.json: self._set_name(self.json['artistName']) - return self.name.encode('utf8') + return self.name def __eq__(self, other): return self.id == other.id @@ -317,9 +320,9 @@ def _set_name(self, name): # GETTERs def get_id(self): if not self.id: - if self.json.has_key('collectionId'): + if 'collectionId' in self.json: self.id = self.json['collectionId'] - elif self.json.has_key('artistId'): + elif 'artistId' in self.json: self.id = self.json['artistId'] return self.id @@ -444,11 +447,11 @@ def _set(self, json): self.url = json.get('trackViewUrl', None) self.preview_url = json.get('previewUrl', None) self.price = None - if json.has_key('trackPrice') and json['trackPrice'] is not 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 json.has_key('trackTimeMillis') and json['trackTimeMillis'] is not 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) @@ -467,7 +470,7 @@ def _set_artist(self, json): self.artist._set(json) def _set_album(self, json): - if json.has_key('collectionId'): + if 'collectionId' in json: id = json['collectionId'] self.album = Album(id) self.album._set(json) diff --git a/itunes/utils.py b/itunes/utils.py new file mode 100644 index 0000000..419b804 --- /dev/null +++ b/itunes/utils.py @@ -0,0 +1,27 @@ +__author__ = 'tal' +def makePrecise(query,results): + ''' + :param query: the query searched for + :param results: the result set returned by the itunes api + :return:results that are an exact match + ''' + f =filter(lambda x: x.name==query,results) + return [res for res in f] + +def filterOn(key,val,results,case_insensitive=False,substring=False): + ''' + Allows filtering of itunes results by a second field. (Can be applied multiple times) + Useful when your retreive a track by name and want to filter by artist + :param key: The key you want to filter by + :param val: The value that key should be + :param results: The results set returned by itunes + :param case_insensitive: Case insensitive filter + :param substring: if True, check if the value is a substring of the field + :return:The filtered result set + ''' + lower = (lambda x: x.lower()) if case_insensitive is True else (lambda x: x) + match = (lambda x,y: x in y) if substring is True else (lambda x,y: x==y) + val = lower(val) + print (val) + f =filter(lambda x: match(val,lower(x.__getattribute__(key).name)),results) + return [res for res in f] \ No newline at end of file diff --git a/setup.py b/setup.py index 0b542e1..f2af0dc 100644 --- a/setup.py +++ b/setup.py @@ -18,4 +18,8 @@ platforms = ["any"], url="https://github.com/ocelma/python-itunes", packages=['itunes'], + install_requires=[ + 'six', + ], + ) diff --git a/tests/tests.py b/tests/tests.py index be4b7c0..b010ba7 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -7,12 +7,13 @@ U2_ACHTUNGBABY = 'Achtung Baby (Deluxe Edition) [Remastered]' # 'Achtung Baby' U2_ONE_ID = 475391315 # Before it was 368617 -U2_ACHTUNGBABY_ID = 475390461 # Before it was 368713 +U2_ACHTUNGBABY_ID = 475387108 # Before it was 368713 U2_ID = 78500 U2_URL = 'http://itunes.apple.com/us/artist/u2/id%s?uo=4' % U2_ID U2_ACHTUNGBABY_URL = 'http://itunes.apple.com/us/album/achtung-baby-deluxe-edition/id%s?uo=4' % U2_ACHTUNGBABY_ID U2_ONE_URL = 'http://itunes.apple.com/us/album/one/id%s?i=%s&uo=4' % (U2_ACHTUNGBABY_ID, U2_ONE_ID) +U2_ONE_URL_HTTPS_ = 'https://itunes.apple.com/us/album/one/id%s?i=%s&uo=4' % (U2_ACHTUNGBABY_ID, U2_ONE_ID) #SEARCHES def test_search_track(): @@ -27,7 +28,8 @@ def test_search_artist(): def test_search_artist_store(): U2_URL_ES = 'http://itunes.apple.com/es/artist/u2/id78500?l=en&uo=4' assert_equal(itunes.search_artist('u2', store='ES')[0].get_id(), U2_ID) - assert_equal(itunes.search_artist('u2', store='ES')[0].get_url(), U2_URL_ES) + url =itunes.search_artist('u2', store='ES')[0].get_url().replace('https','http') + assert_equal(url, U2_URL_ES) #LOOKUPS def test_lookup_track(): @@ -60,15 +62,18 @@ def test_lookup_notfound(): #METHODS def test_artist_url(): item = itunes.lookup(U2_ID) - assert_equal(item.get_url(), U2_URL) + url = item.get_url().replace('https','http') + assert_equal(url, U2_URL) def test_album_url(): item = itunes.lookup(U2_ACHTUNGBABY_ID) - assert_equal(item.get_url(), U2_ACHTUNGBABY_URL) + url = item.get_url().replace('https','http') + assert_equal(url, U2_ACHTUNGBABY_URL) def test_track_url(): item = itunes.lookup(U2_ONE_ID) - assert_equal(item.get_url(), U2_ONE_URL) + url = item.get_url().replace('https','http') + assert_equal(url, U2_ONE_URL) def test_album_length(): item = itunes.lookup(U2_ACHTUNGBABY_ID)