Advertisement
banjax

Untitled

Nov 7th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 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(inp, nick='', db=None, bot=None, notice=None):
  10. """plays [user] [band] -- Displays the number of times [user] has listened $
  11. api_key = bot.config.get("api_keys", {}).get("lastfm")
  12. if not api_key:
  13. return "error: no api key set"
  14.  
  15.  
  16.  
  17. db.execute("create table if not exists lastfm(nick primary key, acc)")
  18.  
  19. if not user:
  20. user = db.execute("select acc from lastfm where nick=lower(?)",
  21. (nick,)).fetchone()
  22. if not user:
  23. notice(lastfm.__doc__)
  24. return
  25. user = user[0]
  26.  
  27. response = http.get_json(api_url, method="user.getartisttracks",
  28. api_key=api_key, user=user, limit=1)
  29.  
  30. if 'error' in response:
  31. return "Error: {}.".format(response["message"])
  32.  
  33. if not "items" in response["artisttracks"] or (response["artisttracks"]["@a$
  34. return '"{}" has never listened to that band.'.format(user)
  35.  
  36. plays = response["artisttracks"]["@attr"]["items"]
  37.  
  38.  
  39. out = '{} {} {} {}'.format(user, status, plays, artist)
  40. if artist:
  41. out += " by \x02{}\x0f".format(artist)
  42.  
  43.  
  44. # append ending based on what type it was
  45. out += ending
  46.  
  47. return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement