This repository has been archived by the owner on Jun 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.py
executable file
·137 lines (117 loc) · 4.61 KB
/
app.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
#!/usr/bin/env python
'''
https://github.com/jboynyc/cmus_app
'''
# =======================================================================
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# =======================================================================
from optparse import OptionParser
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import SafeConfigParser as ConfigParser
from bottle import abort, post, request, response, route, run, view, static_file
from sh import cmus_remote, ErrorReturnCode_1
class ConfigFileNotFound(IOError):
'''Raised when the specified config file does not exist or is empty.'''
pass
class MissingSetting(Exception):
'''Raised when the config file is missing a required setting.'''
pass
def read_config(config_file):
r = {}
try:
config_parser = ConfigParser(inline_comment_prefixes=';')
except TypeError:
config_parser = ConfigParser()
n = config_parser.read(config_file)
if not len(n):
raise ConfigFileNotFound(config_file)
section = 'cmus_app'
fields = ['cmus_host', 'cmus_passwd', 'app_host', 'app_port']
for field in fields:
try:
r[field] = config_parser.get(section, field)
except:
raise MissingSetting(field)
return r
@route('/')
@view('main')
def index():
return {'host': settings['cmus_host']}
@post('/cmd')
def run_command():
legal_commands = {'Play': 'player-play',
'Stop': 'player-stop',
'Next': 'player-next',
'Previous': 'player-prev',
'Increase Volume': 'vol +1%',
'Reduce Volume': 'vol -1%',
'Mute': 'vol 0'}
command = request.POST.get('command', default=None)
if command in legal_commands:
try:
out = Remote('-C', legal_commands[command])
return {'result': out.exit_code, 'output': out.stdout.decode()}
except ErrorReturnCode_1:
abort(503, 'Cmus not running.')
else:
abort(400, 'Invalid command.')
@route('/status')
def get_status():
try:
out = Remote('-Q').stdout.decode().split('\n')
r = {}
play = out[0].split()[1]
if play == 'playing':
r['playing'] = True
elif play == 'stopped':
r['playing'] = False
info = [i for i in out if i.startswith(('tag', 'set'))]
for i in info:
k, v = i.split()[1], i.split()[2:]
if len(v):
r[k] = ' '.join(v)
return r
except ErrorReturnCode_1:
abort(503, 'Cmus not running.')
@route('/static/<file>')
def static(file):
response.set_header('Cache-Control', 'max-age=604800')
return static_file(file, root='static')
@route('/favicon.ico')
def favicon():
response.set_header('Cache-Control', 'max-age=604800')
return static_file('favicon.ico', root='static')
if __name__ == '__main__':
option_parser = OptionParser()
option_parser.add_option('-f', '--config', dest='config_file',
help='Location of configuration file.')
option_parser.add_option('-c', '--cmus-host', dest='cmus_host',
help='Name of cmus host.',
default='localhost')
option_parser.add_option('-w', '--cmus-passwd', dest='cmus_passwd',
help='Cmus password.',
default='')
option_parser.add_option('-a', '--app-host', dest='app_host',
help='Name of cmus_app host.',
default='localhost')
option_parser.add_option('-p', '--app-port', dest='app_port',
help='Port cmus_app is listening on.',
default=8080)
options, _ = option_parser.parse_args()
if options.config_file:
settings = read_config(options.config_file)
else:
settings = vars(options)
Remote = cmus_remote.bake(['--server', settings['cmus_host'],
'--passwd', settings['cmus_passwd']])
run(host=settings['app_host'], port=settings['app_port'])