ignore unicode characters errors

+ add methods on settings.Keys to verify key !== empty string
This commit is contained in:
mungai-njoroge 2023-09-26 17:59:12 +03:00
parent 5cf0bb8c42
commit 54714a224b
5 changed files with 27 additions and 27 deletions

View File

@ -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)

View File

@ -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():

View File

@ -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)

View File

@ -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")

View File

@ -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}"