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
|
#!/usr/bin/env python
import configparser
import gevent
from gevent import monkey, queue
import logging
import mutagen
import os
from db.db_manager import DbManager
from models.track import Track
file_store = queue.Queue()
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
def store_track_task():
while not file_store.empty():
path = file_store.get()
m = mutagen.File(path, easy=True)
Track.store(path, m)
gevent.sleep(0)
def run(path=None):
db = DbManager()
if path is not None:
if os.path.isdir(path):
store_dir(path)
else:
store_file(path)
else:
store_dir("/media/Music")
db.export()
def store_file(path):
logger = logging.getLogger("store_file")
m = mutagen.File(path, easy=True)
if m:
if not Track.store(path, m):
logger.error("Problem saving %s" % (path,))
def store_dir(path):
logger = logging.getLogger("store_dir")
logger.info("Scanning files")
allowed_extensions = [".mp3", ".ogg", ".flac", ".wav", ".aac", ".ape"]
for root, dirs, files in os.walk(path):
for name in files:
file_path = "".join([root, "/", name])
file, ext = os.path.splitext(file_path)
if ext.lower() in allowed_extensions:
file_store.put(file_path)
logger.info("Storing tracks")
gevent.joinall([gevent.spawn(store_track_task)] * 6)
logger.info("Done")
def delete_file(path):
track = Track.find_by_path(path)
if track:
track_album = track.album
track_artists = track.artists
track.delete()
if track_album and len(track_album.tracks) == 0:
track_album.delete()
for artist in track_artists:
if len(artist.tracks) == 0:
artist.delete()
def update_file(path):
m = mutagen.File(path, easy=True)
if m:
track = Track.find_by_path(path)
track.update(m)
def update_track_filename(oldpath, newpath):
track = Track.find_by_path(oldpath)
track.filename = newpath
track.save()
if __name__ == "__main__":
monkey.patch_all()
config = configparser.ConfigParser()
config.read("mach2.ini")
run(config["DEFAULT"]["media_dir"])
|