From 54714a224b33b3a787d859ff39eb3a2c17a1cd6a Mon Sep 17 00:00:00 2001 From: mungai-njoroge Date: Tue, 26 Sep 2023 17:59:12 +0300 Subject: [PATCH] ignore unicode characters errors + add methods on settings.Keys to verify key !== empty string --- app/arg_handler.py | 3 +-- app/db/sqlite/tracks.py | 29 ++++++++++------------------- app/settings.py | 13 +++++++++++++ app/utils/unicode.py | 7 ++----- manage.py | 2 +- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/app/arg_handler.py b/app/arg_handler.py index 4795f6c..3eac229 100644 --- a/app/arg_handler.py +++ b/app/arg_handler.py @@ -72,7 +72,7 @@ class HandleArgs: ] ) finally: - # revert and remove the api keys for dev mode + # revert and remove the api keys for dev mode with open("./app/configs.py", "w", encoding="utf-8") as file: line1 = "LASTFM_API_KEY = ''\n" line2 = "POSTHOG_API_KEY = ''\n" @@ -173,4 +173,3 @@ class HandleArgs: if any((a in ARGS for a in ALLARGS.version)): print(settings.Release.APP_VERSION) sys.exit(0) - diff --git a/app/db/sqlite/tracks.py b/app/db/sqlite/tracks.py index fdf57f2..ab45f88 100644 --- a/app/db/sqlite/tracks.py +++ b/app/db/sqlite/tracks.py @@ -9,7 +9,7 @@ from sqlite3 import Cursor from app.db.sqlite.utils import tuple_to_track, tuples_to_tracks from .utils import SQLiteManager -from pprint import pprint +from app.utils.unicode import handle_unicode class SQLiteTrackMethods: @@ -45,25 +45,20 @@ class SQLiteTrackMethods: track = OrderedDict(sorted(track.items())) - # def should_fail(): - # """ - # Return true randomly. - # """ - # return random.randint(0, 1) == 1 - - # if should_fail(): - # raise Exception("Failed to insert track") - - # def force_surrogatepass(string: str): - # return string.encode("utf-16", "surrogatepass").decode("utf-16") - track["artist"] = track["artists"] track["albumartist"] = track["albumartists"] del track["artists"] del track["albumartists"] - cur.execute(sql, track) + try: + cur.execute(sql, track) + except UnicodeEncodeError: + # for each of the values in the track, call handle_unicode on it + for key, value in track.items(): + track[key] = handle_unicode(value) + + cur.execute(sql, track) @classmethod def insert_many_tracks(cls, tracks: list[dict]): @@ -71,13 +66,9 @@ class SQLiteTrackMethods: Inserts a list of tracks into the database. """ - with SQLiteManager() as cur: for track in tracks: - try: - cls.insert_one_track(track, cur) - except Exception: - pprint(track, indent=4) + cls.insert_one_track(track, cur) @staticmethod def get_all_tracks(): diff --git a/app/settings.py b/app/settings.py index 1da58a6..0ec4f55 100644 --- a/app/settings.py +++ b/app/settings.py @@ -6,6 +6,7 @@ import sys from typing import Any from app import configs +from app.logger import log join = os.path.join @@ -239,3 +240,15 @@ class Keys: if IS_BUILD: cls.LASTFM_API = configs.LASTFM_API_KEY cls.POSTHOG_API_KEY = configs.POSTHOG_API_KEY + + cls.verify_exists() + + @classmethod + def verify_exists(cls): + if not cls.LASTFM_API: + log.error("ERROR: LASTFM_API_KEY not set in environment") + sys.exit(0) + + if not cls.POSTHOG_API_KEY: + log.error("ERROR: POSTHOG_API_KEY not set in environment") + sys.exit(0) diff --git a/app/utils/unicode.py b/app/utils/unicode.py index 263e3e7..d859364 100644 --- a/app/utils/unicode.py +++ b/app/utils/unicode.py @@ -1,8 +1,5 @@ def handle_unicode(string: str): """ - Try resolving unicode characters, else escape them. + Handles Unicode errors by ignoring unicode characters """ - return string.encode("utf-16", "replace").decode("utf-16") - # try: - # except: - # return string.encode("unicode_escape").decode("utf-8") + return string.encode("utf-16", "ignore").decode("utf-16") \ No newline at end of file diff --git a/manage.py b/manage.py index 7c39c3d..35596bd 100644 --- a/manage.py +++ b/manage.py @@ -80,12 +80,12 @@ def start_watchdog(): def run_swingmusic(): + Keys.load() HandleArgs() log_startup_info() bg_run_setup() start_watchdog() telemetry.create_userid() - Keys.load() setproctitle.setproctitle( f"swingmusic - {FLASKVARS.FLASK_HOST}:{FLASKVARS.FLASK_PORT}"