Skip to content

Commit edf9330

Browse files
committed
refactored authentication to separate file
1 parent 68f248b commit edf9330

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

auth.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from uuid import uuid4
2+
import json, os, urllib.request, base64
3+
4+
# get the authorization token to make requests to Spotify API
5+
def get_auth_token():
6+
7+
# Check to see which environment to use by reading from config file
8+
with open("config.json", "r") as config_file:
9+
config = json.load(config_file)
10+
if not config["prod"]:
11+
with open("spotify_token.json", "r") as auth_file:
12+
auth_data = json.load(auth_file)
13+
client_id = auth_data["client_id"]
14+
client_secret = auth_data["client_secret"]
15+
else:
16+
client_id = os.environ["CLIENT_ID"]
17+
client_secret = os.environ["CLIENT_SECRET"]
18+
19+
# Spotify requires base64 encoding for the token
20+
auth_token = client_id + ":" + client_secret
21+
auth_token_encoded = base64.b64encode(auth_token.encode("ascii"))
22+
23+
request_body = urllib.parse.urlencode({"grant_type": "client_credentials"}).encode()
24+
auth_request = urllib.request.Request("https://accounts.spotify.com/api/token", data=request_body)
25+
auth_request.add_header("Authorization", "Basic " + auth_token_encoded.decode())
26+
27+
try:
28+
auth_response = json.loads(urllib.request.urlopen(auth_request).read())
29+
except urllib.error.HTTPError as err:
30+
print(err.read())
31+
32+
access_token = auth_response["access_token"]
33+
34+
return access_token
35+

spotify_bot.py

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from uuid import uuid4
2-
3-
import os, urllib.request, logging, json, sys, base64
1+
import os, urllib.request, logging, json, sys, auth
42
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, InlineQueryHandler
53
from telegram import InputTextMessageContent, InlineQueryResultArticle
64

@@ -17,38 +15,6 @@ def about(bot, update):
1715
print ("About page selected")
1816
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")
1917

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-
5218
def get_thumbnail(response):
5319
# check if images exist for search query
5420
if 'images' in response and len(response['images']) > 0:
@@ -60,7 +26,6 @@ def get_thumbnail(response):
6026
else:
6127
return (None, None, None)
6228

63-
6429
def search(query, query_type, auth_token):
6530
# replace all spaces with %20 as per Spotify Web API
6631
search_query = query.lower().strip().replace(" ", "%20")
@@ -111,7 +76,7 @@ def inlinequery(bot, update):
11176
results = list()
11277
types = ['Track', 'Artist', 'Album', 'Playlist']
11378

114-
auth_token = get_auth_token()
79+
auth_token = auth.get_auth_token()
11580

11681
# if empty query, return blank results to prevent unnecessary Spotify API calls
11782
if is_empty_query(query):

0 commit comments

Comments
 (0)