built library in-memory database with file tags
This commit is contained in:
parent
b0e4b956a9
commit
43efcfc0bf
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
*.json
|
||||||
|
|
70
musik.py
70
musik.py
|
@ -8,10 +8,9 @@ import json
|
||||||
import random
|
import random
|
||||||
import subprocess
|
import subprocess
|
||||||
from urllib import parse
|
from urllib import parse
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
from flask import Flask, Response, render_template, send_file
|
from flask import Flask, Response, render_template, send_file
|
||||||
from werkzeug.utils import secure_filename
|
import mutagen
|
||||||
|
|
||||||
MUSIC_DIR = "/mnt/music/Music"
|
MUSIC_DIR = "/mnt/music/Music"
|
||||||
MUSIC_EXT = ['flac', 'mp3', 'wav', 'm4a']
|
MUSIC_EXT = ['flac', 'mp3', 'wav', 'm4a']
|
||||||
|
@ -25,29 +24,66 @@ FFMPEG_CMD = [
|
||||||
'-'
|
'-'
|
||||||
]
|
]
|
||||||
|
|
||||||
def build_tree(root_dir):
|
class Track:
|
||||||
"""Walks the music directory and builds a tree."""
|
def __init__(self, filepath=None, d=None):
|
||||||
print("Building tree.")
|
if d:
|
||||||
tree = defaultdict(dict)
|
self._from_dict(d)
|
||||||
for dirName, subDirs, files in os.walk(root_dir):
|
return
|
||||||
if dirName == root_dir:
|
|
||||||
|
if filepath.endswith("mp3"):
|
||||||
|
m = mutagen.mp3.EasyMP3(filepath)
|
||||||
|
else:
|
||||||
|
m = mutagen.File(filepath)
|
||||||
|
self.title = m.get('title', [''])[0]
|
||||||
|
self.artist = m.get('artist', [''])[0]
|
||||||
|
self.ablum = m.get('album', [''])[0]
|
||||||
|
self.date = m.get('date', [''])[0]
|
||||||
|
self.length = str(int(m.info.length) // 60) + ":"
|
||||||
|
self.length += str(int(m.info.length) % 60)
|
||||||
|
self.filepath = filepath
|
||||||
|
|
||||||
|
def _from_dict(self, d):
|
||||||
|
self.title = d.get('title')
|
||||||
|
self.artist = d.get('artist')
|
||||||
|
self.ablum = d.get('album')
|
||||||
|
self.date = d.get('date')
|
||||||
|
self.length = d.get('length')
|
||||||
|
self.filepath = d.get('filepath')
|
||||||
|
|
||||||
|
|
||||||
|
def build_library(root_dir):
|
||||||
|
"""Walks the music directory and builds a library of tracks."""
|
||||||
|
print("Building library")
|
||||||
|
tracks = []
|
||||||
|
for dir_name, sub_dirs, files in os.walk(root_dir):
|
||||||
|
for file in files:
|
||||||
|
if not os.path.splitext(file)[1][1:] in MUSIC_EXT:
|
||||||
continue
|
continue
|
||||||
reg = re.search(".*/(.+?) - (.+) \(\d{4}\)", dirName)
|
filepath = os.path.join(root_dir, dir_name, file)
|
||||||
if not reg:
|
track = Track(filepath)
|
||||||
print(dirName)
|
tracks.append(track)
|
||||||
continue
|
return tracks
|
||||||
artist, album = reg.groups()
|
|
||||||
tracks = [f for f in files if f.rpartition('.')[2] in MUSIC_EXT]
|
def init_library():
|
||||||
tree[artist][album] = tracks
|
"""Loads the library from file, or builds a new one if one isn't found."""
|
||||||
return tree
|
if os.path.isfile("library.json"):
|
||||||
|
with open("library.json", "r") as file:
|
||||||
|
tracks = json.loads(file.read())
|
||||||
|
tracks = [Track(d=d) for d in tracks]
|
||||||
|
else:
|
||||||
|
tracks = build_library(MUSIC_DIR)
|
||||||
|
with open("library.json", "w") as file:
|
||||||
|
file.write(json.dumps([t.__dict__ for t in tracks]))
|
||||||
|
return tracks
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
tracks = init_library()
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
"""Main index page."""
|
"""Main index page."""
|
||||||
nav_items = os.listdir(MUSIC_DIR)
|
nav_items = list(set(t.artist for t in tracks))
|
||||||
nav_items.sort()
|
nav_items.sort()
|
||||||
nav_items = [item + '/' for item in nav_items]
|
nav_items = [item + '/' for item in nav_items]
|
||||||
cd = "/"
|
cd = "/"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user