]> kaliko git repositories - mpd-sima.git/blob - sima/lib/track.py
503008b6b1a5a844a58b4538c404421dba394589
[mpd-sima.git] / sima / lib / track.py
1 # -*- coding: utf-8 -*-
2
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
6 #
7 #  This file is part of sima
8 #
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.
13 #
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.
18 #
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/>.
21 #
22 #
23
24 import time
25
26 from .meta import Artist, Album, SEPARATOR
27
28 class Track:
29     """
30     Track object.
31     Instantiate with Player replies.
32
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|albumartist: defaults to ""
37     :param str musicbrainz_{artistid|albumartistid|albumid|trackid}: MusicBrainz IDs, defaults to ``None``
38     """
39
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
43         self.musicbrainz_albumid = self.musicbrainz_trackid = None
44         self.pos = int(pos)
45         self._file = file
46         self._empty = False
47         self.duration = float(duration)
48         if not kwargs:
49             self._empty = True
50         self.__dict__.update(**kwargs)
51         self.tags_to_collapse = ['artist', 'album', 'title', 'date',
52                                  'genre', 'albumartist',
53                                  'musicbrainz_artistid',
54                                  'musicbrainz_albumartistid']
55         #  have tags been collapsed?
56         self.collapsed_tags = list()
57         # Needed for multiple tags which returns a list instead of a string
58         self._collapse_tags()
59
60     def _collapse_tags(self):
61         """
62         Necessary to deal with tags defined multiple times.
63         These entries are set as lists instead of strings.
64         """
65         for tag, value in self.__dict__.items():
66             if tag not in self.tags_to_collapse:
67                 continue
68             if isinstance(value, list):
69                 self.collapsed_tags.append(tag)
70                 self.__dict__.update({tag: SEPARATOR.join(value)})
71
72     def __repr__(self):
73         return '%s(artist="%s", album="%s", title="%s", file="%s")' % (
74             self.__class__.__name__,
75             self.artist,
76             self.album,
77             self.title,
78             self.file,
79         )
80
81     def __str__(self):
82         return '{artist} - {album} - {title} ({length})'.format(
83                 length=self.length,
84                 **self.__dict__
85                 )
86
87     def __int__(self):
88         return int(self.duration)
89
90     def __add__(self, other):
91         return Track(duration=self.duration + other.duration)
92
93     def __sub__(self, other):
94         return Track(duration=self.duration - other.duration)
95
96     def __hash__(self):
97         if self.file:
98             return hash(self.file)
99         return id(self)
100
101     def __eq__(self, other):
102         return hash(self) == hash(other)
103
104     def __ne__(self, other):
105         return hash(self) != hash(other)
106
107     def __bool__(self):
108         if not self._file:
109             return False
110         return not self._empty
111
112     @property
113     def file(self):
114         """file is an immutable attribute that's used for the hash method"""
115         return self._file
116
117     @property
118     def length(self):
119         """Get a fancy duration as ``%H:%M:%S`` (use :attr:`duration` to get duration in second only)"""
120         temps = time.gmtime(self.duration)  #TODO: returns a date not a duration
121         if temps.tm_hour:
122             fmt = '%H:%M:%S'
123         else:
124             fmt = '%M:%S'
125         return time.strftime(fmt, temps)
126
127     @property
128     def genres(self):
129         """Fetches Genres for the track
130         Multivalue genre are dealt with:
131           * when genre tag is multivalued
132           * when single tag uses coma or semi-colon separator
133         """
134         if 'genre' not in self.__dict__:
135             return []
136         genres = self.genre.split(SEPARATOR)
137         for sep in [',', ';']:
138             if sep in self.genre:
139                 genres = [g for multi in genres for g in multi.split(sep) if g]
140         return list(map(str.strip, genres))
141
142     @property
143     def Artist(self):
144         """Get the :class:`sima.lib.meta.Artist` associated to this track"""
145         if not self.artist:
146             if not self.musicbrainz_artistid:
147                 return Artist(name='[unknown]',
148                               mbid='125ec42a-7229-4250-afc5-e057484327fe')
149             return Artist(name='[unknown]', **self.__dict__)
150         return Artist(**self.__dict__)
151
152     @property
153     def Album(self):
154         """Get the :class:`sima.lib.meta.Album` associated to this track"""
155         if not self.album:
156             return Album(name='[unknown]', **self.__dict__)
157         return Album(name=self.album, **self.__dict__)
158
159 # VIM MODLINE
160 # vim: ai ts=4 sw=4 sts=4 expandtab