11from __future__ import absolute_import , unicode_literals
22
3- import collections
4- import logging
5- import numbers
6-
73import gi
84gi .require_version ('Gst' , '1.0' )
95from gi .repository import Gst
106
11- from mopidy import compat , httpclient
12- from mopidy .models import Album , Artist , Track
13-
14- logger = logging .getLogger (__name__ )
15- TRACE = logging .getLevelName ('TRACE' )
7+ from mopidy import httpclient
168
179
1810def calculate_duration (num_samples , sample_rate ):
@@ -68,79 +60,6 @@ def supported_uri_schemes(uri_schemes):
6860 return supported_schemes
6961
7062
71- def _artists (tags , artist_name , artist_id = None , artist_sortname = None ):
72- # Name missing, don't set artist
73- if not tags .get (artist_name ):
74- return None
75- # One artist name and either id or sortname, include all available fields
76- if len (tags [artist_name ]) == 1 and \
77- (artist_id in tags or artist_sortname in tags ):
78- attrs = {'name' : tags [artist_name ][0 ]}
79- if artist_id in tags :
80- attrs ['musicbrainz_id' ] = tags [artist_id ][0 ]
81- if artist_sortname in tags :
82- attrs ['sortname' ] = tags [artist_sortname ][0 ]
83- return [Artist (** attrs )]
84-
85- # Multiple artist, provide artists with name only to avoid ambiguity.
86- return [Artist (name = name ) for name in tags [artist_name ]]
87-
88-
89- # TODO: split based on "stream" and "track" based conversion? i.e. handle data
90- # from radios in it's own helper instead?
91- def convert_tags_to_track (tags ):
92- """Convert our normalized tags to a track.
93-
94- :param tags: dictionary of tag keys with a list of values
95- :type tags: :class:`dict`
96- :rtype: :class:`mopidy.models.Track`
97- """
98- album_kwargs = {}
99- track_kwargs = {}
100-
101- track_kwargs ['composers' ] = _artists (tags , Gst .TAG_COMPOSER )
102- track_kwargs ['performers' ] = _artists (tags , Gst .TAG_PERFORMER )
103- track_kwargs ['artists' ] = _artists (tags , Gst .TAG_ARTIST ,
104- 'musicbrainz-artistid' ,
105- 'musicbrainz-sortname' )
106- album_kwargs ['artists' ] = _artists (
107- tags , Gst .TAG_ALBUM_ARTIST , 'musicbrainz-albumartistid' )
108-
109- track_kwargs ['genre' ] = '; ' .join (tags .get (Gst .TAG_GENRE , []))
110- track_kwargs ['name' ] = '; ' .join (tags .get (Gst .TAG_TITLE , []))
111- if not track_kwargs ['name' ]:
112- track_kwargs ['name' ] = '; ' .join (tags .get (Gst .TAG_ORGANIZATION , []))
113-
114- track_kwargs ['comment' ] = '; ' .join (tags .get ('comment' , []))
115- if not track_kwargs ['comment' ]:
116- track_kwargs ['comment' ] = '; ' .join (tags .get (Gst .TAG_LOCATION , []))
117- if not track_kwargs ['comment' ]:
118- track_kwargs ['comment' ] = '; ' .join (tags .get (Gst .TAG_COPYRIGHT , []))
119-
120- track_kwargs ['track_no' ] = tags .get (Gst .TAG_TRACK_NUMBER , [None ])[0 ]
121- track_kwargs ['disc_no' ] = tags .get (Gst .TAG_ALBUM_VOLUME_NUMBER , [None ])[0 ]
122- track_kwargs ['bitrate' ] = tags .get (Gst .TAG_BITRATE , [None ])[0 ]
123- track_kwargs ['musicbrainz_id' ] = tags .get ('musicbrainz-trackid' , [None ])[0 ]
124-
125- album_kwargs ['name' ] = tags .get (Gst .TAG_ALBUM , [None ])[0 ]
126- album_kwargs ['num_tracks' ] = tags .get (Gst .TAG_TRACK_COUNT , [None ])[0 ]
127- album_kwargs ['num_discs' ] = tags .get (Gst .TAG_ALBUM_VOLUME_COUNT , [None ])[0 ]
128- album_kwargs ['musicbrainz_id' ] = tags .get ('musicbrainz-albumid' , [None ])[0 ]
129-
130- if tags .get (Gst .TAG_DATE ) and tags .get (Gst .TAG_DATE )[0 ]:
131- track_kwargs ['date' ] = tags [Gst .TAG_DATE ][0 ].isoformat ()
132-
133- # Clear out any empty values we found
134- track_kwargs = {k : v for k , v in track_kwargs .items () if v }
135- album_kwargs = {k : v for k , v in album_kwargs .items () if v }
136-
137- # Only bother with album if we have a name to show.
138- if album_kwargs .get ('name' ):
139- track_kwargs ['album' ] = Album (** album_kwargs )
140-
141- return Track (** track_kwargs )
142-
143-
14463def setup_proxy (element , config ):
14564 """Configure a GStreamer element with proxy settings.
14665
@@ -157,46 +76,6 @@ def setup_proxy(element, config):
15776 element .set_property ('proxy-pw' , config .get ('password' ))
15877
15978
160- def convert_taglist (taglist ):
161- """Convert a :class:`Gst.TagList` to plain Python types.
162-
163- Knows how to convert:
164-
165- - Dates
166- - Buffers
167- - Numbers
168- - Strings
169- - Booleans
170-
171- Unknown types will be ignored and debug logged. Tag keys are all strings
172- defined as part GStreamer under GstTagList_.
173-
174- .. _GstTagList: https://developer.gnome.org/gstreamer/stable/\
175- gstreamer-GstTagList.html
176-
177- :param taglist: A GStreamer taglist to be converted.
178- :type taglist: :class:`Gst.TagList`
179- :rtype: dictionary of tag keys with a list of values.
180- """
181- result = collections .defaultdict (list )
182-
183- for n in range (taglist .n_tags ()):
184- tag = taglist .nth_tag_name (n )
185-
186- for i in range (taglist .get_tag_size (tag )):
187- value = taglist .get_value_index (tag , i )
188-
189- if isinstance (value , Gst .DateTime ):
190- result [tag ].append (value .to_iso8601_string ())
191- if isinstance (value , (compat .string_types , bool , numbers .Number )):
192- result [tag ].append (value )
193- else :
194- logger .log (
195- TRACE , 'Ignoring unknown tag data: %r = %r' , tag , value )
196-
197- return result
198-
199-
20079class Signals (object ):
20180
20281 """Helper for tracking gobject signal registrations"""
0 commit comments