New hack: mpd_random_playlist.c
[mpd-hacks.git] / mpdgajim.py
blob3a4c7d8aa67e7970c51abb2036ae5355443c1660
1 """
2 MPD2Gajim 0.1.1.1
3 MPD to Gajim (via DBUS) script.
5 Copyright (c) 2007, Alexander Stanley
6 alexanderwstanley@gmail.com
7 All rights reserved.
9 Redistribution and use in source and binary forms, with or without modification, are permitted
10 provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this list of conditions
13 and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice, this list of
15 conditions and the following disclaimer in the documentation and/or other materials provided with the
16 distribution.
17 * Neither the name of the Orloglausa.Net nor the names of its contributors may be used to endorse
18 or promote products derived from this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """
32 ### Only play with this if your system is getting
33 ### flogged by the process (which is shouldn't be)
34 interval = 1 # number of seconds between checks
36 import dbus
37 import sys
38 import string
39 import os
40 import cgi
41 import time
42 import mpdclient2
43 import datetime
45 old_t = ""
46 old_a = ""
47 old_n = ""
48 tmp_a = ""
49 tmp_t = ""
50 tmp_n = ""
51 tichk = 0
52 pause = 0
54 bus = dbus.SessionBus()
55 obj = bus.get_object('org.gajim.dbus', '/org/gajim/dbus/RemoteObject')
56 myinterface = dbus.Interface(obj, 'org.gajim.dbus.RemoteInterface')
58 def update(title, artist, track, album, mytime):
59 global pause
60 note = u'\u266B'
61 pausestr = ""
62 if pause == 1:
63 pausestr = "[Paused]"
64 tichk = 0
65 stat_msg = str("%s %s - %s %s %s") % (note,artist,title,note,pausestr)
66 global myinterface
67 for account in myinterface.list_accounts():
68 status = myinterface.get_status(account)
69 myinterface.change_status(status,stat_msg,account)
71 def stopmusic():
72 global tmp_a
73 global tmp_n
74 global tmp_t
75 global tichk
76 stat_msg = str("Music Stopped.")
77 global myinterface
78 for account in myinterface.list_accounts():
79 status = myinterface.get_status(account)
80 myinterface.change_status(status,stat_msg,account)
81 tmp_a = ""
82 tmp_t = ""
83 tmp_n = ""
84 tichk = 0
86 def checksong():
87 m = mpdclient2.connect()
88 if str(m.status().state) == str("stop"):
89 stopmusic()
90 else:
91 global pause
92 global old_t
93 global old_n
94 global old_a
95 global tmp_t
96 global tmp_n
97 global tmp_a
98 global tichk
99 var = m.currentsong()
100 s = m.status()
101 if 'album' in var:
102 album = str(var['album'])
103 else:
104 album = " "
105 if 'artist' in var:
106 artist = str(var['artist'])
107 else:
108 artist = " "
109 if 'title' in var:
110 title = str(var['title'])
111 else:
112 title = " "
113 if 'track' in var:
114 track = str(var['track'])
115 else:
116 track = int(0)
117 current = s.time.split(':', 2)[0]
118 mytime = s.time.split(':', 2)[1]
119 mytime = str("[" + str(timehack(mytime)+ "]"))
120 tichk = tichk + 1
122 if str(m.status().state) == str("pause"):
123 pause = 1
124 update(title,artist,track,album,mytime)
125 else:
126 pause = 0
127 if current > 5:
128 if str(old_t) == str(title) and str(old_a) == str(artist):
129 if tichk > int(current):
130 update(title, artist, track, album, mytime)
131 else:
132 update(title, artist, track, album, mytime)
133 tmp_t = title
134 tmp_a = artist
135 else:
136 tichk == current - 3
138 if (str(tmp_t) != str(title) and str(old_a) != str(artist)):
139 old_a = tmp_a
140 old_t = tmp_t
142 def timehack(vartime):
143 valmins = int(vartime)/60
144 valsecs = int(vartime) - (valmins * 60)
145 if valsecs < 10:
146 valsecs = "%s%s" % (0,valsecs)
147 valtime = "%s:%s" % (valmins,valsecs)
148 return valtime
150 def main():
151 while 1:
152 checksong()
153 time.sleep(interval)
154 main()
156 def createDaemon():
157 try:
158 if os.fork() > 0: os._exit(0)
159 except OSError, error:
160 print 'Error in fork 1 :: %d (%s)' % (error.errno, error.strerror)
161 os._exit(1)
163 os.chdir('/')
164 os.setsid()
165 os.umask(0)
167 try:
168 pid = os.fork()
169 if pid > 0:
170 print 'Daemonised (PID: %d)' % pid
171 os._exit(0)
172 except OSError, error:
173 print 'Error in fork 2 :: %d (%s)' % (error.errno, error.strerror)
174 os._exit(1)
176 main()
178 createDaemon()