diff --git a/supysonic/scanner.py b/supysonic/scanner.py index 6da6d22..a4c0022 100644 --- a/supysonic/scanner.py +++ b/supysonic/scanner.py @@ -142,7 +142,7 @@ class Scanner: trdict['duration'] = int(tag.info.length) trdict['has_art'] = bool(Track._extract_cover_art(path)) - trdict['bitrate'] = (tag.info.bitrate if hasattr(tag.info, 'bitrate') else int(os.path.getsize(path) * 8 / tag.info.length)) // 1000 + trdict['bitrate'] = int(tag.info.bitrate if hasattr(tag.info, 'bitrate') else os.path.getsize(path) * 8 / tag.info.length) // 1000 trdict['content_type'] = mimetypes.guess_type(path, False)[0] or 'application/octet-stream' trdict['last_modification'] = int(os.path.getmtime(path)) @@ -309,7 +309,10 @@ class Scanner: if transform: value = transform(value) return value if value else default - except (KeyError, ValueError): + # KeyError: missing tag + # IndexError: tag is present but doesn't have any value + # ValueError: tag can't be transformed to correct type + except (KeyError, IndexError, ValueError): return default def stats(self): diff --git a/tests/__init__.py b/tests/__init__.py index d6897a2..6df5548 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -18,6 +18,7 @@ from . import frontend from .issue101 import Issue101TestCase from .issue129 import Issue129TestCase from .issue133 import Issue133TestCase +from .issue139 import Issue139TestCase def suite(): suite = unittest.TestSuite() @@ -29,6 +30,7 @@ def suite(): suite.addTest(unittest.makeSuite(Issue101TestCase)) suite.addTest(unittest.makeSuite(Issue129TestCase)) suite.addTest(unittest.makeSuite(Issue133TestCase)) + suite.addTest(unittest.makeSuite(Issue139TestCase)) return suite diff --git a/tests/assets/issue139.aac b/tests/assets/issue139.aac new file mode 100644 index 0000000..b16037a Binary files /dev/null and b/tests/assets/issue139.aac differ diff --git a/tests/assets/issue139.mp3 b/tests/assets/issue139.mp3 new file mode 100644 index 0000000..71f5f72 Binary files /dev/null and b/tests/assets/issue139.mp3 differ diff --git a/tests/issue139.py b/tests/issue139.py new file mode 100644 index 0000000..55a93c0 --- /dev/null +++ b/tests/issue139.py @@ -0,0 +1,49 @@ +# coding: utf-8 +# +# This file is part of Supysonic. +# Supysonic is a Python implementation of the Subsonic server API. +# +# Copyright (C) 2019 Alban 'spl0k' FĂ©ron +# +# Distributed under terms of the GNU AGPLv3 license. + +import shutil +import tempfile +import unittest + +from pony.orm import db_session + +from supysonic.db import init_database, release_database +from supysonic.db import Folder, Track +from supysonic.managers.folder import FolderManager +from supysonic.scanner import Scanner + +class Issue139TestCase(unittest.TestCase): + def setUp(self): + self.__dir = tempfile.mkdtemp() + init_database('sqlite:') + with db_session: + FolderManager.add('folder', self.__dir) + + def tearDown(self): + release_database() + shutil.rmtree(self.__dir) + + @db_session + def do_scan(self): + scanner = Scanner() + folder = Folder.select(lambda f: f.root).first() + scanner.scan(folder) + scanner.finish() + del scanner + + def test_null_genre(self): + shutil.copy('tests/assets/issue139.mp3', self.__dir) + self.do_scan() + + def test_float_bitrate(self): + shutil.copy('tests/assets/issue139.aac', self.__dir) + self.do_scan() + +if __name__ == '__main__': + unittest.main()