audio: Update scanner to just return tags+duration
The caller should know what URI we are talking about. Additionally finding the mtime is never belonged in this class and has been moved out.
This commit is contained in:
parent
dcaa0f6732
commit
d9d501cd98
@ -1,6 +1,5 @@
|
||||
from __future__ import absolute_import, division, unicode_literals
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
import pygst
|
||||
@ -10,7 +9,7 @@ import gst # noqa
|
||||
from mopidy import exceptions
|
||||
from mopidy.audio import utils
|
||||
from mopidy.models import Album, Artist, Track
|
||||
from mopidy.utils import encoding, path
|
||||
from mopidy.utils import encoding
|
||||
|
||||
|
||||
class Scanner(object):
|
||||
@ -49,21 +48,21 @@ class Scanner(object):
|
||||
|
||||
:param uri: URI of the resource to scan.
|
||||
:type event: string
|
||||
:return: Dictionary of tags, duration, mtime and uri information.
|
||||
:return: (tags, duration) pair. tags is a dictionary of lists for all
|
||||
the tags we found and duration is the length of the URI in
|
||||
nanoseconds. No duration is indicated by -1 as in GStreamer.
|
||||
"""
|
||||
try:
|
||||
self._setup(uri)
|
||||
tags = self._collect() # Ensure collect before queries.
|
||||
data = {'uri': uri, 'tags': tags,
|
||||
'mtime': self._query_mtime(uri),
|
||||
'duration': self._query_duration()}
|
||||
duration = self._query_duration()
|
||||
finally:
|
||||
self._reset()
|
||||
|
||||
if self._min_duration_ms is None:
|
||||
return data
|
||||
elif data['duration'] >= self._min_duration_ms * gst.MSECOND:
|
||||
return data
|
||||
return tags, duration
|
||||
elif duration >= self._min_duration_ms * gst.MSECOND:
|
||||
return tags, duration
|
||||
|
||||
raise exceptions.ScannerError('Rejecting file with less than %dms '
|
||||
'audio data.' % self._min_duration_ms)
|
||||
@ -115,11 +114,6 @@ class Scanner(object):
|
||||
except gst.QueryError:
|
||||
return None
|
||||
|
||||
def _query_mtime(self, uri):
|
||||
if not uri.startswith('file:'):
|
||||
return None
|
||||
return os.path.getmtime(path.uri_to_path(uri))
|
||||
|
||||
|
||||
def _artists(tags, artist_name, artist_id=None):
|
||||
# Name missing, don't set artist
|
||||
|
||||
@ -127,9 +127,14 @@ class ScanCommand(commands.Command):
|
||||
try:
|
||||
relpath = translator.local_track_uri_to_path(uri, media_dir)
|
||||
file_uri = path.path_to_uri(os.path.join(media_dir, relpath))
|
||||
data = scanner.scan(file_uri)
|
||||
track = translator.add_musicbrainz_coverart_to_track(
|
||||
scan.audio_data_to_track(data).copy(uri=uri)).copy(uri=uri)
|
||||
tags, duration = scanner.scan(file_uri)
|
||||
# TODO: reuse mtime from above...
|
||||
mtime = os.path.getmtime(os.path.join(media_dir, relpath))
|
||||
track = scan.audio_data_to_track({'uri': uri,
|
||||
'tags': tags,
|
||||
'duration': duration,
|
||||
'mtime': mtime})
|
||||
track = translator.add_musicbrainz_coverart_to_track(track)
|
||||
library.add(track)
|
||||
logger.debug('Added %s', track.uri)
|
||||
except exceptions.ScannerError as error:
|
||||
|
||||
@ -44,8 +44,9 @@ class StreamLibraryProvider(backend.LibraryProvider):
|
||||
return [Track(uri=uri)]
|
||||
|
||||
try:
|
||||
data = self._scanner.scan(uri)
|
||||
track = scan.audio_data_to_track(data)
|
||||
tags, duration = self._scanner.scan(uri)
|
||||
track = scan.audio_data_to_track({
|
||||
'uri': uri, 'tags': tags, 'duration': duration, 'mtime': None})
|
||||
except exceptions.ScannerError as e:
|
||||
logger.warning('Problem looking up %s: %s', uri, e)
|
||||
track = Track(uri=uri)
|
||||
|
||||
@ -17,7 +17,7 @@ from tests import path_to_data_dir
|
||||
|
||||
# TODO: keep ids without name?
|
||||
class TranslatorTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
def setUp(self): # noqa
|
||||
self.data = {
|
||||
'uri': 'uri',
|
||||
'duration': 4531000000,
|
||||
@ -268,7 +268,7 @@ class TranslatorTest(unittest.TestCase):
|
||||
|
||||
|
||||
class ScannerTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
def setUp(self): # noqa
|
||||
self.errors = {}
|
||||
self.data = {}
|
||||
|
||||
@ -284,7 +284,9 @@ class ScannerTest(unittest.TestCase):
|
||||
uri = path_lib.path_to_uri(path)
|
||||
key = uri[len('file://'):]
|
||||
try:
|
||||
self.data[key] = scanner.scan(uri)
|
||||
tags, duration = scanner.scan(uri)
|
||||
self.data[key] = {
|
||||
'uri': uri, 'tags': tags, 'duration': duration}
|
||||
except exceptions.ScannerError as error:
|
||||
self.errors[key] = error
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user