usr bin env python coding utf-8 import re import socket import sys fro

 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#import re
import socket
import sys
from time import sleep
import os
_widgetName = "mpd"
_mpdHost = ""
_mpdPort = 6600
_awesome_client_path = '%s/bin/awesome-client' % os.environ.get('HOME')
_spaces = ' '*4
mpd_conn_stat = -1
def mpd_connect():
global sock
try:
sock.connect((_mpdHost, _mpdPort))
except:
return -1
return 1
def get_mpd_status():
global sock, mpd_conn_stat
if mpd_conn_stat != -1:
try:
sock.send("currentsong\nstatus\n")
buff = sock.recv(1024)
except:
sock = socket.socket()
mpd_conn_stat = mpd_connect()
return '[ failed to fetch song! %) ]'
else:
sock = socket.socket()
mpd_conn_stat = mpd_connect()
return '[ connect failed! %) ]'
result = ''
try: result = buff.split('file: ')[1].split('\n')[0]
except: pass
try: result = buff.split('Name: ')[1].split('\n')[0]
except: pass
try: result = buff.split('Artist: ')[1].split('\n')[0]
except: pass
try:
result += ' - %s' % buff.split('Title: ')[1].split('\n')[0]
try: result += ' (%s)' % buff.split('Album: ')[1].split('\n')[0]
except: pass
except:
try: result = buff.split('file: ')[1].split('\n')[0]
except: pass
try: result = buff.split('Name: ')[1].split('\n')[0]
except: pass
try:
x = buff.split('state: ')[1].split('\n')[0]
if x == 'play':
result = '|> %s' % result
elif x == 'pause':
result = '|| %s' % result
elif x == 'stop':
result = '-- %s' % result
except: pass
return '[ %s ]' % result
if __name__ == "__main__":
if len(sys.argv) > 1:
_widgetName = sys.argv[1]
sock = socket.socket()
socket.setdefaulttimeout(30)
mpd_conn_stat = mpd_connect()
while 1:
try:
pipe = os.popen(_awesome_client_path, 'w')
pipe.write("0 widget_tell %s %s%s\n" % (_widgetName, _spaces, get_mpd_status()))
pipe.close()
#print get_mpd_status()
finally:
pass
sleep(1)