@@ -74,6 +74,49 @@ def _search(text, _type, reply):
7474 return request .json ()[TYPE_MAP [_type ]]["items" ][0 ]
7575
7676
77+ def _do_format (data , _type ):
78+ name = data ["name" ]
79+ if _type == "track" :
80+ artist = data ["artists" ][0 ]["name" ]
81+ album = data ["album" ]["name" ]
82+
83+ return "Spotify Track" , "\x02 {}\x02 by \x02 {}\x02 from the album \x02 {}\x02 " .format (name , artist , album )
84+ elif _type == "artist" :
85+ return "Spotify Artist" , "\x02 {}\x02 , followers: \x02 {}\x02 , genres: \x02 {}\x02 " .format (
86+ name , data ["followers" ]["total" ], ', ' .join (data ["genres" ])
87+ )
88+ elif _type == "album" :
89+ return "Spotify Album" , "\x02 {}\x02 - \x02 {}\x02 " .format (data ["artists" ][0 ]["name" ], name )
90+ else :
91+ raise ValueError ("Attempt to format unknown Spotify API type: " + _type )
92+
93+
94+ def _format_response (data , _type , show_pre = False , show_url = False , show_uri = False ):
95+ pre , text = _do_format (data , _type )
96+ if show_pre :
97+ out = pre + ": "
98+ else :
99+ out = ""
100+
101+ out += text
102+
103+ if show_uri or show_url :
104+ out += ' -'
105+
106+ if show_url :
107+ out += ' ' + data ["external_urls" ]["spotify" ]
108+
109+ if show_uri :
110+ out += ' ' + "[{}]" .format (data ["uri" ])
111+
112+ return out
113+
114+
115+ def _format_search (text , _type , reply ):
116+ data = _search (text , _type , reply )
117+ return _format_response (data , _type , show_url = True , show_uri = True )
118+
119+
77120@hook .onload
78121def create_api (bot ):
79122 keys = bot .config ['api_keys' ]
@@ -86,39 +129,19 @@ def create_api(bot):
86129@hook .command ('spotify' , 'sptrack' )
87130def spotify (text , reply ):
88131 """<song> - Search Spotify for <song>"""
89- data = _search (text , "track" , reply )
90-
91- try :
92- return "\x02 {}\x02 by \x02 {}\x02 - {} / {}" .format (
93- data ["name" ], data ["artists" ][0 ]["name" ],
94- data ["external_urls" ]["spotify" ], data ["uri" ])
95- except IndexError :
96- return "Unable to find any tracks!"
132+ return _format_search (text , "track" , reply )
97133
98134
99135@hook .command ("spalbum" )
100136def spalbum (text , reply ):
101137 """<album> - Search Spotify for <album>"""
102- data = _search (text , "album" , reply )
103-
104- try :
105- return "\x02 {}\x02 by \x02 {}\x02 - {} / {}" .format (
106- data ["artists" ][0 ]["name" ], data ["name" ],
107- data ["external_urls" ]["spotify" ], data ["uri" ])
108- except IndexError :
109- return "Unable to find any albums!"
138+ return _format_search (text , "album" , reply )
110139
111140
112141@hook .command ("spartist" , "artist" )
113142def spartist (text , reply ):
114143 """<artist> - Search Spotify for <artist>"""
115- data = _search (text , "artist" , reply )
116-
117- try :
118- return "\x02 {}\x02 - {} / {}" .format (
119- data ["name" ], data ["external_urls" ]["spotify" ], data ["uri" ])
120- except IndexError :
121- return "Unable to find any artists!"
144+ return _format_search (text , "artist" , reply )
122145
123146
124147@hook .regex (http_re )
@@ -131,15 +154,4 @@ def spotify_url(match):
131154
132155 data = request .json ()
133156
134- if _type == "track" :
135- name = data ["name" ]
136- artist = data ["artists" ][0 ]["name" ]
137- album = data ["album" ]["name" ]
138-
139- return "Spotify Track: \x02 {}\x02 by \x02 {}\x02 from the album \x02 {}\x02 " .format (name , artist , album )
140- elif _type == "artist" :
141- return "Spotify Artist: \x02 {}\x02 , followers: \x02 {}\x02 , genres: \x02 {}\x02 " .format (
142- data ["name" ], data ["followers" ]["total" ],
143- ', ' .join (data ["genres" ]))
144- elif _type == "album" :
145- return "Spotify Album: \x02 {}\x02 - \x02 {}\x02 " .format (data ["artists" ][0 ]["name" ], data ["name" ])
157+ return _format_response (data , _type , show_pre = True )
0 commit comments