82 lines
1.8 KiB
Python
Executable File
82 lines
1.8 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
import argparse
|
|
import hashlib
|
|
import logging
|
|
import os
|
|
|
|
import pytiger.logging.config
|
|
|
|
from tinytag import TinyTag
|
|
|
|
from model import Tracks, session
|
|
|
|
|
|
# "Constants"
|
|
ROOT = "/home/kae/music"
|
|
# Instantiate logging
|
|
pytiger.logging.config.basic_config(stderr=False, level=logging.INFO)
|
|
log = logging.getLogger(__name__)
|
|
log.info("Starting")
|
|
|
|
|
|
def main():
|
|
"Main loop"
|
|
|
|
# Parse command line
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument('-u', '--update',
|
|
action="store_true", dest="update",
|
|
default=True, help="Update database")
|
|
args = p.parse_args()
|
|
|
|
# Run as required
|
|
if args.update:
|
|
log.info("Updating database")
|
|
update_db()
|
|
|
|
log.info("Finished")
|
|
|
|
|
|
def update_db():
|
|
"""
|
|
Repopulate database
|
|
|
|
TODO: remove missing files
|
|
"""
|
|
|
|
count = 0
|
|
for root, dirs, files in os.walk(ROOT):
|
|
for f in files:
|
|
count += 1
|
|
path = os.path.join(root, f)
|
|
ext = os.path.splitext(f)[1]
|
|
if ext in [".flac", ".mp3"]:
|
|
track = Tracks.get_or_create(os.path.relpath(path, ROOT))
|
|
tag = TinyTag.get(path)
|
|
track.title = tag.title
|
|
track.artist = tag.artist
|
|
track.length = int(tag.duration * 1000)
|
|
track.mtime = os.path.getmtime(path)
|
|
elif ext not in [".jpg"]:
|
|
print(f"Unrecognised file type: {path}")
|
|
|
|
session.commit()
|
|
|
|
print(f"{count} files processed")
|
|
|
|
|
|
def md5(path):
|
|
"https://stackoverflow.nl9om/questions/3431825/"
|
|
"generating-an-md5-checksum-of-a-file"
|
|
|
|
hash_md5 = hashlib.md5()
|
|
with open(path, "rb") as f:
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
hash_md5.update(chunk)
|
|
return hash_md5.hexdigest()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|