-
Notifications
You must be signed in to change notification settings - Fork 3
/
serve.py
executable file
·197 lines (165 loc) · 4.99 KB
/
serve.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python2
from bottle import route, run, request, get, post, static_file
import gi
gi.require_version('Playerctl', '1.0')
from gi.repository import GLib, GObject, Playerctl
from subprocess import Popen
import sys
import os
from urllib import unquote
Popen([sys.executable, './sendup.py']) #run the websocket script (used to update info)
############
# SETTINGS
############
links = [['DevPy','http://DevPy.me'],['Reddit','http://Reddit.com'],['Google','http://Google.com']]
theme = 'dark' #light or dark
############
# /SETTINGS
############
if theme == 'dark':
bgc = '#222'
fgc = 'rgba(255,255,255,0.6)'
bw = 'gray'
else:
bgc = '#fff'
fgc = '#111'
bw = 'black'
for x in range(len(links)):
links[x] = '<a class="db link no-underline underline-hover '+bw+'" href="'+links[x][1]+'">'+links[x][0]+'</a>'
linkhtml = '''<div class="db center" style="width: 182px;"><div class="mt4 lh-copy">'''
for x in links:
linkhtml += x
linkhtml += '''</div></div>'''
@get('/') # or @route('/login')
def index():
player = Playerctl.Player()
try:
title = player.get_title()
artist = player.get_artist()
if player.get_properties('player-name')[0] == 'vlc':
imgpath = unquote(os.popen("playerctl metadata mpris:artUrl").read()).replace('file://', '')
with open('artwork.jpg', 'wb') as artwork:
artwork.write(open(imgpath, 'rb').read())
img = '/static/artwork.jpg?{0}'.format(player.get_title())
else:
img = os.popen("playerctl metadata mpris:artUrl").read()
except:
title = ''
artist = ''
img = ''
return '''
<html>
<head>
<title>Start Page</title>
<link rel="stylesheet" href="/static/tachyons.css" />
<link rel="stylesheet" href="/static/font-awesome.min.css" />
<style>
/* fallback */
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: local('Material Icons'), local('MaterialIcons-Regular'), url(/static/mat.woff2) format('woff2');
}
.material-icons {
cursor: pointer;
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 30px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
-webkit-font-feature-settings: 'liga';
-webkit-font-smoothing: antialiased;
}
.material-icons.md-light { color: rgba(0, 0, 0, 0.54); }
.material-icons.md-dark { color: rgba(255, 255, 255, 1); }
</style>
</head>
<body style="background-color:'''+bgc+'''">
<div style="position:relative; top: 35%%; transform: translateY(-50%%); ">
<div class="mt4 db center black link" style="width: 178px;">
<img id="img" style="width:180px;height:178px;background-color:#222;"class="db ba b--black-10" src="%s">
<dl class="mt2 lh-copy">
<dt class="clip">Title</dt>
<dd id="title" class="ml0 fw9" style="color:''' % (img)+fgc+'''">%s</dd>
<dt class="clip">Artist</dt>
<dd id="artist" class="ml0 gray">%s</dd>
</dl>
<div style="text-align:center;color:''' % (title, artist)+fgc+'''">
<a onclick="previous();" style="float:left;"><i class="material-icons md-'''+theme+'''">skip_previous</i></a>
<a onclick="toggle();"><i id ="stateicon" style="width:32px;" class="material-icons md-'''+theme+'''">pause_arrow</i></a>
<a onclick="next();" style="float:right;"><i class="material-icons md-'''+theme+'''">skip_next</i></a>
</div>
</div>
''' + linkhtml + '''
</div>
<script src="/static/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
var ws;
ws = new WebSocket("ws://0.0.0.0:7000/websocket");
ws.onopen = function (evt) {
ws.send("Connected");
};
ws.onclose = function (evt) {
ws.send("Closed");
};
ws.onmessage = function (evt) {
var array = evt.data.split(',');
if (array.length == 4) {
console.log(array);
$("#img").attr("src", array[0]);
$("#title").text(array[1]);
$("#artist").text(array[2]);
if (array[3] === 'Playing') {
$('#stateicon').text('pause');
} else {
$('#stateicon').text('play_arrow');
}
ws.send("Updated");
} else {
ws.send("OK.")
}
};
</script>
<script>
function next() {
$.post('/next')
return false;
}
function previous() {
$.post('/previous')
return false;
}
function toggle() {
$.post('/toggle');
return false;
}
</script>
</body>
</html>
'''
@post('/next')
def next_song():
Playerctl.Player().next()
@post('/previous')
def next_song():
Playerctl.Player().previous()
@post('/toggle')
def next_song():
Playerctl.Player().play_pause()
@post('/status')
def is_playing():
return Playerctl.Player().get_property("status")
@route('/static/<filename:path>')
def send_static(filename):
return static_file(filename, root=os.path.dirname(os.path.realpath(' ')))
@route('/fonts/<filename:path>')
def send_static(filename):
return static_file(filename, root=os.path.dirname(os.path.realpath(' ')))
run(host='0.0.0.0', port=8080)