#NEWLINE# def now_playing( self , cardinal , user , channel , msg ) : #NEWLINE# #INDENT# if ( self . api_key is None ) : #NEWLINE# #INDENT# cardinal . sendMsg ( channel , 'Last.fm plugin is not configured. Please set API key.' ) #NEWLINE# self . logger . error ( 'Attempt to get now playing failed, API key not set' ) #NEWLINE# return #NEWLINE# #UNINDENT# if ( not self . conn ) : #NEWLINE# #INDENT# cardinal . sendMsg ( channel , 'Unable to access local Last.fm database.' ) #NEWLINE# self . logger . error ( 'Attempt to get now playing failed, no database connection' ) #NEWLINE# return #NEWLINE# #UNINDENT# c = self . conn . cursor ( ) #NEWLINE# message = msg . split ( ) #NEWLINE# if ( ( message [ 0 ] != '.np' ) and ( message [ 0 ] != '.nowplaying' ) ) : #NEWLINE# #INDENT# message . pop ( 0 ) #NEWLINE# #UNINDENT# if ( len ( message ) >= 2 ) : #NEWLINE# #INDENT# nick = message [ 1 ] #NEWLINE# c . execute ( 'SELECT username FROM users WHERE nick=?' , ( nick , ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# nick = user . group ( 1 ) #NEWLINE# vhost = user . group ( 3 ) #NEWLINE# c . execute ( 'SELECT username FROM users WHERE nick=? OR vhost=?' , ( nick , vhost ) ) #NEWLINE# #UNINDENT# result = c . fetchone ( ) #NEWLINE# if ( not result ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# username = message [ 1 ] #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# username = user . group ( 1 ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# username = result [ 0 ] #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# uh = urllib2 . urlopen ( ( 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&api_key=%s&limit=1&format=json' % ( username , self . api_key ) ) ) #NEWLINE# content = json . load ( uh ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# cardinal . sendMsg ( c , 'Unable to connect to Last.fm.' ) #NEWLINE# self . logger . exception ( 'Failed to connect to Last.fm' ) #NEWLINE# return #NEWLINE# #UNINDENT# if ( ( 'error' in content ) and ( content [ 'error' ] == 10 ) ) : #NEWLINE# #INDENT# cardinal . sendMsg ( channel , 'Last.fm plugin is not configured. Please set API key.' ) #NEWLINE# self . logger . error ( 'Attempt to get now playing failed, API key incorrect' ) #NEWLINE# return #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( 'error' in content ) and ( content [ 'error' ] == 6 ) ) : #NEWLINE# #INDENT# cardinal . sendMsg ( channel , ( 'Your Last.fm username is incorrect. No user exists by the username %s.' % str ( username ) ) ) #NEWLINE# return #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# song = content [ 'recenttracks' ] [ 'track' ] [ 0 ] [ 'name' ] #NEWLINE# artist = content [ 'recenttracks' ] [ 'track' ] [ 0 ] [ 'artist' ] [ '#text' ] #NEWLINE# cardinal . sendMsg ( channel , ( '%s is now listening to: %s by %s' % ( str ( username ) , str ( song ) , str ( artist ) ) ) ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# cardinal . sendMsg ( channel , 'Unable to find any tracks played. (Is your Last.fm username correct?)' )