2012-04-27 00:27:19 +12:00
from datetime import datetime
2012-02-28 08:01:57 +08:00
2014-02-14 16:36:57 +13:00
from util import hook , http , timesince
2012-09-05 07:52:03 +12:00
api_url = " http://ws.audioscrobbler.com/2.0/?format=json "
2012-03-12 14:35:36 -08:00
@hook.command ( ' l ' , autohelp = False )
@hook.command ( autohelp = False )
2013-09-04 18:30:04 +08:00
def lastfm ( inp , nick = ' ' , db = None , bot = None , notice = None ) :
""" lastfm [user] [dontsave] -- Displays the now playing (or last played)
track of LastFM user [ user ] . """
2012-04-19 20:32:31 +12:00
api_key = bot . config . get ( " api_keys " , { } ) . get ( " lastfm " )
2012-04-19 13:29:53 +12:00
if not api_key :
return " error: no api key set "
2012-04-19 20:32:31 +12:00
2012-09-05 07:52:03 +12:00
# check if the user asked us not to save his details
dontsave = inp . endswith ( " dontsave " )
2012-04-19 21:25:08 +12:00
if dontsave :
2012-09-05 07:52:03 +12:00
user = inp [ : - 9 ] . strip ( ) . lower ( )
else :
user = inp
2012-02-28 08:01:57 +08:00
2012-04-19 21:25:08 +12:00
db . execute ( " create table if not exists lastfm(nick primary key, acc) " )
if not user :
user = db . execute ( " select acc from lastfm where nick=lower(?) " ,
( nick , ) ) . fetchone ( )
if not user :
notice ( lastfm . __doc__ )
return
user = user [ 0 ]
2012-02-28 08:01:57 +08:00
response = http . get_json ( api_url , method = " user.getrecenttracks " ,
api_key = api_key , user = user , limit = 1 )
if ' error ' in response :
2014-03-27 10:08:54 +13:00
return u " Error: {} . " . format ( response [ " message " ] )
2012-02-28 08:01:57 +08:00
2012-04-19 13:31:47 +12:00
if not " track " in response [ " recenttracks " ] or len ( response [ " recenttracks " ] [ " track " ] ) == 0 :
2014-03-27 10:08:54 +13:00
return u ' No recent tracks for user " {} " found. ' . format ( user )
2012-09-05 07:52:03 +12:00
2012-04-19 13:31:47 +12:00
tracks = response [ " recenttracks " ] [ " track " ]
2012-02-28 08:01:57 +08:00
if type ( tracks ) == list :
# if the user is listening to something, the tracks entry is a list
# the first item is the current track
track = tracks [ 0 ]
2012-04-19 20:45:37 +12:00
status = ' is listening to '
2012-04-27 00:27:19 +12:00
ending = ' . '
2012-02-28 08:01:57 +08:00
elif type ( tracks ) == dict :
# otherwise, they aren't listening to anything right now, and
# the tracks entry is a dict representing the most recent track
track = tracks
2012-04-19 20:45:37 +12:00
status = ' last listened to '
2012-04-27 00:27:19 +12:00
# lets see how long ago they listened to it
time_listened = datetime . fromtimestamp ( int ( track [ " date " ] [ " uts " ] ) )
time_since = timesince . timesince ( time_listened )
2013-09-05 10:11:18 +08:00
ending = ' ( {} ago) ' . format ( time_since )
2012-09-05 07:52:03 +12:00
2012-02-28 08:01:57 +08:00
else :
2012-02-28 08:05:41 +08:00
return " error: could not parse track listing "
2012-02-28 08:01:57 +08:00
title = track [ " name " ]
album = track [ " album " ] [ " #text " ]
artist = track [ " artist " ] [ " #text " ]
2014-03-27 10:08:54 +13:00
out = u ' {} {} " {} " ' . format ( user , status , title )
2012-02-28 08:01:57 +08:00
if artist :
2014-03-27 10:08:54 +13:00
out + = u " by \x02 {} \x0f " . format ( artist )
2012-02-28 08:01:57 +08:00
if album :
2014-03-27 10:08:54 +13:00
out + = u " from the album \x02 {} \x0f " . format ( album )
2012-09-05 07:52:03 +12:00
2012-04-27 00:27:19 +12:00
# append ending based on what type it was
out + = ending
2012-09-05 07:52:03 +12:00
2012-04-19 21:25:08 +12:00
if inp and not dontsave :
db . execute ( " insert or replace into lastfm(nick, acc) values (?,?) " ,
2014-02-14 17:03:08 +13:00
( nick . lower ( ) , user ) )
2012-04-19 21:25:08 +12:00
db . commit ( )
2012-02-28 08:01:57 +08:00
2012-04-19 13:29:53 +12:00
return out