Skip to content

Commit 36c1b6b

Browse files
committed
added thumbnails to search resultsi and adding unit tests
1 parent 5500041 commit 36c1b6b

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

spotify_bot.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ def get_auth_token():
4949

5050
return access_token
5151

52+
def get_thumbnail(response):
53+
# check if images exist for search query
54+
if 'images' in response and len(response['images']) > 0:
55+
image = response['images'][0]
56+
thumbnail = image['url']
57+
thumbnail_width = image['width']
58+
thumbnail_height = image['height']
59+
return (thumbnail, thumbnail_width, thumbnail_height)
60+
else:
61+
return (None, None, None)
62+
63+
5264
def search(query, query_type, auth_token):
5365
# replace all spaces with %20 as per Spotify Web API
5466
search_query = query.lower().strip().replace(" ", "%20")
@@ -79,8 +91,11 @@ def search(query, query_type, auth_token):
7991

8092
if len(content_data[query_type + 's']['items']) == 0:
8193
return None
82-
else :
83-
return content_data[query_type + 's']['items'][0]['external_urls']['spotify']
94+
else:
95+
response = content_data[query_type + 's']['items'][0]
96+
spotify_link = response['external_urls']['spotify']
97+
(thumbnail, thumbnail_width, thumbnail_height) = get_thumbnail(response)
98+
return (spotify_link, thumbnail, thumbnail_width, thumbnail_height)
8499

85100
def is_empty_query(query):
86101
return True if query == '' else False
@@ -108,7 +123,10 @@ def inlinequery(bot, update):
108123
if response is not None:
109124
results.append(InlineQueryResultArticle(id=uuid4(),
110125
title=_type,
111-
input_message_content=InputTextMessageContent(response)))
126+
input_message_content=InputTextMessageContent(response[0]),
127+
thumb_url=response[1],
128+
thumb_width=response[2],
129+
thumb_height=response[3]))
112130

113131
# if there are no results, tell user
114132
if check_no_results(results):

tests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,24 @@ def test_empty_results(self):
1919
def test_nonempty_results(self):
2020
self.assertFalse(spotify_bot.check_no_results(self.nonEmptyResults))
2121

22+
def test_get_thumbnail_noThumnail(self):
23+
response = {}
24+
empty_tuple = spotify_bot.get_thumbnail(response)
25+
self.assertTrue(empty_tuple[0] == None
26+
and empty_tuple[1] == None
27+
and empty_tuple[2] == None)
28+
def test_get_thumbnail_thumbnailExists(self):
29+
response = {
30+
'images' : [ {
31+
'url' : 'some_url',
32+
'width' : 123,
33+
'height' : 123
34+
}]
35+
}
36+
thumbnail_info = spotify_bot.get_thumbnail(response)
37+
self.assertTrue(thumbnail_info[0] != None
38+
and thumbnail_info[1] != None
39+
and thumbnail_info[2] != None)
40+
2241
if __name__ == '__main__':
2342
unittest.main()

0 commit comments

Comments
 (0)