1
- from uuid import uuid4
2
-
3
- import os , urllib .request , logging , json , sys , base64
1
+ import os , urllib .request , logging , json , sys , auth
4
2
from telegram .ext import Updater , CommandHandler , MessageHandler , Filters , InlineQueryHandler
5
3
from telegram import InputTextMessageContent , InlineQueryResultArticle
6
4
@@ -17,38 +15,6 @@ def about(bot, update):
17
15
print ("About page selected" )
18
16
bot .sendMessage (update .message .chat_id , text = "This bot has been created by GMCtree using Python and the Python Telegram Bot API created by the Python-Telegram-Bot Team" )
19
17
20
- # get the authorization token to make requests to Spotify API
21
- def get_auth_token ():
22
-
23
- # Check to see which environment to use by reading from config file
24
- with open ("config.json" , "r" ) as config_file :
25
- config = json .load (config_file )
26
- if not config ["prod" ]:
27
- with open ("spotify_token.json" , "r" ) as auth_file :
28
- auth_data = json .load (auth_file )
29
- client_id = auth_data ["client_id" ]
30
- client_secret = auth_data ["client_secret" ]
31
- else :
32
- client_id = os .environ ["CLIENT_ID" ]
33
- client_secret = os .environ ["CLIENT_SECRET" ]
34
-
35
- # Spotify requires base64 encoding for the token
36
- auth_token = client_id + ":" + client_secret
37
- auth_token_encoded = base64 .b64encode (auth_token .encode ("ascii" ))
38
-
39
- request_body = urllib .parse .urlencode ({"grant_type" : "client_credentials" }).encode ()
40
- auth_request = urllib .request .Request ("https://accounts.spotify.com/api/token" , data = request_body )
41
- auth_request .add_header ("Authorization" , "Basic " + auth_token_encoded .decode ())
42
-
43
- try :
44
- auth_response = json .loads (urllib .request .urlopen (auth_request ).read ())
45
- except urllib .error .HTTPError as err :
46
- print (err .read ())
47
-
48
- access_token = auth_response ["access_token" ]
49
-
50
- return access_token
51
-
52
18
def get_thumbnail (response ):
53
19
# check if images exist for search query
54
20
if 'images' in response and len (response ['images' ]) > 0 :
@@ -60,7 +26,6 @@ def get_thumbnail(response):
60
26
else :
61
27
return (None , None , None )
62
28
63
-
64
29
def search (query , query_type , auth_token ):
65
30
# replace all spaces with %20 as per Spotify Web API
66
31
search_query = query .lower ().strip ().replace (" " , "%20" )
@@ -111,7 +76,7 @@ def inlinequery(bot, update):
111
76
results = list ()
112
77
types = ['Track' , 'Artist' , 'Album' , 'Playlist' ]
113
78
114
- auth_token = get_auth_token ()
79
+ auth_token = auth . get_auth_token ()
115
80
116
81
# if empty query, return blank results to prevent unnecessary Spotify API calls
117
82
if is_empty_query (query ):
0 commit comments