2020-10-12 16:22:00 -04:00
|
|
|
# Copyright 2020 (c) Anna Schumaker.
|
2020-10-20 11:10:04 -04:00
|
|
|
from lib import publisher
|
2020-10-12 16:22:00 -04:00
|
|
|
import datetime
|
2020-10-12 17:02:13 -04:00
|
|
|
import mutagen
|
2020-10-18 12:53:57 -04:00
|
|
|
import re
|
2020-10-12 16:22:00 -04:00
|
|
|
|
2020-10-20 11:10:04 -04:00
|
|
|
TrackUpdated = publisher.Publisher()
|
|
|
|
|
2020-10-12 16:22:00 -04:00
|
|
|
class Track:
|
2021-06-18 10:42:11 -04:00
|
|
|
def __init__(self, trackid, filepath, library):
|
2020-10-12 16:22:00 -04:00
|
|
|
self.trackid = trackid
|
2021-06-18 10:42:11 -04:00
|
|
|
self.path = filepath.relative_to(library.path)
|
|
|
|
self.library = library
|
2021-06-22 12:47:52 -04:00
|
|
|
self.lastplayed = None
|
|
|
|
self.playcount = None
|
2020-10-12 16:22:00 -04:00
|
|
|
|
2020-10-12 17:02:13 -04:00
|
|
|
f = mutagen.File(filepath)
|
2021-06-22 12:47:52 -04:00
|
|
|
self.album = f.get("album", ["Unknown Album"])[0]
|
|
|
|
self.artist = f.get("artist", ["Unknown Artist"])[0]
|
|
|
|
self.artist = f.get("albumartist", [self.artist])[0]
|
|
|
|
self.discnumber = int(f.get("discnumber", [1])[0])
|
|
|
|
self.genre = f.get("genre", [ "" ])[0]
|
|
|
|
self.genre = [ g.strip() for g in re.split(",|;|/|:", self.genre) ]
|
|
|
|
self.length = int(f.info.length)
|
|
|
|
self.title = f.get("title", [""])[0]
|
|
|
|
self.tracknumber = int(f.get("tracknumber", [0])[0])
|
|
|
|
self.year = f.get("date", ["0"])[0]
|
|
|
|
self.year = f.get("originalyear", [self.year])[0]
|
|
|
|
self.year = int(re.match("\d+", self.year).group(0))
|
2020-10-18 12:53:57 -04:00
|
|
|
|
|
|
|
def __getitem__(self, item):
|
2021-06-22 12:47:52 -04:00
|
|
|
tag = self.__dict__.get(item, None)
|
2020-10-18 12:53:57 -04:00
|
|
|
if item == "length":
|
|
|
|
(m, s) = divmod(tag, 60)
|
|
|
|
return f"{m}:{s:02}"
|
|
|
|
elif item in ("discnumber", "tracknumber"):
|
|
|
|
return f"{tag:02}"
|
|
|
|
elif item == "lastplayed":
|
|
|
|
return "Never" if tag == None else str(tag)
|
2020-10-18 13:26:40 -04:00
|
|
|
elif item == "genre":
|
2021-06-22 12:47:52 -04:00
|
|
|
return ", ".join(self.genre)
|
2020-10-18 12:53:57 -04:00
|
|
|
return None if tag == None else str(tag)
|
2020-10-12 17:02:13 -04:00
|
|
|
|
2020-10-18 13:38:27 -04:00
|
|
|
def decade(self):
|
2021-06-22 12:47:52 -04:00
|
|
|
return (self.year // 10) * 10
|
2020-10-18 13:26:40 -04:00
|
|
|
|
2021-06-18 10:42:11 -04:00
|
|
|
def filepath(self):
|
|
|
|
return self.library.path / self.path
|
|
|
|
|
2020-10-12 16:22:00 -04:00
|
|
|
def played(self):
|
2021-06-22 12:47:52 -04:00
|
|
|
self.playcount += 1
|
|
|
|
self.lastplayed = datetime.date.today()
|
2020-10-20 11:10:04 -04:00
|
|
|
TrackUpdated.publish(self)
|
|
|
|
|
|
|
|
def reset():
|
|
|
|
TrackUpdated.reset()
|