@@ -106,7 +106,11 @@ def lastfm(text, nick, db, bot, notice):
106106 except :
107107 url = track ["url" ]
108108 pass
109- tags = getartisttags (artist , bot )
109+
110+ tags = gettracktags (artist , title , bot )
111+ if tags == "no tags" :
112+ tags = getartisttags (artist , bot )
113+
110114 playcount = getusertrackplaycount (artist , title , user , bot )
111115
112116 out = '{} {} "{}"' .format (user , status , title )
@@ -160,6 +164,33 @@ def getartisttags(artist, bot):
160164
161165 return ', ' .join (tag_list ) if tag_list else 'no tags'
162166
167+ # get tags for $title
168+ def gettracktags (artist , title , bot ):
169+ tag_list = []
170+ api_key = bot .config .get ("api_keys" , {}).get ("lastfm" )
171+ params = { 'method' : 'track.getTopTags' , 'api_key' : api_key , 'artist' : artist ,
172+ 'track' : title , 'autocorrect' : '1' }
173+ request = requests .get (api_url , params = params )
174+ tags = request .json ()
175+
176+ # if artist doesn't exist return no tags
177+ if tags .get ("error" ) == 6 :
178+ return "no tags"
179+
180+ if 'tag' in tags ['toptags' ]:
181+ for item in tags ['toptags' ]['tag' ]:
182+ try :
183+ if not item ['name' ] == "seen live" :
184+ tag_list .append (item ['name' ])
185+ else :
186+ pass
187+ except KeyError :
188+ pass
189+
190+ tag_list = tag_list [0 :4 ]
191+
192+ return ', ' .join (tag_list ) if tag_list else 'no tags'
193+
163194def getsimilarartists (artist , bot ):
164195 artist_list = []
165196 api_key = bot .config .get ('api_keys' , {}).get ('lastfm' )
0 commit comments