-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmlyric.py
executable file
·76 lines (61 loc) · 1.46 KB
/
fmlyric.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
#! /usr/bin/env python2
import sys
import getopt
import socket
import time
import json
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.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.conn.connect((socket.gethostbyname(self.addr), self.port))
except:
print 'Connect to FMD failed. Is FMD running?'
return
while True:
try:
time.sleep(self.interval)
except:
print 'Quit.'
self.conn.send('bye')
self.conn.close()
self.notilyric.close()
break
try:
self.conn.send('info')
obj = json.loads(self.conn.recv(4096))
except:
print 'Server quit.'
self.conn.close()
self.notilyric.close()
break
status = obj['status']
if status != 'play':
self.notilyric.close()
continue
artist = obj['artist'].encode('utf-8')
title = obj['title'].encode('utf-8')
progress = obj['pos']
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 = 10098
for k,v in opts:
if k == '-a':
addr = v
elif k == '-p':
port = int(v)
fmlyric = FMLyric(addr, port)
fmlyric.run()