refactor Track initialization
This commit is contained in:
parent
43efcfc0bf
commit
1d9037310e
17
musik.py
17
musik.py
|
@ -3,7 +3,6 @@
|
|||
Music streaming.
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
import random
|
||||
import subprocess
|
||||
|
@ -27,7 +26,8 @@ FFMPEG_CMD = [
|
|||
class Track:
|
||||
def __init__(self, filepath=None, d=None):
|
||||
if d:
|
||||
self._from_dict(d)
|
||||
for attr, value in d.items():
|
||||
setattr(self, attr, value)
|
||||
return
|
||||
|
||||
if filepath.endswith("mp3"):
|
||||
|
@ -36,20 +36,12 @@ class Track:
|
|||
m = mutagen.File(filepath)
|
||||
self.title = m.get('title', [''])[0]
|
||||
self.artist = m.get('artist', [''])[0]
|
||||
self.ablum = m.get('album', [''])[0]
|
||||
self.album = 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."""
|
||||
|
@ -64,6 +56,7 @@ def build_library(root_dir):
|
|||
tracks.append(track)
|
||||
return tracks
|
||||
|
||||
|
||||
def init_library():
|
||||
"""Loads the library from file, or builds a new one if one isn't found."""
|
||||
if os.path.isfile("library.json"):
|
||||
|
@ -73,7 +66,7 @@ def init_library():
|
|||
else:
|
||||
tracks = build_library(MUSIC_DIR)
|
||||
with open("library.json", "w") as file:
|
||||
file.write(json.dumps([t.__dict__ for t in tracks]))
|
||||
file.write(json.dumps([vars(t) for t in tracks]))
|
||||
return tracks
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user