1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009-2021 kaliko <kaliko@azylum.org>
4 # Copyright (c) 2009 J. Alexander Treuman (Tag collapse method)
5 # Copyright (c) 2008 Rick van Hattem
7 # This file is part of sima
9 # sima is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # sima is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with sima. If not, see <http://www.gnu.org/licenses/>.
26 from .meta import Artist, SEPARATOR
31 Instantiate with Player replies.
33 :param str file: media file, defaults to ``None``
34 :param int duration: duration in second, defaults to 0
35 :param int pos: position in queue, defaults to -1
36 :param str title|artist|album: defaults to ""
37 :param str musicbrainz_artistid|musicbrainz_albumartistid: MusicBrainz IDs, defaults to ``None``
40 def __init__(self, file=None, duration=0, pos=-1, **kwargs):
41 self.title = self.artist = self.album = self.albumartist = ''
42 self.musicbrainz_artistid = self.musicbrainz_albumartistid = None
46 self.duration = float(duration)
49 self.__dict__.update(**kwargs)
50 self.tags_to_collapse = ['artist', 'album', 'title', 'date',
51 'genre', 'albumartist',
52 'musicbrainz_artistid',
53 'musicbrainz_albumartistid']
54 # have tags been collapsed?
55 self.collapsed_tags = list()
56 # Needed for multiple tags which returns a list instead of a string
59 def _collapse_tags(self):
61 Necessary to deal with tags defined multiple times.
62 These entries are set as lists instead of strings.
64 for tag, value in self.__dict__.items():
65 if tag not in self.tags_to_collapse:
67 if isinstance(value, list):
68 self.collapsed_tags.append(tag)
69 self.__dict__.update({tag: SEPARATOR.join(value)})
72 return '%s(artist="%s", album="%s", title="%s", file="%s")' % (
73 self.__class__.__name__,
81 return '{artist} - {album} - {title} ({length})'.format(
87 return int(self.duration)
89 def __add__(self, other):
90 return Track(duration=self.duration + other.duration)
92 def __sub__(self, other):
93 return Track(duration=self.duration - other.duration)
97 return hash(self.file)
100 def __eq__(self, other):
101 return hash(self) == hash(other)
103 def __ne__(self, other):
104 return hash(self) != hash(other)
109 return not self._empty
113 """file is an immutable attribute that's used for the hash method"""
118 """Get a fancy duration as ``%H:%M:%S`` (use :attr:`duration` to get duration in second only)"""
119 temps = time.gmtime(self.duration) #TODO: returns a date not a duration
124 return time.strftime(fmt, temps)
128 """Fetches Genres for the track
129 Multivalue genre are dealt with:
130 * when genre tag is multivalued
131 * when single tag uses coma or semi-colon separator
133 if 'genre' not in self.__dict__:
135 genres = self.genre.split(SEPARATOR)
136 for sep in [',', ';']:
137 if sep in self.genre:
138 genres = [g for multi in genres for g in multi.split(sep) if g]
139 return list(map(str.strip, genres))
143 """Get the :class:`sima.lib.meta.Artist` associated to this track"""
145 if not self.musicbrainz_artistid:
146 return Artist(name='[unknown]',
147 mbid='125ec42a-7229-4250-afc5-e057484327fe')
148 return Artist(name='[unknown]', **self.__dict__)
149 return Artist(**self.__dict__)
152 # vim: ai ts=4 sw=4 sts=4 expandtab