Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python3.x support #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions itunes/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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','')


Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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':
Expand All @@ -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)
Expand Down Expand Up @@ -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']
Expand All @@ -268,42 +271,42 @@ 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):
self.country_store = json.get('country', None)

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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions itunes/utils.py
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
platforms = ["any"],
url="https://github.com/ocelma/python-itunes",
packages=['itunes'],
install_requires=[
'six',
],

)
15 changes: 10 additions & 5 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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)
Expand Down