v0.3: Slacked code down, so no more PyQt is required
[LastFMNotify.git] / LastFMNotify.py
1 #!/usr/bin/env python
2
3 import os
4 import sys
5 from time import sleep, time
6 from hashlib import md5
7 import urllib
8 import logging
9
10 logging.basicConfig(filename="lastfmnotify.log", level = logging.DEBUG )
11
12 class NotifyNowPlaying():
13 def __init__(self,user,password):
14 self.user = user
15 self.password = password
16 self.client = "ark"
17 self.version = "1.0"
18 self.unixtimestamp = time().__int__()
19
20 def GenToken(self):
21 self.auth = md5(md5(str(self.password)).hexdigest() + str(self.unixtimestamp)).hexdigest()
22
23 def Handshake(self):
24 self.GenToken()
25 response = urllib.urlopen("http://post.audioscrobbler.com/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s"%(self.client,self.version,self.user,self.unixtimestamp,self.auth))
26 r = str(response.read()).split("\n")
27 if r[0]=="OK":
28 self.SessionId = r[1]
29 self.UrlNotification = r[2]
30 self.UrlSubmit = r[3]
31 debug("Handshake OK")
32 return True
33 debug("Handshake Error")
34 return False
35
36 def SendNotification(self,artist,track,album,sec,track_number,mb_trackid=""):
37 params={}
38 params['s'] = self.SessionId
39 params['a'] = artist
40 params['t'] = track
41 params['b'] = album
42 params['l'] = sec
43 params['n'] = track
44 params['m'] = mb_trackid
45
46 params = urllib.urlencode(params)
47 response = urllib.urlopen(self.UrlNotification,params)
48 r = str(response.read()).split("\n")
49 if r[0]=="OK":
50 debug("Notification sent")
51 return True
52 debug("Error while sending song data")
53 return False
54
55 #####
56 def debug( message ):
57 """ Prints debug message to stdout """
58 logging.debug(message)
59
60 # Notification callbacks. Implement these functions to react to specific notification
61 # events from Amarok:
62 def sendNotify():
63 debug("sendNotify()")
64 artist = str(os.popen("dcop amarok player artist").read().strip())
65 track = str(os.popen("dcop amarok player title").read().strip())
66 album = str(os.popen("dcop amarok player album").read().strip())
67 sec = str(os.popen("dcop amarok player trackTotalTime").read().strip())
68 pista = str(os.popen("dcop amarok player track").read().strip())
69
70 lastfm.Handshake()
71 os.system("dcop amarok playlist shortStatusMessage 'Sending notification to Last.fm'")
72 if lastfm.SendNotification(artist,track,album,sec,pista):
73 sleep(3)
74 os.system("dcop amarok playlist shortStatusMessage '%s - %s sent'"%(artist,track))
75 return True
76 os.system("dcop amarok playlist shortStatusMessage 'An error occurred while sending %s-%s'"%(artist,track))
77 return None
78
79 #####
80 user = None
81 passwort = None
82 lastfm = None
83
84 if __name__ == "__main__":
85 user = os.popen("dcop amarok script readConfig 'ScrobblerUsername'").read().strip()
86 password = os.popen("dcop amarok script readConfig 'ScrobblerPassword'").read().strip()
87
88 debug("Notifier Starting...")
89 lastfm = NotifyNowPlaying(user, password)
90
91 # start main thread
92 debug("Starting main thread...")
93 while True:
94 line = sys.stdin.readline()
95
96 if line:
97 debug( "Received notification: %s " % line )
98
99 if line.find( "trackChange" ) == 0:
100 debug("track changed!")
101 sendNotify()
102 else:
103 break
104
This page took 0.116041 seconds and 5 git commands to generate.