From bfe0003551e8c745e425668ec8c5c15f0245e07a Mon Sep 17 00:00:00 2001 From: Tyrone-Zhao Date: Sat, 22 Sep 2018 15:32:11 +0800 Subject: [PATCH] change to print song_name and file_sha1 with 'utf8' encoding correctly --- dejavu.py | 3 ++- dejavu/__init__.py | 4 ++-- dejavu/database_sql.py | 4 ++-- dejavu/recognize.py | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/dejavu.py b/dejavu.py index 0bb4d0c..a7f74a1 100755 --- a/dejavu.py +++ b/dejavu.py @@ -88,6 +88,7 @@ if __name__ == '__main__': song = djv.recognize(MicrophoneRecognizer, seconds=opt_arg) elif source == 'file': song = djv.recognize(FileRecognizer, opt_arg) - print(song) + decoded_song = repr(song).decode('string_escape') + print(decoded_song) sys.exit(0) diff --git a/dejavu/__init__.py b/dejavu/__init__.py index 4f6e6e8..7cc3f3b 100755 --- a/dejavu/__init__.py +++ b/dejavu/__init__.py @@ -155,11 +155,11 @@ class Dejavu(object): fingerprint.DEFAULT_OVERLAP_RATIO, 5) song = { Dejavu.SONG_ID : song_id, - Dejavu.SONG_NAME : songname, + Dejavu.SONG_NAME : songname.encode("utf8"), Dejavu.CONFIDENCE : largest_count, Dejavu.OFFSET : int(largest), Dejavu.OFFSET_SECS : nseconds, - Database.FIELD_FILE_SHA1 : song.get(Database.FIELD_FILE_SHA1, None),} + Database.FIELD_FILE_SHA1 : song.get(Database.FIELD_FILE_SHA1, None).encode("utf8"),} return song def recognize(self, recognizer, *options, **kwoptions): diff --git a/dejavu/database_sql.py b/dejavu/database_sql.py index 18bb238..e827260 100755 --- a/dejavu/database_sql.py +++ b/dejavu/database_sql.py @@ -214,7 +214,7 @@ class SQLDatabase(Database): """ Return songs that have the fingerprinted flag set TRUE (1). """ - with self.cursor(cursor_type=DictCursor) as cur: + with self.cursor(cursor_type=DictCursor, charset="utf8") as cur: cur.execute(self.SELECT_SONGS) for row in cur: yield row @@ -223,7 +223,7 @@ class SQLDatabase(Database): """ Returns song by its ID. """ - with self.cursor(cursor_type=DictCursor) as cur: + with self.cursor(cursor_type=DictCursor, charset="utf8") as cur: cur.execute(self.SELECT_SONG, (sid,)) return cur.fetchone() diff --git a/dejavu/recognize.py b/dejavu/recognize.py index b43a879..09c03d9 100755 --- a/dejavu/recognize.py +++ b/dejavu/recognize.py @@ -1,3 +1,4 @@ +# encoding: utf-8 import dejavu.fingerprint as fingerprint import dejavu.decoder as decoder import numpy as np