From c329ea135ed8c7b56a16e08bf0ee8f6f82609406 Mon Sep 17 00:00:00 2001 From: 0xhtml <34682885+0xhtml@users.noreply.github.com> Date: Wed, 31 Jul 2019 20:44:41 +0200 Subject: [PATCH 1/4] Fix spotify engine --- searx/engines/spotify.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/searx/engines/spotify.py b/searx/engines/spotify.py index aed756be..da32b334 100644 --- a/searx/engines/spotify.py +++ b/searx/engines/spotify.py @@ -12,10 +12,14 @@ from json import loads from searx.url_utils import urlencode +import requests +import base64 # engine dependent config categories = ['music'] paging = True +api_client_id = None +api_client_secret = None # search-url url = 'https://api.spotify.com/' @@ -31,6 +35,16 @@ def request(query, params): params['url'] = search_url.format(query=urlencode({'q': query}), offset=offset) + r = requests.post( + 'https://accounts.spotify.com/api/token', + data={'grant_type': 'client_credentials'}, + headers={'Authorization': 'Basic ' + str(base64.b64encode( + (api_client_id + ":" + api_client_secret).encode('utf-8') + ), 'utf-8')} + ) + j = loads(r.text) + params['headers'] = {'Authorization': 'Bearer ' + j['access_token']} + return params From ae3eeedb14b1f693872787ac67975728c5773b84 Mon Sep 17 00:00:00 2001 From: 0xhtml <34682885+0xhtml@users.noreply.github.com> Date: Wed, 31 Jul 2019 20:51:01 +0200 Subject: [PATCH 2/4] Require Spotify API credentials in settings --- searx/settings.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/searx/settings.yml b/searx/settings.yml index 490b3af0..281aacea 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -590,9 +590,12 @@ engines: shortcut : se categories : science - - name : spotify - engine : spotify - shortcut : stf +# Spotify needs API credentials +# - name : spotify +# engine : spotify +# shortcut : stf +# api_client_id : ******* +# api_client_secret : ******* - name : startpage engine : startpage From 275b37cc7c87b562d08576be5268a4f8797b84ea Mon Sep 17 00:00:00 2001 From: 0xhtml <34682885+0xhtml@users.noreply.github.com> Date: Wed, 31 Jul 2019 21:01:24 +0200 Subject: [PATCH 3/4] Fix error if the user hasn't set api credentials --- searx/engines/spotify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searx/engines/spotify.py b/searx/engines/spotify.py index da32b334..57b08a1e 100644 --- a/searx/engines/spotify.py +++ b/searx/engines/spotify.py @@ -39,7 +39,7 @@ def request(query, params): 'https://accounts.spotify.com/api/token', data={'grant_type': 'client_credentials'}, headers={'Authorization': 'Basic ' + str(base64.b64encode( - (api_client_id + ":" + api_client_secret).encode('utf-8') + "{}:{}".format(api_client_id, api_client_secret).encode('utf-8') ), 'utf-8')} ) j = loads(r.text) From b2e1ee8d35050033b41765a2de49c0eea5f8b4b4 Mon Sep 17 00:00:00 2001 From: 0xhtml <34682885+0xhtml@users.noreply.github.com> Date: Wed, 31 Jul 2019 21:09:02 +0200 Subject: [PATCH 4/4] Fix some more errors with none/wrong credentials --- searx/engines/spotify.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/searx/engines/spotify.py b/searx/engines/spotify.py index 57b08a1e..00c39570 100644 --- a/searx/engines/spotify.py +++ b/searx/engines/spotify.py @@ -38,12 +38,12 @@ def request(query, params): r = requests.post( 'https://accounts.spotify.com/api/token', data={'grant_type': 'client_credentials'}, - headers={'Authorization': 'Basic ' + str(base64.b64encode( + headers={'Authorization': 'Basic ' + base64.b64encode( "{}:{}".format(api_client_id, api_client_secret).encode('utf-8') - ), 'utf-8')} + ).decode('utf-8')} ) j = loads(r.text) - params['headers'] = {'Authorization': 'Bearer ' + j['access_token']} + params['headers'] = {'Authorization': 'Bearer {}'.format(j.get('access_token'))} return params