-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
87 lines (71 loc) · 2.42 KB
/
main.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
#!/usr/bin/env python
import mpd
import subprocess
import web
import socket
from web.contrib.template import render_jinja
def getMPDStatus():
client = mpd.MPDClient() # Init MPD Client
client.connect("localhost", 6600) # Connect to local MPD Server
cs = client.currentsong() # Get the currentsong dict
if 'title' in cs: # Check to see if "title" title exists in the dict
songtitle = cs['title'] # If it does set songtitle to the id3 title
elif 'file' in cs: # If it doesn't have a title use the filename
songtitle = cs['file'] # Set songtitle to the filename
songartist = cs['artist'] # Set songartist if the song has one
songalbum = cs['album'] # Set the songalbum if the song has one
return '%s by %s from %s' % (songtitle, songartist, songalbum)
def getMPDLyrics():
lyrics = subprocess.Popen("lyricsdownloader", stdout=subprocess.PIPE).communicate() #get lyrics
if lyrics:
return lyrics[0]
else:
return "Error, please restart MPD" #We no got lyrics
def getMPDTime():
client = mpd.MPDClient() # Init MPD Client
client.connect("localhost", 6600) # Connect to local MPD Server
time = client.status()["time"]
return time
"""
Here is where the web part starts
"""
################ WEB #######################
##############################################
urls = (
'/', 'homepage',
'/control/(.*)', 'control',
'/lyrics', 'lyrics',
'/title', 'title',
'/time', 'time',
)
app = web.application(urls, globals())
render = render_jinja(
'templates',
encoding = 'utf-8',
)
class homepage:
def GET(self, hostname=socket.gethostname()):
return render.hello(hostname=hostname)
class control:
def GET(self, cmd):
if cmd == "toggle":
subprocess.Popen(["mpc", "toggle"])
elif cmd == "next":
subprocess.Popen(["mpc", "next"])
elif cmd == "prev":
subprocess.Popen(["mpc", "prev"])
return cmd
class lyrics:
def GET(self):
lyricdata = getMPDLyrics()
return lyricdata
class title:
def GET(self):
titledata = getMPDStatus()
return titledata
class time:
def GET(self):
timedata = getMPDTime()
return timedata
if __name__ == "__main__":
app.run()