Advertisement
banjax

Untitled

Nov 7th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. from util import hook, http, timesince
  2. from datetime import datetime
  3.  
  4. api_url = "http://ws.audioscrobbler.com/2.0/?format=json"
  5.  
  6.  
  7. @hook.command('plays', autohelp=False)
  8. @hook.command(autohelp=False)
  9. def plays(band, nick='', db=None, bot=None, notice=None):
  10. api_key = bot.config.get("api_keys", {}).get("lastfm")
  11. if not api_key:
  12. return "error: no api key set"
  13.  
  14. user = db.execute("select acc from lastfm where nick=lower(?)", (nick,)).fetchone()
  15.  
  16. if not user:
  17. notice(lastfm.__doc__)
  18. return
  19.  
  20. user = user[0]
  21.  
  22. response = http.get_json(api_url, method="user.getartisttracks", api_key=api_key, user=user, artist=band, limit=1)
  23.  
  24. if 'error' in response:
  25. return "Error: {}.".format(response["message"])
  26.  
  27. if not "items" in response["artisttracks"] or (response["artisttracks"]["@attr"]["items"]) == 0:
  28. return '"{}" has never listened to that band.'.format(user)
  29.  
  30. plays = response["artisttracks"]["@attr"]["items"]
  31.  
  32.  
  33. out = '{} {} {} {}'.format(user, status, plays, artist)
  34. if artist:
  35. out += " by \x02{}\x0f".format(artist)
  36.  
  37. # append ending based on what type it was
  38. out += ending
  39.  
  40. return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement