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