2020-10-12 16:22:00 -04:00
|
|
|
# Copyright 2020 (c) Anna Schumaker.
|
2021-07-06 16:21:37 -04:00
|
|
|
from lib import metadata
|
2020-10-20 11:10:04 -04:00
|
|
|
from lib import publisher
|
2021-06-23 16:48:10 -04:00
|
|
|
from . import tags
|
2021-06-29 13:59:50 -04:00
|
|
|
from gi.repository import GObject
|
2020-10-12 16:22:00 -04:00
|
|
|
import datetime
|
|
|
|
|
2021-06-29 13:59:50 -04:00
|
|
|
class Track(GObject.Object):
|
2021-06-18 10:42:11 -04:00
|
|
|
def __init__(self, trackid, filepath, library):
|
2021-06-29 13:59:50 -04:00
|
|
|
GObject.Object.__init__(self)
|
2020-10-12 16:22:00 -04:00
|
|
|
self.trackid = trackid
|
2021-06-24 13:33:47 -04:00
|
|
|
self.path = filepath.relative_to(library.name)
|
2021-06-18 10:42:11 -04:00
|
|
|
self.library = library
|
2021-06-22 12:47:52 -04:00
|
|
|
self.lastplayed = None
|
2021-06-25 12:12:04 -04:00
|
|
|
self.playcount = 0
|
2020-10-12 16:22:00 -04:00
|
|
|
|
2021-07-06 16:21:37 -04:00
|
|
|
with metadata.Metadata(filepath) as meta:
|
|
|
|
self.title = meta.title()
|
|
|
|
self.length = meta.length()
|
|
|
|
self.discnumber = meta.discnumber()
|
|
|
|
self.tracknumber = meta.tracknumber()
|
|
|
|
|
2021-07-23 11:35:48 -04:00
|
|
|
self.artist = tags.Artist.add(meta.artist(), self, sort=meta.artistsort())
|
2021-06-23 17:14:14 -04:00
|
|
|
self.album = tags.Album.add(self.artist, meta.album(), self)
|
2021-06-21 14:42:32 -04:00
|
|
|
self.genres = [ tags.Genre.add(g, self) for g in meta.genres() ]
|
2021-06-24 10:35:04 -04:00
|
|
|
self.decade = tags.Decade.add(f"{meta.decade()}s", self)
|
2021-06-24 11:28:10 -04:00
|
|
|
self.year = tags.Year.add(self.decade, str(meta.year()), self)
|
2020-10-18 12:53:57 -04:00
|
|
|
|
2021-07-07 10:39:09 -04:00
|
|
|
self.playlists = [ tags.User.add("Collection", self),
|
|
|
|
tags.User.add("New Tracks", self) ]
|
|
|
|
|
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}"
|
2021-07-23 11:40:25 -04:00
|
|
|
elif item == "discnumber":
|
2020-10-18 12:53:57 -04:00
|
|
|
return f"{tag:02}"
|
2021-07-23 11:40:25 -04:00
|
|
|
elif item == "tracknumber":
|
|
|
|
return f"{self.discnumber}-{tag:02}"
|
2020-10-18 12:53:57 -04:00
|
|
|
elif item == "lastplayed":
|
|
|
|
return "Never" if tag == None else str(tag)
|
2021-06-21 14:42:32 -04:00
|
|
|
elif item == "genres":
|
|
|
|
return ", ".join([ str(g) for g in self.genres ])
|
2020-10-18 12:53:57 -04:00
|
|
|
return None if tag == None else str(tag)
|
2020-10-12 17:02:13 -04:00
|
|
|
|
2021-06-23 16:48:10 -04:00
|
|
|
def __getstate__(self):
|
|
|
|
state = self.__dict__.copy()
|
|
|
|
state["artist"] = str(self.artist)
|
2021-06-23 17:14:14 -04:00
|
|
|
state["album"] = str(self.album)
|
2021-06-21 14:42:32 -04:00
|
|
|
state["genres"] = [ str(g) for g in self.genres ]
|
2021-06-24 10:35:04 -04:00
|
|
|
state["decade"] = str(self.decade)
|
2021-06-24 11:28:10 -04:00
|
|
|
state["year"] = str(self.year)
|
2021-07-07 10:39:09 -04:00
|
|
|
state["playlists" ] = [ str(p) for p in self.playlists \
|
|
|
|
if str(p) not in ("New Tracks", "Previous") ]
|
2021-06-23 16:48:10 -04:00
|
|
|
return state
|
|
|
|
|
|
|
|
def __setstate__(self, state):
|
2021-06-29 13:59:50 -04:00
|
|
|
GObject.Object.__init__(self)
|
2021-06-23 16:48:10 -04:00
|
|
|
self.__dict__.update(state)
|
2021-06-29 13:53:37 -04:00
|
|
|
|
|
|
|
def __set_tags__(self):
|
2021-06-23 16:48:10 -04:00
|
|
|
self.artist = tags.Artist.init_track(self.artist, self)
|
2021-06-23 17:14:14 -04:00
|
|
|
self.album = tags.Album.init_track(self.artist, self.album, self)
|
2021-06-21 14:42:32 -04:00
|
|
|
self.genres = [ tags.Genre.init_track(g, self) for g in self.genres ]
|
2021-06-24 10:35:04 -04:00
|
|
|
self.decade = tags.Decade.init_track(self.decade, self)
|
2021-06-24 11:28:10 -04:00
|
|
|
self.year = tags.Year.init_track(self.decade, self.year, self)
|
2021-07-07 10:39:09 -04:00
|
|
|
self.playlists = [ tags.User.init_track(p, self) for p in self.playlists ]
|
2020-10-18 13:26:40 -04:00
|
|
|
|
2021-06-24 17:08:27 -04:00
|
|
|
def about_to_remove(self):
|
|
|
|
tags.Artist.remove(self.artist, self)
|
|
|
|
tags.Album.remove(self.album, self)
|
|
|
|
for genre in self.genres:
|
|
|
|
tags.Genre.remove(genre, self)
|
|
|
|
tags.Decade.remove(self.decade, self)
|
|
|
|
tags.Year.remove(self.year, self)
|
2021-07-07 10:39:09 -04:00
|
|
|
for tag in self.playlists:
|
|
|
|
tag.remove_track(self)
|
|
|
|
|
|
|
|
def add_to_playlist(self, name):
|
|
|
|
self.playlists.append(tags.User.add(name, self))
|
2021-06-24 17:08:27 -04:00
|
|
|
|
2021-06-18 10:42:11 -04:00
|
|
|
def filepath(self):
|
2021-06-24 13:33:47 -04:00
|
|
|
return self.library.name / self.path
|
2021-06-18 10:42:11 -04:00
|
|
|
|
2020-10-12 16:22:00 -04:00
|
|
|
def played(self):
|
2021-06-22 12:47:52 -04:00
|
|
|
self.playcount += 1
|
2021-10-01 11:16:02 -04:00
|
|
|
self.lastplayed = datetime.datetime.now()
|
2021-07-07 10:39:09 -04:00
|
|
|
|
|
|
|
def remove_from_playlist(self, name):
|
|
|
|
tag = tags.User[name]
|
|
|
|
tag.remove_track(self)
|
|
|
|
self.playlists.remove(tag)
|