Skip to content

Commit 7dd137f

Browse files
committed
updated how results are displayed and added unit tests
1 parent 2ce599c commit 7dd137f

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

spotify_bot.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,33 @@ def search(query, query_type, auth_token):
6060
return None
6161
else:
6262
response = content_data[query_type + 's']['items'][0]
63-
spotify_link = response['external_urls']['spotify']
64-
(thumbnail, thumbnail_width, thumbnail_height) = get_thumbnail(response)
65-
return (spotify_link, thumbnail, thumbnail_width, thumbnail_height)
63+
return response
6664

6765
def is_empty_query(query):
6866
return True if query == '' else False
6967

7068
def check_no_results(results):
7169
return True if len(results) == 0 else False
7270

71+
def build_inline_query_result_article(_type, response):
72+
(thumb_url, thumb_width, thumb_height) = (None, None, None)
73+
# use the album art of the track for the thumbnail
74+
if _type == 'Track':
75+
(thumb_url, thumb_width, thumb_height) = get_thumbnail(response['album'])
76+
else:
77+
(thumb_url, thumb_width, thumb_height) = get_thumbnail(response)
78+
79+
spotify_link = response['external_urls']['spotify']
80+
name = response['name']
81+
query_result_article = InlineQueryResultArticle(id=uuid4(),
82+
title=_type + ' - ' + name,
83+
input_message_content=InputTextMessageContent(spotify_link),
84+
thumb_url=thumb_url,
85+
thumb_width=thumb_width,
86+
thumb_height=thumb_height)
87+
88+
return query_result_article
89+
7390
# main function to handle all inline queries
7491
def inlinequery(bot, update):
7592

@@ -88,12 +105,7 @@ def inlinequery(bot, update):
88105
for _type in types:
89106
response = search(query, _type.lower(), auth_token)
90107
if response is not None:
91-
results.append(InlineQueryResultArticle(id=uuid4(),
92-
title=_type,
93-
input_message_content=InputTextMessageContent(response[0]),
94-
thumb_url=response[1],
95-
thumb_width=response[2],
96-
thumb_height=response[3]))
108+
results.append(build_inline_query_result_article(_type, response))
97109

98110
# if there are no results, tell user
99111
if check_no_results(results):

tests.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,54 @@ def test_get_thumbnail_noThumnail(self):
2525
self.assertTrue(empty_tuple[0] == None
2626
and empty_tuple[1] == None
2727
and empty_tuple[2] == None)
28+
2829
def test_get_thumbnail_thumbnailExists(self):
2930
response = {
30-
'images' : [ {
31-
'url' : 'some_url',
32-
'width' : 123,
33-
'height' : 123
34-
}]
31+
'images' : [ {
32+
'url' : 'some_url',
33+
'width' : 123,
34+
'height' : 123
35+
}]
3536
}
3637
thumbnail_info = spotify_bot.get_thumbnail(response)
3738
self.assertTrue(thumbnail_info[0] != None
3839
and thumbnail_info[1] != None
3940
and thumbnail_info[2] != None)
4041

42+
def test_build_inline_query_article_track_thumbnail(self):
43+
_type = 'Track'
44+
response = {
45+
'album' : {
46+
'images' : [ {
47+
'url' : 'some_url',
48+
'width' : 123,
49+
'height' : 123
50+
}]
51+
},
52+
'external_urls': {
53+
'spotify' : 'some_spotify_link'
54+
},
55+
'name' : 'some_name'
56+
}
57+
inlineQuery = spotify_bot.build_inline_query_result_article(_type, response)
58+
self.assertIsNotNone(inlineQuery.thumb_url)
59+
60+
def test_build_inline_query_article_not_track_thumbnail(self):
61+
_type = 'Album'
62+
response = {
63+
'images' : [{
64+
'url' : 'some_url',
65+
'width' : 123,
66+
'height' : 123
67+
}],
68+
'external_urls': {
69+
'spotify' : 'some_spotify_link'
70+
},
71+
'name' : 'some_name'
72+
}
73+
inlineQuery = spotify_bot.build_inline_query_result_article(_type, response)
74+
self.assertIsNotNone(inlineQuery.thumb_url)
75+
76+
4177
if __name__ == '__main__':
4278
unittest.main()

0 commit comments

Comments
 (0)