forked from dreamer2908/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexchat-audacious.py
160 lines (129 loc) · 4 KB
/
hexchat-audacious.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
#!/usr/bin/python
# encoding: utf-8
__module_name__ = "(He)xchat Audacious Now Playing"
__module_author__ = "dreamer2908"
__module_version__ = "1.0"
__module_description__ = "Get NP information from Audacious"
import xchat, sys, os, math
position = 0
length = 0
artist = u''
album = u''
tracknumber = 0
title = u''
bitrate = 0
samplerate = 0
fmt = u''
quality = u''
def getAuIFace():
from dbus import Bus, DBusException
bus = Bus(Bus.TYPE_SESSION)
try:
return bus.get_object('org.mpris.audacious', '/Player')
except: # DBusException: # catch all exceptions
xchat.prnt("Either Audacious is not running or you have something wrong with your D-Bus setup.")
return None
def getValue(auData, key, keyFB, default):
try:
return auData[key]
except KeyError:
try:
return auData[keyFB]
except:
return default
except:
return default
def toUtf8(text):
return unicode(text).encode('utf-8')
def getNpInfo():
import urllib
global position, artist, album, tracknumber, title, bitrate, samplerate, length, fmt, quality
auIFace = getAuIFace()
if auIFace != None:
auData = auIFace.GetMetadata()
try:
position = auIFace.PositionGet() / 1000
except:
position = 0
artist = getValue(auData, 'artist', '', None)
album = getValue(auData, 'album', '', None)
tracknumber = getValue(auData, 'tracknumber', '', -1)
title = getValue(auData, 'title', '', '')
bitrate = '%d kbps' % getValue(auData, 'bitrate', 'audio-bitrate', 0)
# dbus.String(u'quality'): dbus.String(u'Stereo, 44100 Hz', variant_level=1),
quality = getValue(auData, 'quality', '', "Stereo, 0 Hz")
tmp = quality.split(', ')
samplerate = tmp[len(tmp) - 1]
length = getValue(auData, 'mtime', '', 0) / 1000
# get format from file extention
# or dbus.String(u'codec'): dbus.String(u'MPEG-1 layer 3', variant_level=1),
location = urllib.unquote(getValue(auData, 'location', '', ''))
namae, ext = os.path.splitext(location)
if len(ext) > 0:
fmt = ext.upper().strip('.')
else:
fmt = 'Unknown'
if len(title) < 1:
dirName, fileName = os.path.split(location)
title = fileName
return True
return False
def formatTime(time):
if time >= 3600:
result = '%d:' % math.floor(time / 3600)
else:
result = ''
time = time % 3600
result += '%02d' % math.floor(time / 60)
time = time % 60
result += ':%02d' % math.floor(time)
return result
def nowPlaying(word, word_eol, userdata):
if getNpInfo():
if len(title) < 1:
text = "me is playing nothing on Audacious"
else:
text = "me is playing on Audacious: "
text += '[ %s / %s ] ' % (formatTime(position), formatTime(length))
text += '\"' + title + '\" '
if artist != None:
text += 'by "%s" ' % artist
elif album != None:
text += 'by "Unknown artist" '
if album != None:
if tracknumber > 0:
text += '(track #%d' % tracknumber + ' of album \"' + album + '\") '
else:
text += '(album \"' + album + '\") '
text += '| ' + fmt + ' | ' + samplerate + ' | ' + bitrate
xchat.command(text)
return xchat.EAT_ALL
def nowPlaying2(word, word_eol, userdata):
if getNpInfo():
if len(title) < 1:
text = "me is playing nothing on Audacious"
else:
text = "me > "
if artist != None:
text += '%s - ' % artist
elif album != None:
text += 'Unknown artist - '
text += '%s ' % title
if album != None:
if tracknumber > 0:
text += ' - [ %s #%d ] ' % (album, tracknumber)
else:
text += ' - [ %s ] ' % (album)
text += '- [ %s / %s ] ' % (formatTime(position), formatTime(length))
xchat.command(text)
return xchat.EAT_ALL
def test(word, word_eol, userdata):
auIFace = getAuIFace()
auData = auIFace.GetMetadata()
print(auData)
print(auIFace.PositionGet())
return xchat.EAT_ALL
xchat.hook_command("aud", nowPlaying, help="Displays current playing song in Audacious (long).")
xchat.hook_command("aud2", nowPlaying2, help="Displays current playing song in Audacious (short).")
xchat.hook_command("audt", test, help="Test Audacious")
xchat.prnt(u'%s v%s plugin loaded' % (__module_name__, __module_version__))