From 87d10206fbca6a4c4c0f7f622ae34decab0bbfdb Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Mon, 27 Jun 2022 16:24:12 +0100 Subject: [PATCH] Add UTF-8 encoding to all open statements --- setup.py | 4 ++-- utils/checker.py | 8 ++++---- utils/subreddit.py | 4 +++- utils/videos.py | 4 +++- video_creation/final_video.py | 2 +- video_creation/screenshot_downloader.py | 8 ++++++-- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/setup.py b/setup.py index 6063e5a..e3cae6d 100755 --- a/setup.py +++ b/setup.py @@ -142,7 +142,7 @@ theme = handle_input( loader = Loader("Attempting to save your credentials...", "Done!").start() # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... console.print("Writing to the .env file...") -with open(".env", "w") as f: +with open(".env", "w", encoding="utf-8") as f: f.write( f"""REDDIT_CLIENT_ID="{client_id}" REDDIT_CLIENT_SECRET="{client_sec}" @@ -155,7 +155,7 @@ OPACITY={opacity} """ ) -with open(".setup-done-before", "w") as f: +with open(".setup-done-before", "w", encoding="utf-8") as f: f.write( "This file blocks the setup assistant from running again. Delete this file to run setup again." ) diff --git a/utils/checker.py b/utils/checker.py index 9388029..c56e063 100755 --- a/utils/checker.py +++ b/utils/checker.py @@ -21,10 +21,10 @@ def check_env() -> bool: return True if not os.path.exists(".env"): console.print("[red]Couldn't find the .env file, creating one now.") - with open(".env", "x") as file: + with open(".env", "x", encoding="utf-8") as file: file.write("") success = True - with open(".env.template", "r") as template: + with open(".env.template", "r", encoding="utf-8") as template: # req_envs = [env.split("=")[0] for env in template.readlines() if "=" in env] matching = {} explanations = {} @@ -172,7 +172,7 @@ def check_env() -> bool: console.print("[red]Aborting: Unresolved missing variables") return False if len(incorrect): - with open(".env", "r+") as env_file: + with open(".env", "r+", encoding="utf-8") as env_file: lines = [] for line in env_file.readlines(): line.split("=")[0].strip() not in incorrect and lines.append(line) @@ -180,7 +180,7 @@ def check_env() -> bool: env_file.write("\n".join(lines)) env_file.truncate() console.print("[green]Successfully removed incorrectly set variables from .env") - with open(".env", "a") as env_file: + with open(".env", "a", encoding="utf-8") as env_file: for env in missing: env_file.write( env diff --git a/utils/subreddit.py b/utils/subreddit.py index a40b6c3..94de416 100644 --- a/utils/subreddit.py +++ b/utils/subreddit.py @@ -17,7 +17,9 @@ def get_subreddit_undone(submissions: list, subreddit): """ recursively checks if the top submission in the list was already done. """ - with open("./video_creation/data/videos.json", "r") as done_vids_raw: + with open( + "./video_creation/data/videos.json", "r", encoding="utf-8" + ) as done_vids_raw: done_videos = json.load(done_vids_raw) for submission in submissions: if already_done(done_videos, submission): diff --git a/utils/videos.py b/utils/videos.py index 8d6021f..b41dfa7 100755 --- a/utils/videos.py +++ b/utils/videos.py @@ -18,7 +18,9 @@ def check_done( dict[str]|None: Reddit object in args """ - with open("./video_creation/data/videos.json", "r") as done_vids_raw: + with open( + "./video_creation/data/videos.json", "r", encoding="utf-8" + ) as done_vids_raw: done_videos = json.load(done_vids_raw) for video in done_videos: if video["id"] == str(redditobj): diff --git a/video_creation/final_video.py b/video_creation/final_video.py index c05006c..e0caf10 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -154,7 +154,7 @@ def save_data(filename: str): Args: filename (str): The finished video title name """ - with open("./video_creation/data/videos.json", "r+") as raw_vids: + with open("./video_creation/data/videos.json", "r+", encoding="utf-8") as raw_vids: done_vids = json.load(raw_vids) if str(subreddit.submission.id) in [video["id"] for video in done_vids]: return # video already done but was specified to continue anyway in the .env file diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index f638abc..83dd4c6 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -38,9 +38,13 @@ def download_screenshots_of_reddit_posts(reddit_object: dict[str], screenshot_nu context = browser.new_context() if getenv("THEME").upper() == "DARK": - cookie_file = open("./video_creation/data/cookie-dark-mode.json") + cookie_file = open( + "./video_creation/data/cookie-dark-mode.json", encoding="utf-8" + ) else: - cookie_file = open("./video_creation/data/cookie-light-mode.json") + cookie_file = open( + "./video_creation/data/cookie-light-mode.json", encoding="utf-8" + ) cookies = json.load(cookie_file) context.add_cookies(cookies) # load preference cookies # Get the thread screenshot