Skip to content
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
28 changes: 2 additions & 26 deletions applemusicpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import requests
from requests.exceptions import HTTPError
import time
import re


class AppleMusic:
Expand Down Expand Up @@ -778,7 +777,7 @@ def storefronts_all(self, l=None, limit=None, offset=None):
return self._get(url, l=l, limit=limit, offset=offset)

# Search
def search(self, term, storefront='us', l=None, limit=None, offset=None, types=None, hints=False, os='linux'):
def search(self, term, storefront='us', l=None, limit=None, offset=None, types=None, hints=False):
"""
Query the Apple Music API based on a search term

Expand All @@ -789,41 +788,18 @@ def search(self, term, storefront='us', l=None, limit=None, offset=None, types=N
:param offset: The index of the first item returned
:param types: A list of resource types to return (e.g. songs, artists, etc.)
:param hints: Include search hints
:param os: Operating System being used. If search isn't working on Windows, try os='windows'.

:return: The search results in JSON format
"""
url = self.root + 'catalog/{}/search'.format(storefront)
if hints:
url += '/hints'
term = re.sub(' +', '+', term)
if types:
type_str = ','.join(types)
else:
type_str = None

if os == 'linux':
return self._get(url, term=term, l=l, limit=limit, offset=offset, types=type_str)
elif os == 'windows':
params = {
'term': term,
'limit': limit,
'offset': offset,
'types': type_str
}

# The params parameter in requests converts '+' to '%2b'
# On some Windows computers, this breaks the API request, so generate full URL instead
param_string = '?'
for param, value in params.items():
if value is None:
continue
param_string = param_string + str(param) + '=' + str(value) + '&'
param_string = param_string[:len(param_string) - 1] # This removes the last trailing '&'

return self._get(url + param_string)
else:
return None
return self._get(url, term=term, l=l, limit=limit, offset=offset, types=type_str)



Expand Down
4 changes: 0 additions & 4 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ def test_search(self):
results = am.search(self.search_term, types=['songs'])
self.assertTrue(results['results']['songs']['data'][0]['attributes']['name'] == 'Nice For What')

def test_search_windows(self):
results = am.search(self.search_term, types=['songs'], os='windows')
self.assertTrue(results['results']['songs']['data'][0]['attributes']['name'] == 'Nice For What')

def test_charts(self):
results = am.charts(types=['songs'], genre=self.pop)
self.assertTrue(results['results']['songs'][0]['name'] == 'Top Songs')
Expand Down