11from datetime import datetime
22
33import requests
4+ import string
45
56from sqlalchemy import Table , Column , PrimaryKeyConstraint , String
67
1819)
1920
2021
22+ def filter_tags (tags , artist , limit = 4 ):
23+ """
24+ Takes a list of tags (strings) and an artist name.
25+ returns a new list of N tags with the following changes:
26+ * All lowercase
27+ * Artist name removed
28+ * Blacklist of words removed
29+ """
30+
31+ # We never want to see these specific tags
32+ blacklist = [
33+ "seen live" ,
34+ ]
35+
36+ # Force all tags to lowercase first
37+ # This allows easy comparisons with artist and blacklist
38+ tags = [tag .lower () for tag in tags ]
39+
40+ # Remove punctuation marks from artist name and force to lowercase
41+ # This accounts for inconsistencies in naming, e.g. "Panic! at the disco"
42+ translator = str .maketrans ('' , '' , string .punctuation )
43+ artist = artist .translate (translator ).lower ()
44+
45+ # Perform the actual filtering, stop when we reach the desired number of tags
46+ count = 0
47+ filtered_tags = []
48+ for tag in tags :
49+ if not tag == artist :
50+ if not tag in blacklist :
51+ filtered_tags .append (tag )
52+ count += 1
53+ if count == limit :
54+ return filtered_tags
55+
56+
2157@hook .on_start ()
2258def load_cache (db ):
2359 """
@@ -146,27 +182,15 @@ def getartisttags(artist, bot):
146182 request = requests .get (api_url , params = params )
147183 tags = request .json ()
148184
149- # Don't show tags from this list
150- blacklist = [
151- "seen live" ,
152- artist .lower (),
153- ]
154-
155185 # if artist doesn't exist return no tags
156186 if tags .get ("error" ) == 6 :
157187 return "no tags"
158188
159189 if 'tag' in tags ['toptags' ]:
160190 for item in tags ['toptags' ]['tag' ]:
161- try :
162- if not item ['name' ].lower () in blacklist :
163- tag_list .append (item ['name' ])
164- else :
165- pass
166- except KeyError :
167- pass
191+ tag_list .append (item ['name' ])
168192
169- tag_list = tag_list [ 0 : 4 ]
193+ tag_list = filter_tags ( tag_list , artist , limit = 4 )
170194
171195 return ', ' .join (tag_list ) if tag_list else 'no tags'
172196
@@ -185,15 +209,9 @@ def gettracktags(artist, title, bot):
185209
186210 if 'tag' in tags ['toptags' ]:
187211 for item in tags ['toptags' ]['tag' ]:
188- try :
189- if not item ['name' ] == "seen live" :
190- tag_list .append (item ['name' ])
191- else :
192- pass
193- except KeyError :
194- pass
195-
196- tag_list = tag_list [0 :4 ]
212+ tag_list .append (item ['name' ])
213+
214+ tag_list = filter_tags (tag_list , artist , limit = 4 )
197215
198216 return ', ' .join (tag_list ) if tag_list else 'no tags'
199217
0 commit comments