11import math
22import string
33from datetime import datetime
4- from functools import wraps
54
65import requests
76from sqlalchemy import Table , Column , PrimaryKeyConstraint , String
@@ -24,20 +23,6 @@ def format_user(user):
2423 return '\u200B ' .join ((user [:1 ], user [1 :]))
2524
2625
27- def require_api_key (func ):
28- """Marks a hook that requires an api key"""
29-
30- @wraps (func )
31- def wrapper (event , bot , db , text , nick ):
32- key = bot .config .get ("api_keys" , {}).get ("lastfm" )
33- if not key :
34- return "error: no api key set"
35-
36- return func (key , event , db , text , nick )
37-
38- return wrapper
39-
40-
4126def filter_tags (tags , artist , limit = 4 ):
4227 """
4328 Takes a list of tags (strings) and an artist name.
@@ -168,7 +153,11 @@ def getartistinfo(api_key, artist, user=''):
168153 return artist
169154
170155
171- def _topartists (api_key , text , nick , period = None , limit = 10 ):
156+ def _topartists (bot , text , nick , period = None , limit = 10 ):
157+ api_key = bot .config .get ("api_keys" , {}).get ("lastfm" )
158+ if not api_key :
159+ return "error: no api key set"
160+
172161 if text :
173162 username = get_account (text )
174163 if not username :
@@ -196,9 +185,12 @@ def _topartists(api_key, text, nick, period=None, limit=10):
196185
197186
198187@hook .command ("lastfm" , "last" , "np" , "l" , autohelp = False )
199- @require_api_key
200- def lastfm (api_key , event , db , text , nick ):
188+ def lastfm (event , db , text , nick , bot ):
201189 """[user] [dontsave] - displays the now playing (or last played) track of LastFM user [user]"""
190+ api_key = bot .config .get ("api_keys" , {}).get ("lastfm" )
191+ if not api_key :
192+ return "error: no api key set"
193+
202194 # check if the user asked us not to save his details
203195 dontsave = text .endswith (" dontsave" )
204196 if dontsave :
@@ -286,9 +278,12 @@ def lastfm(api_key, event, db, text, nick):
286278
287279
288280@hook .command ("plays" )
289- @require_api_key
290- def getuserartistplaycount (api_key , event , db , text , nick ):
281+ def getuserartistplaycount (event , bot , text , nick ):
291282 """[artist] - displays the current user's playcount for [artist]. You must have your username saved."""
283+ api_key = bot .config .get ("api_keys" , {}).get ("lastfm" )
284+ if not api_key :
285+ return "error: no api key set"
286+
292287 user = get_account (nick )
293288 if not user :
294289 event .notice_doc ()
@@ -310,9 +305,12 @@ def getuserartistplaycount(api_key, event, db, text, nick):
310305
311306
312307@hook .command ("band" , "la" )
313- @require_api_key
314- def displaybandinfo (api_key , event , db , text , nick ):
308+ def displaybandinfo (bot , text ):
315309 """[artist] - displays information about [artist]."""
310+ api_key = bot .config .get ("api_keys" , {}).get ("lastfm" )
311+ if not api_key :
312+ return "error: no api key set"
313+
316314 artist = getartistinfo (api_key , text )
317315
318316 if 'error' in artist :
@@ -330,9 +328,12 @@ def displaybandinfo(api_key, event, db, text, nick):
330328
331329
332330@hook .command ("lastfmcompare" , "compare" , "lc" )
333- @require_api_key
334- def lastfmcompare (api_key , event , db , text , nick ):
331+ def lastfmcompare (bot , text , nick ):
335332 """[user] ([user] optional) - displays the now playing (or last played) track of LastFM user [user]"""
333+ api_key = bot .config .get ("api_keys" , {}).get ("lastfm" )
334+ if not api_key :
335+ return "error: no api key set"
336+
336337 if not text :
337338 return "please specify a lastfm username to compare"
338339 try :
@@ -387,9 +388,12 @@ def lastfmcompare(api_key, event, db, text, nick):
387388
388389
389390@hook .command ("ltop" , "ltt" , autohelp = False )
390- @require_api_key
391- def toptrack (api_key , event , db , text , nick ):
391+ def toptrack (bot , text , nick ):
392392 """Grabs a list of the top tracks for a last.fm username"""
393+ api_key = bot .config .get ("api_keys" , {}).get ("lastfm" )
394+ if not api_key :
395+ return "error: no api key set"
396+
393397 if text :
394398 username = get_account (text )
395399 if not username :
@@ -414,28 +418,24 @@ def toptrack(api_key, event, db, text, nick):
414418
415419
416420@hook .command ("lta" , "topartist" , autohelp = False )
417- @require_api_key
418- def topartists (api_key , event , db , text , nick ):
421+ def topartists (bot , text , nick ):
419422 """Grabs a list of the top artists for a last.fm username. You can set your lastfm username with .l username"""
420- return _topartists (api_key , text , nick )
423+ return _topartists (bot , text , nick )
421424
422425
423426@hook .command ("ltw" , "topweek" , autohelp = False )
424- @require_api_key
425- def topweek (api_key , event , db , text , nick ):
427+ def topweek (bot , text , nick ):
426428 """Grabs a list of the top artists in the last week for a last.fm username. You can set your lastfm username with .l username"""
427- return _topartists (api_key , text , nick , '7day' )
429+ return _topartists (bot , text , nick , '7day' )
428430
429431
430432@hook .command ("ltm" , "topmonth" , autohelp = False )
431- @require_api_key
432- def topmonth (api_key , event , db , text , nick ):
433+ def topmonth (bot , text , nick ):
433434 """Grabs a list of the top artists in the last month for a last.fm username. You can set your lastfm username with .l username"""
434- return _topartists (api_key , text , nick , '1month' )
435+ return _topartists (bot , text , nick , '1month' )
435436
436437
437438@hook .command ("lty" , "topyear" , autohelp = False )
438- @require_api_key
439- def topall (api_key , event , db , text , nick ):
439+ def topall (bot , text , nick ):
440440 """Grabs a list of the top artists in the last year for a last.fm username. You can set your lastfm username with .l username"""
441- return _topartists (api_key , text , nick , '1year' )
441+ return _topartists (bot , text , nick , '1year' )
0 commit comments