diff options
Diffstat (limited to 'library.py')
-rwxr-xr-x | library.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/library.py b/library.py new file mode 100755 index 0000000..762b18b --- /dev/null +++ b/library.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +import configparser +import mutagen +import os + +from models.track import Track + + +def run(path=None): + print("Scanning files") + + if path is not None: + if os.path.isdir(path): + store_dir(path) + else: + store_file(path) + else: + store_dir("/media/Music") + + +def store_file(path): + m = mutagen.File(path, easy=True) + if m: + if not Track.store(path, m): + print("Problem saving %s" % (path,)) + + +def store_dir(path): + file_store = [] + + for root, dirs, files in os.walk(path): + for name in files: + file_path = "".join([root, "/", name]) + file_store.append(file_path) + + file_store.sort() + j = 0 + media_files = 0 + print("Storing files") + for file_path in file_store: + j += 1 + m = mutagen.File(file_path, easy=True) + if m: + if not Track.store(file_path, m): + print("Problem saving %s" % (file_path,)) + + media_files += 1 + print( + "%d%% complete, (%d files)" % (((j / len(file_store)) * 100), + j) + ) + print("Stored %d tracks" % (media_files,)) + + +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__": + config = configparser.ConfigParser() + config.read("mach2.ini") + + run(config["DEFAULT"]["media_dir"]) |