This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnowplaying.py
executable file
·201 lines (174 loc) · 4.53 KB
/
nowplaying.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import fauxchat as xchat
import sys
import os
import re
import dbus
__module_name__ = "NowPlaying"
__module_author__ = "Sapphire Becker (logicplace.com)"
__module_version__ = "0.3"
__module_description__ = "Announce what's now playing on your [linux] system"
#TODO: Why does this crash xchat on exit?
bus = dbus.SessionBus()
players = []
settingsLoc = os.path.join(xchat.get_info("xchatdir"),"settings")
try: os.mkdir(settingsLoc)
except OSError: pass
settingsLoc = os.path.join(settingsLoc,"nowplaying")
try:
f = open(settingsLoc,"r")
now_playing_message = f.read()
f.close()
except IOError: now_playing_message = "me is listening to %(title) - %(artist)"
class Player:
def __init__(self,name,dbusInfo,playing,song):
global bus
self.name = name
self._info = dbusInfo
if type(self._info) is not list:
self._nolist = True
self._info = [self._info]
#endif
for x in self._info:
if "interface" not in x: x["interface"] = x["sender"]
#endfor
# Function that returns true (player is playing music) or false (player is off)
self._playing = playing
# Function that returns song info: {
# title: Title of the song
# artist: Artists' names
# album: Album title
# url: URL related to the song
# length: Total length of the song in seconds
# time: Current place in the song in seconds
# player: Name of the program playing the song # Added by this class!
# }
self._song = song
#enddef
def create(self):
ret = []
for x in self._info:
try:
ret.append(dbus.Interface(
bus.get_object(x["sender"],x["path"]),x["interface"]
))
except dbus.exceptions.DBusException: return None
# For some reason it's not catching the above..
except: return None
#endtry
#endfor
if self._nolist: return ret[0]
return ret
#enddef
def playing(self):
intf = self.create()
if intf is None: return False
ret = self._playing(intf)
del intf
return ret
#endif
def song(self):
intf = self.create()
if intf is None: return {}
ret = self._song(intf)
ret["player"] = self.name
del intf
return ret
#enddef
#endclass
### Pithos ###
def pithosGetCurrentSong(self):
data = self.GetCurrentSong()
return {
"album": unicode(data["album"]),
"artist": unicode(data["artist"]),
"title": unicode(data["title"]),
"url": unicode(data["songDetailURL"]),
}
#enddef
players.append(Player(
"Pithos (Pandora)",{
"sender": "net.kevinmehall.Pithos",
"path": "/net/kevinmehall/Pithos"
},
(lambda self: bool(self.IsPlaying())),
pithosGetCurrentSong
))
### Audacious ###
def audGetCurrentTrack(self):
curTrack = self[0].GetCurrentTrack()
return {
"title": self[1].SongTitle(curTrack)
#TODO: More data
}
#enddef
players.append(Player(
"Audacious",[{
"sender": "org.mpris.audacious",
"path": "/TrackList",
"interface": "org.freedesktop.MediaPlayer",
},{
"sender": "org.mpris.audacious",
"path": "/org/atheme/audacious",
"interface": "org.atheme.audacious"
}],
(lambda self: True), #TODO: Fix
audGetCurrentTrack
))
### XChat Stuff ###
bsection = re.compile("%\{([^}]+)\}")
psection = re.compile("(%\(([a-z]+)\))")
def NowPlaying(word,word_eol,userdata):
msg = now_playing_message
if len(word) > 1: msg = word[1]+" "+msg.split(" ",1)[1]
songData = None
for x in players:
if x.playing():
songData = x.song()
break
#endif
#endfor
if songData:
msgs = bsection.split(msg)
msg = ""
for i in range(len(msgs)):
if i%2 == 0: # replace with "" if non existent
msg += psection.sub((lambda m: songData[m.group(2)] if m.group(2) in songData else ""),msgs[i])
else:
try: msg += psection.sub(r'\1s',msgs[i]) % songData
except KeyError: pass
#endif
#endfor
xchat.command(msg)
else:
xchat.prnt("Not playing anything")
#endif
return xchat.EAT_XCHAT
#enddef
def SetMessage(word,word_eol,userdata):
global now_playing_message
if len(word) < 2:
# Display mode
xchat.prnt("now_playing_message..........: " + now_playing_message)
return
#endif
if word[1] == "now_playing_message":
if len(word_eol) > 2: # Set
now_playing_message = word_eol[2]
xchat.prnt("%s set to: %s" % (word[1],word_eol[2]))
else:
dots = 29-len(word[1])
xchat.prnt(word[1]+"\00318"+("."*dots)+"\00319:\x0f "+now_playing_message)
#endif
return xchat.EAT_XCHAT
#endif
return xchat.EAT_NONE
#enddef
def SaveMessage(userdata):
f = open(settingsLoc,"w")
f.write(now_playing_message)
f.close()
#enddef
xchat.hook_command("np",NowPlaying)
xchat.hook_command("set",SetMessage)
xchat.hook_unload(SaveMessage)
xchat.prnt("Loaded %s version %s." % (__module_name__,__module_version__))