-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·264 lines (210 loc) · 5.76 KB
/
server.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python3
from flask import Flask,make_response,abort
from mutagen.easyid3 import EasyID3
from mutagen import File
from threading import Timer
import sqlite3 as sqlite
import os, sys
app = Flask(__name__)
# Set musics directory
def error():
print(
"""
USAGE: server.py [-p <PORT>] <MUSIC>
<PORT> is port number, default 80
<MUSIC> must be the absolute path of you musics folder
""")
exit()
### CONFIGURATIONS ###
# Database file used for indexing songs
DB_FILE = os.path.abspath('songs.db')
# The cover file that used when no cover is available.
# Replace with False to disable it
NO_COVER = os.path.abspath('no_cover.jpg')
# Host ip or name. you often don't need to change it
HOST = '0.0.0.0'
# Note: for port <= 1024, you need superuser access.
PORT = 80
# app debug. disable it for releases.
app.debug = False
class Database:
def __init__(self):
self.open()
def open(self):
self.conn = sqlite.connect(DB_FILE)
self.cur = self.conn.cursor()
def create_table(self):
self.cur.executescript('''
DROP TABLE IF EXISTS Songs;
CREATE TABLE Songs(Artist TEXT, Album TEXT, Title TEXT, Path TEXT);
''')
self.conn.commit()
def _songs_table_exist(self):
query = 'SELECT COUNT(*) FROM sqlite_master WHERE type="table" AND name="Songs";'
self.cur.execute(query)
self.conn.commit()
result = self.cur.fetchone()[0]
return bool(result)
def count_records(self):
if not self._songs_table_exist():
return 0
query = 'SELECT COUNT(*) FROM Songs'
self.cur.execute(query)
self.conn.commit()
count = self.cur.fetchone()[0]
return int(count)
def __len__(self):
return self.count_records()
def _insert_song(self,song):
query = 'INSERT INTO Songs VALUES(?, ?, ?, ?)'
self.cur.execute(query,
(
song.artist,
song.album,
song.title,
song.path,
),
)
def insert(self,song):
self._insert_song(song)
self.conn.commit()
def batch_insert(self, songs):
for song in songs:
self._insert_song(song)
self.conn.commit()
def get_song(self, artist, album, title='any'):
if title == 'any':
query = 'SELECT Path FROM Songs WHERE Artist=? AND Album=?'
self.cur.execute(query,(artist,album))
else:
query = 'SELECT Path FROM Songs WHERE Artist=? AND Album=? AND Title=?'
self.cur.execute(query,(artist,album,title))
self.conn.commit()
path = self.cur.fetchone()
return Song(artist,album,title,path)
def close(self):
self.conn.close()
def __del__(self):
self.close()
class Song:
def clean_arg(string):
string = str(string)
if string.endswith(']'):
return string[2:-2]
elif string.endswith(')'):
return string[2:-3]
else:
return string
def __init__(self, artist, album, title, path):
self.artist = Song.clean_arg(artist)
self.album = Song.clean_arg(album)
self.title = Song.clean_arg(title)
self.path = Song.clean_arg(path)
def __str__(self):
return '{} by {} from {}'.format(self.title, self.artist, self.album)
def song_from_path(path):
song = EasyID3(path)
try:
artist = song['artist']
except:
artist = ''
try:
album = song['album']
except:
album = ''
try:
title = song['title']
except:
title = ''
return Song(artist=artist,
album=album,
title=title,
path=path)
def list_songs():
songs = []
for root, dirs, files in os.walk(MUSIC_DIR):
for file in files:
if file.endswith(".mp3"):
path = os.path.join(root, file)
song = song_from_path(path)
songs.append(song)
return songs
def update_database(db):
Timer(10 * 60, update_database, [db]).start()
songs = list_songs()
if len(db) == len(songs):
# no need to update
return
db.create_table()
db.batch_insert(songs)
def init_database():
if not os.path.isfile(DB_FILE):
db = Database()
print('Indexing musics. Please wait...')
try:
update_database(db)
finally:
db.close()
def get_cover_art(song):
if song.path == 'None':
return None
file = File(song.path)
APIC = None
for key in file.keys():
if 'APIC:' in key:
APIC = key
if APIC is None:
return None
artwork = file.tags[APIC].data
return artwork
def generate_response(Artist, Album, Title='any'):
db = Database()
song = db.get_song(Artist, Album, Title)
db.close()
cover_raw = get_cover_art(song)
if cover_raw is None:
if NO_COVER == False:
abort(404)
else:
with open(NO_COVER, 'rb') as f:
cover_raw = f.read()
resp = make_response(cover_raw)
resp.content_type = "image/jpeg"
return resp
def is_title(title):
return not title in ['cover','folder','%placeholder_filename']
@app.route("/<Artist>/<Album>/Covers/front.jpg")
def album_cover(Artist, Album):
return generate_response(Artist, Album)
@app.route("/<Artist>/<Album>/artwork/front.jpg")
def album_artwork(Artist, Album):
return generate_response(Artist, Album)
@app.route("/<Artist>/<Album>/<Title>.jpg")
def song_cover_jpg(Artist, Album, Title):
if is_title(Title):
return generate_response(Artist, Album, Title)
else:
return generate_response(Artist, Album)
def main():
global MUSIC_DIR, PORT
if len(sys.argv) < 2:
error()
MUSIC_DIR = str(sys.argv[-1])
if not os.path.exists(MUSIC_DIR):
print('{} doesn\'t exist.\n'.format(MUSIC_DIR))
error()
if '-p' in sys.argv:
try:
idx = sys.argv.index('-p')
arg = sys.argv[idx+1]
PORT = int(arg)
except Exception:
error()
init_database()
try:
app.run(host=HOST,port=PORT)
except PermissionError:
print('ERROR: can\'t open port {}: you have not enough premissions.'.format(PORT))
exit()
if __name__ == '__main__':
main()