diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2013-01-11 18:07:17 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2013-01-11 18:07:30 +0100 |
commit | 297514d6e6c42057ee769f19a3b9b4ed7127d503 (patch) | |
tree | 481b61b2627ed4809acb20f727e60d0407c0674e | |
parent | Add readme and license and robots. (diff) | |
download | zmusic-ng-297514d6e6c42057ee769f19a3b9b4ed7127d503.tar.xz zmusic-ng-297514d6e6c42057ee769f19a3b9b4ed7127d503.zip |
Work around unicode and bad mp3s.
Some MP3s can't be scanned, and picard throws an exception, so we check
for that.
We also coerse str strings into unicode strings using SQLAlchemy black
magic. It isn't pretty, but it's what the SQLAlchemy documentation
instructed us to do, alas.
Reported-by: James Pic <jamespic@gmail.com>
-rw-r--r-- | backend/zmusic/database.py | 37 | ||||
-rw-r--r-- | backend/zmusic/endpoints/scan.py | 5 |
2 files changed, 27 insertions, 15 deletions
diff --git a/backend/zmusic/database.py b/backend/zmusic/database.py index 3e61b74..6935a45 100644 --- a/backend/zmusic/database.py +++ b/backend/zmusic/database.py @@ -2,15 +2,24 @@ from zmusic import db from uuid import uuid4 import time +# This is indeed ugly, but it was suggested by the SQLAlchemy documention +# verbatim as a way of dealing with str-centric apps. +class CoerceUTF8(db.TypeDecorator): + impl = db.Unicode + def process_bind_param(self, value, dialect): + if isinstance(value, str): + value = value.decode('utf-8') + return value + class Song(db.Model): __tablename__ = 'songs' - filename = db.Column(db.String, primary_key=True) - id = db.Column(db.String, nullable=False, index=True) - title = db.Column(db.String) - album = db.Column(db.String) - artist = db.Column(db.String) - mimetype = db.Column(db.String) + filename = db.Column(CoerceUTF8, primary_key=True) + id = db.Column(CoerceUTF8, nullable=False, index=True) + title = db.Column(CoerceUTF8) + album = db.Column(CoerceUTF8) + artist = db.Column(CoerceUTF8) + mimetype = db.Column(CoerceUTF8) year = db.Column(db.Integer) track = db.Column(db.Integer) disc = db.Column(db.Integer) @@ -49,15 +58,15 @@ class Song(db.Model): class Download(db.Model): __tablename__ = 'downloads' - id = db.Column(db.String, primary_key=True) - leader_id = db.Column(db.String) + id = db.Column(CoerceUTF8, primary_key=True) + leader_id = db.Column(CoerceUTF8) time = db.Column(db.Integer) - ip = db.Column(db.String, index=True) - useragent = db.Column(db.String) - song_id = db.Column(db.String, db.ForeignKey(Song.id)) - artist = db.Column(db.String) - album = db.Column(db.String) - title = db.Column(db.String) + ip = db.Column(CoerceUTF8, index=True) + useragent = db.Column(CoerceUTF8) + song_id = db.Column(CoerceUTF8, db.ForeignKey(Song.id)) + artist = db.Column(CoerceUTF8) + album = db.Column(CoerceUTF8) + title = db.Column(CoerceUTF8) is_zip = db.Column(db.Boolean) def __init__(self, song, request): diff --git a/backend/zmusic/endpoints/scan.py b/backend/zmusic/endpoints/scan.py index 1eab692..46d59a7 100644 --- a/backend/zmusic/endpoints/scan.py +++ b/backend/zmusic/endpoints/scan.py @@ -26,7 +26,10 @@ def scan_music(): continue else: song = Song() - tags = readtags(name) + try: + tags = readtags(name) + except: + tags = None if tags == None: yield "%i | Skipping [%s].\n" % (int(time.time()), name) continue |