From 1d9037310e696d6b01fa99a4021e85bf84065aaf Mon Sep 17 00:00:00 2001 From: iou1name Date: Fri, 1 Feb 2019 12:59:19 -0500 Subject: [PATCH] refactor Track initialization --- musik.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/musik.py b/musik.py index 832055b..31fd2b1 100755 --- a/musik.py +++ b/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