-
Notifications
You must be signed in to change notification settings - Fork 1
/
mplyric.py
executable file
·66 lines (52 loc) · 1.25 KB
/
mplyric.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
#! /usr/bin/env python2
import sys
import getopt
import time
import mpd
from notilyric import NotiLyric
class FMLyric(object):
def __init__(self, addr, port):
self.addr = addr
self.port = port
self.notilyric = NotiLyric('fmlyric')
self.interval = 1
self.artist = self.title = ''
def run(self):
self.client = mpd.MPDClient()
try:
self.client.connect(self.addr, self.port)
except:
print 'Connect to MPD failed. Is MPD running?'
return
while True:
try:
time.sleep(self.interval)
except:
print 'Quit.'
self.client.disconnect()
self.notilyric.close()
break
status = self.client.status()
if status['state'] != 'play':
self.notilyric.close()
continue
song = self.client.currentsong()
artist = song['artist']
title = song['title']
progress = int(status['time'].split(':')[0])
if self.artist != artist or self.title != title:
self.artist = artist
self.title = title
self.notilyric.setinfo(artist, title)
self.notilyric.display(progress)
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], 'a:p:')
addr = 'localhost'
port = 6600
for k,v in opts:
if k == '-a':
addr = v
elif k == '-p':
port = int(v)
fmlyric = FMLyric(addr, port)
fmlyric.run()