-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpandora.py
executable file
·444 lines (362 loc) · 16.6 KB
/
pandora.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/python
'''
sinusbot script: pandora 07.04.2017 Erebos
'''
# standard libs
import os
import datetime
import hashlib
import uuid
import sqlite3
import threading
import queue
import traceback
# other libs
from tinytag import TinyTag
'''path to sinusbot root_dir'''
root_dir = None
# queues for communication between threads
q_create_slink = queue.Queue()
q_to_db = queue.Queue()
# CLIIO class -> handles all command line input/output
class CLIIO:
# ASCII banner
ascii_banner=' \n'\
' ██▓███ ▄▄▄ ███▄ █ ▓█████▄ ▒█████ ██▀███ ▄▄▄ \n'\
'▓██░ ██▒▒████▄ ██ ▀█ █ ▒██▀ ██▌▒██▒ ██▒▓██ ▒ ██▒▒████▄ \n'\
'▓██░ ██▓▒▒██ ▀█▄ ▓██ ▀█ ██▒░██ █▌▒██░ ██▒▓██ ░▄█ ▒▒██ ▀█▄ \n'\
'▒██▄█▓▒ ▒░██▄▄▄▄██ ▓██▒ ▐▌██▒░▓█▄ ▌▒██ ██░▒██▀▀█▄ ░██▄▄▄▄██ \n'\
'▒██▒ ░ ░ ▓█ ▓██▒▒██░ ▓██░░▒████▓ ░ ████▓▒░░██▓ ▒██▒ ▓█ ▓██▒\n'\
'▒▓▒░ ░ ░ ▒▒ ▓▒█░░ ▒░ ▒ ▒ ▒▒▓ ▒ ░ ▒░▒░▒░ ░ ▒▓ ░▒▓░ ▒▒ ▓▒█░\n'\
'░▒ ░ ▒ ▒▒ ░░ ░░ ░ ▒░ ░ ▒ ▒ ░ ▒ ▒░ ░▒ ░ ▒░ ▒ ▒▒ ░\n'\
'░░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ▒ ░░ ░ ░ ▒ \n'\
' ░ ░ ░ ░ ░ ░ ░ ░ ░\n'\
' ░ \n'
# run!
@classmethod
def run(cls):
root_dir = BOX.os(os.name)
argv = os.sys.argv[1:]
if len(argv) > 0:
counter = 0
while argv[counter:]:
re_value = BOX.commands(argv[counter].strip('-')[0])(*argv[counter:])
cls.print_to_shell(re_value[0] + '\n')
counter += re_value[1]
else:
cls.print_to_shell(f'\x1b[31m{cls.ascii_banner}\x1b[0m')
cls.print_to_shell(f'root_dir {root_dir}\n')
cls.print_to_shell(BOX.commands('help'[0])()[0])
for cmd in cls.get_cmd('>> '):
cmd = cmd.split(' ', 1)
cls.print_to_shell(BOX.commands(cmd[0][0])(*cmd)[0])
@staticmethod
def get_cmd(placeholder):
while True:
yield input(placeholder)
@staticmethod
def get_input(placeholder):
return input(placeholder)
@staticmethod
def print_to_shell(output_str):
print(output_str)
# FILEIO class -> handles all input/output from/to files
class FILEIO:
@staticmethod
def create_slink_t(pill2kill): # <- code of create_slink_t thread
fe_log = None
try:
while not pill2kill.is_set() or q_create_slink.full():
try:
source, e_path_n = q_create_slink.get(block=True) # -> wait for input
link_dst = [f'{root_dir}data/store/' + str(*name) + f'/{e_path_n}' for name in FILEIO.get_folderns_and_files(f'{root_dir}data/store/') if isinstance(name, tuple)][1]
os.symlink(source, link_dst)
q_create_slink.task_done()
except Exception as e:
fe_log = FILEIO.write_to_log('fe_log.txt', f'S_LINK_Error: {e}\n{traceback.format_exc()}')
continue
finally:
if fe_log:
CLIIO.print_to_shell('file create_slink_t error -> {root_dir}fe_log.txt')
CLIIO.print_to_shell('create_slink_t closed')
@staticmethod
def get_folderns_and_files(source_dir):
for path, subdirs, files in os.walk(source_dir):
yield (path.split('/')[-1],)
if subdirs:
yield subdirs
for name in files:
yield os.path.join(path, name)
@staticmethod
def del_all_slinks():
sldir = [f'{root_dir}data/store/' + str(*name) + '/' for name in FILEIO.get_folderns_and_files(f'{root_dir}data/store/') if isinstance(name, tuple)][1]
for path, subdirs, files in os.walk(sldir):
for name in files:
os.remove(os.path.join(path, name))
@staticmethod
def write_to_log(name, log_msg):
with open(f'{root_dir}{name}', 'a') as log:
log.write(log_msg)
return True
# DBIO class -> handles all input/output from/to database(sqlite file... I know)
class DBIO:
# creating command of the tabel files: db_conn.execute("CREATE TABLE files ( uuid STRING PRIMARY KEY ON CONFLICT IGNORE, artist STRING NOT NULL, title STRING NOT NULL, album STRING NOT NULL, trackinfo STRING NOT NULL, created DATETIME NOT NULL, createdby STRING NOT NULL , type STRING NOT NULL DEFAULT('file'), playcount INT NOT NULL DEFAULT(0), size INT NOT NULL DEFAULT(0));")
# meths
@staticmethod
def to_db_t(pill2kill): # <- code of to_db_t thread
dbe_log = None
db_conn = None
try:
while not pill2kill.is_set() or q_to_db.full():
try:
evil = q_to_db.get(block=True) # -> wait for input
db_conn = sqlite3.connect([path for path in FILEIO.get_folderns_and_files(f'{root_dir}data/db/') if isinstance(path, str) and path != '/opt/ts3soundboard/data/db/global.sqlite'][0])
db_cursor = db_conn.cursor()
if isinstance(evil, EVILS):
values_for_db = evil.for_db()
db_cursor.execute(f"INSERT INTO files (uuid, artist, title, album, trackinfo, created, createdby, type, playcount, size) VALUES ('{values_for_db['uuid']}', '{values_for_db['artist']}', '{values_for_db['title']}', '{values_for_db['album']}', '{values_for_db['trackinfo']}', '{values_for_db['created']}', '{values_for_db['createby']}', '{values_for_db['type']}', '{values_for_db['playcount']}', '{values_for_db['size']}');")
db_conn.commit()
else:
values_for_db = evil.for_db()
db_cursor.execute(f"INSERT INTO files (uuid, artist, title, album, trackinfo, created, createdby, type, playcount, size) VALUES ('{values_for_db['uuid']}', '{values_for_db['artist']}', '{values_for_db['title']}', '{values_for_db['album']}', '{values_for_db['trackinfo']}', '{values_for_db['created']}', '{values_for_db['createby']}', '{values_for_db['type']}', '{values_for_db['playcount']}', '{values_for_db['size']}');")
db_conn.commit()
q_to_db.task_done()
except Exception as e:
dbe_log = FILEIO.write_to_log('dbe_log.txt', f'INSERT_DB_Error: {e}\n{traceback.format_exc()}')
continue
finally:
if dbe_log:
CLIIO.print_to_shell('database to_db_t error -> {root_dir}dbe_log.txt')
if db_conn:
db_conn.close()
CLIIO.print_to_shell('to_db_t closed')
@staticmethod
def del_db():
dbe_log = None
db_conn = None
try:
db_conn = sqlite3.connect([path for path in FILEIO.get_folderns_and_files(f'{root_dir}data/db/') if isinstance(path, str) and path != '/opt/ts3soundboard/data/db/global.sqlite'][0])
db_cursor = db_conn.cursor()
db_cursor.execute("DELETE FROM files;")
db_conn.commit()
return True
except Exception as e:
dbe_log = FILEIO.write_to_log('dbe_log.txt', f'INSERT_DB_Error: {e}\n{traceback.format_exc()}')
return False
finally:
if dbe_log:
CLIIO.print_to_shell('database del_db error')
if db_conn:
db_conn.close()
CLIIO.print_to_shell('database closed\n')
# TOOLBOX class
class TOOLBOX:
# meths
@staticmethod
def get_hash(path, hash_type='sha256'):
func = getattr(hashlib, hash_type)()
with open(path, 'rb') as f:
for block in iter(lambda: f.read(2048*func.block_size), b''):
func.update(block)
return str(func.hexdigest())
@staticmethod
def get_time():
return datetime.datetime.now()
# Welp, I know ignoring errors is... kinda stupid
@staticmethod
def ignore_exception(IgnoreException=Exception, DefaultVal=None):
def dec(function):
def _dec(*args, **kwargs):
try:
return function(*args, **kwargs)
except IgnoreException:
return DefaultVal
return _dec
return dec
# BOX object
class BOX:
# meths
@classmethod
def os(cls, os):
return{
'posix': lambda: '/opt/ts3soundboard/',
'nt': lambda: 'C:\\SinusBot\\',
'mac': lambda: os.sys.exit()
}.get(os, lambda: cls.help)()
@classmethod
def commands(cls, cmd):
return{
'a': lambda: cls.add,
'h': lambda: cls.help,
'e': lambda: os.sys.exit()
}.get(cmd, lambda: cls.help)()
@classmethod
def add(cls, *args):
ae_log = None
arg_counter = 1
if not args[1:]:
args = (args[0], 'noarg')
try:
DBIO.del_db()
FILEIO.del_all_slinks()
pill2kill = threading.Event() # -> cyanide pills
create_slink_thread = threading.Thread(target=FILEIO.create_slink_t, args=(pill2kill,))
create_slink_thread.daemon = True # -> dies after main thread is closed
create_slink_thread.start()
to_db_thread = threading.Thread(target=DBIO.to_db_t, args=(pill2kill,))
to_db_thread.daemon = True
to_db_thread.start()
for index, arg in enumerate(args[1:]):
arg = arg.strip('\'\"')
for evil in cls.get_EVIL(arg):
if isinstance(evil, EVILS):
CLIIO.print_to_shell(evil.get_song())
CLIIO.print_to_shell(f'{evil}\n')
q_create_slink.put((evil.get_song(), evil.get_path()))
q_to_db.put(evil)
elif isinstance(evil, EVIL):
CLIIO.print_to_shell(f'\n\n{evil.get_title()}\t{evil.get_uuid()}\n\n\n')
q_to_db.put(evil)
elif evil:
ae_log = evil
arg_counter += 1
finally:
if ae_log:
CLIIO.print_to_shell('adding error -> {root_dir}ae_log')
pill2kill.set()
create_slink_thread.join()
to_db_thread.join()
return ('done', arg_counter)
# recursive EVIL object generator -> literally evil code
@classmethod
def get_EVIL(cls, arg, parent=''):
ae_log = None
'''supp_formats = ('.flac', '.mp3', '.m4a', '.ogg', '.wav', '.wma', '.opus', '.mp4')'''
supp_formats = ('.flac', '.mp3', '.m4a', '.ogg', '.wav', '.wma', '.opus')
if os.path.isdir(arg):
arg_last_c = arg[-1]
for dn_or_s in FILEIO.get_folderns_and_files(arg):
try:
if isinstance(dn_or_s, tuple):
parent_dir = dn_or_s[0]
if parent_dir:
evil = EVIL(parent_dir, parent)
parent = evil.get_uuid()
yield evil
elif isinstance(dn_or_s, list):
for dn in dn_or_s:
if arg_last_c is '/':
yield from cls.get_EVIL(f'{arg}{dn}', parent)
else:
yield from cls.get_EVIL(f'{arg}/{dn}', parent)
return
elif dn_or_s.endswith(supp_formats):
yield EVILS(dn_or_s, parent)
except Exception as e:
ae_log = FILEIO.write_to_log('ae_log.txt', f'ADD_Error: {dn_or_s}: {e}\n{traceback.format_exc()}')
continue
elif arg.endswith(supp_formats):
yield EVILS(arg, parent)
else:
CLIIO.print_to_shell('What song do you want to add?')
cls.get_EVIL([CLIIO.get_input('> ').strip('\'\"')])
yield ae_log
@classmethod
def help(cls, *args):
return ('useable commands and arguments are:\n\thelp\t--help -h\n\tadd\t--add -a\n\texit\t--exit -e', 1)
# super EVIL object -> data of folders
class EVIL:
# fields
_uuid = ''
_artist = ''
_title = ''
_album = ''
_trackinfo = ''
_parent = ''
_created = ''
_createby = 'fd3591e0-97e0-4a5f-ad08-697c37f0e2bc'
_type = 'folder'
_playcount = 0
_size = 0
# ctor
def __init__(self, title, parent=''):
self._uuid = str(uuid.uuid4())
self._artist = self._artist
self._title = title
self._album = self._album
self._created = TOOLBOX.get_time()
self._createby = self._createby
self._type = self._type
self._playcount = self._playcount
self._size = self._size
self._parent = parent
self._trackinfo = f'{{"v":1,"uuid":"{self._uuid}","parent":"{self._parent}","type":"{self._type}","path":"","title":"{self._title}"}}'
def __repr__(self):
return f'{self.__class__.__name__}({self._title})'
def __str__(self):
return f'{self._uuid}, {self._artist}, {self._title}, {self._album}, {self._trackinfo}, {self._parent}, {self._created}, {self._createby}, {self._type}, {self._playcount}'
# props
def get_uuid(self):
return self._uuid
def get_title(self):
return self._title
def for_db(self):
return {
'uuid': self._uuid,
'artist': self._artist.replace('\'', '\'\''),
'title': self._title.replace('\'', '\'\''),
'album': self._album.replace('\'', '\'\''),
'trackinfo': self._trackinfo.replace('\'', '\'\''),
'created': self._created,
'createby': self._createby,
'type': self._type,
'playcount': self._playcount,
'size': self._size
}
# EVILS object -> data of songs
class EVILS(EVIL):
# fields
_song = ''
_tag = ''
_path = ''
_duration = 0
_bitrate = 0
_samplerate = 0
_filesize = 0
_filename = ''
_track = 4
_genre = ''
# ctor
def __init__(self, song, parent=''):
self._song = song
self._tag = TinyTag.get(song)
self._path = TOOLBOX.get_hash(song)
self._artist = self._tag.artist or self._artist
self._title = self._tag.title or self._title
self._album = self._tag.album or self._album
self._duration = int(self._tag.duration * 1000)
self._bitrate = int(self._tag.bitrate * 1000)
self._samplerate = int(self._tag.samplerate)
self._filesize = self._tag.filesize
self._filename = self._tag.title or self._filename
self._track = TOOLBOX.ignore_exception()(int)(self._tag.track) or self._track
self._genre = self._tag.genre or self._genre
super().__init__(self._title, parent)
self._type = 'file'
self._trackinfo = f'{{"v":1,"uuid":"{self._uuid}","parent":"{self._parent}","type":"{self._type}","path":"{self._path}","codec":"-","duration":{self._duration},"bitrate":{self._bitrate},"samplerate":{self._samplerate},"filesize":{self._filesize},"filename":"{self._filename}","title":"{self._title}","artist":"{self._artist}","album":"{self._album}","track":{self._track},"genre":"{self._genre}"}}'
def __repr__(self):
return f'{self.__class__.__name__}({self._song})'
def __str__(self):
return f'{self._uuid}, {self._artist}, {self._title}, {self._album}, {self._trackinfo}, {self._created}, {self._createby}, {self._type}, {self._playcount}, {self._filesize}'
# props
def get_song(self):
return self._song
def get_path(self):
return self._path
try:
if __name__ == '__main__':
os.sys.exit(CLIIO.run())
except Exception as e:
CLIIO.print_to_shell(f'Oh no! Something has gone wrong. {e}\n{traceback.format_exc()}')
finally:
print('cya')