From 8bd380f2ff3f2a93d38af097524ca9a467c664e0 Mon Sep 17 00:00:00 2001 From: hyanx Date: Sun, 12 Nov 2017 17:28:24 +0800 Subject: [PATCH] Update character encoding error --- ncm/downloader.py | 15 ++++++++++++--- ncm/file_util.py | 6 +++++- ncm/start.py | 5 ++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/ncm/downloader.py b/ncm/downloader.py index 2e58ce0..95c801b 100644 --- a/ncm/downloader.py +++ b/ncm/downloader.py @@ -52,7 +52,10 @@ def download_song_by_song(song, download_folder, sub_folder=True): return is_already_download = download_file(song_url, song_file_name, song_download_folder) if is_already_download: - print('Mp3 file already download:', song_file_name) + try: + print('Mp3 file already download:', song_file_name) + except: + print('Mp3 file already download:', song_file_name.encode("GBK", 'ignore')) return # download cover @@ -114,11 +117,17 @@ def refresh(self, count): # Update progress if down size > 10k if (self.count - self.prev_count) > 10240: self.prev_count = self.count - print(self.__get_info(), end=self.end_str) + try: + print(self.__get_info(), end=self.end_str) + except: + print(self.__get_info().encode("GBK", 'ignore'), end=self.end_str) # Finish downloading if self.count >= self.total: self.end_str = '\n' - print(self.__get_info(), end=self.end_str) + try: + print(self.__get_info(), end=self.end_str) + except: + print(self.__get_info().encode("GBK", 'ignore'), end=self.end_str) def format_string(string): diff --git a/ncm/file_util.py b/ncm/file_util.py index 4d647b1..8c430ac 100644 --- a/ncm/file_util.py +++ b/ncm/file_util.py @@ -14,7 +14,11 @@ def resize_img(file_path, max_size=(640, 640), quality=90): if img.size[0] > max_size[0] or img.size[1] > max_size[1]: img.thumbnail(max_size, Image.ANTIALIAS) - img.save(file_path, quality=quality) + try: + img.save(file_path, quality=quality) + except: + img = img.convert('RGB') + img.save(file_path, quality=quality) def add_metadata_to_song(file_path, cover_path, song): diff --git a/ncm/start.py b/ncm/start.py index 42c707d..3c44ef0 100644 --- a/ncm/start.py +++ b/ncm/start.py @@ -38,7 +38,10 @@ def download_playlist_songs(playlist_id): folder_name = format_string(playlist_name) + ' - playlist' folder_path = os.path.join(config.DOWNLOAD_DIR, folder_name) for i, song in enumerate(songs): - print('{}: {}'.format(i + 1, song['name'])) + try: + print('{}: {}'.format(i + 1, song['name'])) + except: + print('{}: {}'.format(i + 1, song['name'].encode("GBK", 'ignore'))) download_song_by_song(song, folder_path, False)