From 100da530319d626e7539dbeaf27f617051da7a80 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= <tlovinator@gmail.com>
Date: Tue, 1 Mar 2022 04:22:42 +0100
Subject: [PATCH] Fix line length, use os.path.join and other small things

---
 discord_embed/main.py     | 91 +++++++++++++++++++++------------------
 discord_embed/settings.py | 15 +++----
 2 files changed, 56 insertions(+), 50 deletions(-)

diff --git a/discord_embed/main.py b/discord_embed/main.py
index bf8b1eb..ba4df40 100644
--- a/discord_embed/main.py
+++ b/discord_embed/main.py
@@ -1,3 +1,4 @@
+import os
 import sys
 from datetime import datetime
 from pathlib import Path
@@ -40,15 +41,16 @@ def normal_file_uploaded(file: UploadFile) -> Dict[str, str]:
         Dict[str, str]: Returns URL for file.
     """
     # Save file to disk.
-    with open(f"{Settings.upload_folder}/{file.filename}", "wb+") as file_object:
-        file_object.write(file.file.read())
+    folder_filename = os.path.join(Settings.upload_folder, file.filename)
+    with open(folder_filename, "wb+") as f:
+        f.write(file.file.read())
 
     hook.send(f"{Settings.domain}/{file.filename} was uploaded.")
     return {"html_url": f"{Settings.domain}/{file.filename}"}
 
 
 def video_file_uploaded(file: UploadFile) -> Dict[str, str]:
-    """Save video to disk, generate HTML, thumbnail, and return the .HTML file instead of the file.
+    """Save video to disk, generate HTML, thumbnail, and return a .html URL.
 
     Args:
         file (UploadFile): Our file object.
@@ -57,18 +59,23 @@ def video_file_uploaded(file: UploadFile) -> Dict[str, str]:
         Dict[str, str]: Returns URL for video.
     """
     # Create folder if it doesn't exist.
-    Path(f"{Settings.upload_folder}/video").mkdir(parents=True, exist_ok=True)
+    folder_video = os.path.join(Settings.upload_folder, "video")
+    Path(folder_video).mkdir(parents=True, exist_ok=True)
 
     # Save file to disk.
-    with open(f"{Settings.upload_folder}/video/{file.filename}", "wb+") as file_object:
-        file_object.write(file.file.read())
+    file_location = os.path.join(folder_video, file.filename)
+    with open(file_location, "wb+") as f:
+        f.write(file.file.read())
 
     file_url = f"{Settings.domain}/video/{file.filename}"
-    file_location = f"{Settings.upload_folder}/video/{file.filename}"
     height, width = find_video_resolution(file_location)
     screenshot_url = make_thumbnail_from_video(file_location, file.filename)
     html_url = generate_html_for_videos(
-        filename=file.filename, url=file_url, width=width, height=height, screenshot=screenshot_url
+        url=file_url,
+        width=width,
+        height=height,
+        screenshot=screenshot_url,
+        filename=file.filename,
     )
     hook.send(f"{Settings.domain}/{file.filename} was uploaded.")
     return {"html_url": f"{html_url}"}
@@ -84,28 +91,29 @@ def text_file_uploaded(file: UploadFile) -> Dict[str, str]:
         Dict[str, str]: Returns URL for file.
     """
     # Create folder if it doesn't exist.
-    Path(f"{Settings.upload_folder}/text").mkdir(parents=True, exist_ok=True)
+    text_location = os.path.join(Settings.upload_folder, "text")
+    Path(text_location).mkdir(parents=True, exist_ok=True)
 
-    save_location = f"{Settings.upload_folder}/text/{file.filename}"
+    save_location = os.path.join(text_location, file.filename)
     # Save file to disk.
-    with open(save_location, "wb+") as file_object:
-        file_object.write(file.file.read())
+    with open(save_location, "wb+", encoding="utf-8") as f:
+        f.write(file.file.read())
 
-    with open(save_location, encoding="utf-8") as file_object:
-        lines = file_object.read()
+    with open(save_location, encoding="utf-8") as f:
+        lines = f.read()
         colored_text = highlight(
             lines,
             guess_lexer(lines),
             HtmlFormatter(
                 style="fruity",  # Dark style
-                linenos="table",  # Output line numbers as a table w/ two cells, one with line numbers, other with code
+                linenos="table",  # Output line numbers
                 full=True,  # Use inline styles instead of CSS classes.
-                filename=f"{file.filename}",
+                filename=file.filename,
             ),
         )
-
-    with open(f"{Settings.upload_folder}/{file.filename}.html", "w", encoding="utf-8") as file_object:
-        file_object.write(colored_text)
+    html_color = os.path.join(Settings.upload_folder, f"{file.filename}.html")
+    with open(html_color, "w", encoding="utf-8") as f:
+        f.write(colored_text)
 
     html_url = f"{Settings.domain}/{file.filename}.html"
 
@@ -117,15 +125,16 @@ def text_file_uploaded(file: UploadFile) -> Dict[str, str]:
 async def upload_file(file: UploadFile = File(...)) -> Dict[str, str]:
     """Page for uploading files.
 
-    If it is a video, we need to make a HTML file, and a thumbnail otherwise we can just save
-     the file and return the URL for it.
+    If it is a video, we need to make a HTML file, and a thumbnail
+    otherwise we can just save the file and return the URL for it.
     If something goes wrong, we will send a message to Discord.
 
     Args:
         file (UploadFile): Our uploaded file.
 
     Returns:
-        Dict[str, str]: Returns a dict with the filename or a link to the .html if it was a video.
+        Dict[str, str]: Returns a dict with the filename or a link to
+        the .html if it was a video.
     """
     try:
         if file.content_type.startswith("video/"):
@@ -139,14 +148,14 @@ async def upload_file(file: UploadFile = File(...)) -> Dict[str, str]:
         ):
             return text_file_uploaded(file)
 
-        with open(f"{Settings.upload_folder}/{file.filename}", "wb+") as file_object:
-            file_object.write(file.file.read())
-
-        hook.send(f"{Settings.domain}/{file.filename} was uploaded.")
-        return {"html_url": f"{Settings.domain}/{file.filename}"}
+        with open(f"{Settings.upload_folder}/{file.filename}", "wb+") as f:
+            f.write(file.file.read())
+        domain_url = f"{Settings.domain}/{file.filename}"
+        hook.send(f"{domain_url} was uploaded.")
+        return {"html_url": domain_url}
     except Exception as e:
         # TODO: Change response code to 400.
-        hook.send(f"Something went wrong for {Settings.domain}/{file.filename}:\n{e}")
+        hook.send(f"Something went wrong for {domain_url}:\n{e}")
         return {"error": f"Something went wrong: {e}"}
 
 
@@ -178,7 +187,7 @@ async def main():
 """
 
 
-def generate_html_for_videos(url: str, width: int, height: int, screenshot: str, filename: str) -> str:
+def generate_html_for_videos(url: str, width: int, height: int, screenshot: str, filename: str) -> str:  # noqa: E501
     """Generate HTML for video files.
 
     This is what we will send to other people on Discord.
@@ -193,8 +202,8 @@ def generate_html_for_videos(url: str, width: int, height: int, screenshot: str,
         url (str): URL for the video. This is accessible from the browser.
         width (int): This is the width of the video.
         height (int): This is the height of the video.
-        screenshot (str): URL for screenshot. This is what you will see in Discord.
-        filename (str): Original video filename. We will append .html to the filename.
+        screenshot (str): URL for screenshot.
+        filename (str): Original video filename.
 
     Returns:
         str: [description]
@@ -214,13 +223,14 @@ def generate_html_for_videos(url: str, width: int, height: int, screenshot: str,
     </head>
     </html>
     """
-    html_url = f"{Settings.domain}/{filename}"
+    html_url = os.path.join(Settings.domain, filename)
 
     # Take the filename and append .html to it.
     filename += ".html"
 
-    with open(f"{Settings.upload_folder}/{filename}", "w", encoding="utf-8") as file:
-        file.write(video_html)
+    file_path = os.path.join(Settings.upload_folder, filename)
+    with open(file_path, "w", encoding="utf-8") as f:
+        f.write(video_html)
 
     return html_url
 
@@ -235,10 +245,7 @@ def find_video_resolution(path_to_video: str) -> tuple[int, int]:
         tuple[int, int]: Returns height and width.
     """
     probe = ffmpeg.probe(path_to_video)
-    video_stream = next(
-        (stream for stream in probe["streams"] if stream["codec_type"] == "video"),
-        None,
-    )
+    video_stream = next((stream for stream in probe["streams"] if stream["codec_type"] == "video"), None)  # noqa: E501
     if video_stream is None:
         print("No video stream found", file=sys.stderr)
         sys.exit(1)
@@ -260,10 +267,10 @@ def make_thumbnail_from_video(path_video: str, file_filename: str) -> str:
         str: Returns thumbnail filename.
     """
     (
-        ffmpeg.input(path_video, ss="1")  # Take a screenshot at 1 second.
-        .output(f"{Settings.upload_folder}/{file_filename}.jpg", vframes=1)  # Output to file.
-        .overwrite_output()  # Overwrite output.
-        .run()  # Run.
+        ffmpeg.input(path_video, ss="1")
+        .output(f"{Settings.upload_folder}/{file_filename}.jpg", vframes=1)
+        .overwrite_output()
+        .run()
     )
     # Return URL for thumbnail.
     return f"{Settings.domain}/{file_filename}.jpg"
diff --git a/discord_embed/settings.py b/discord_embed/settings.py
index dd8d7d9..de4867b 100644
--- a/discord_embed/settings.py
+++ b/discord_embed/settings.py
@@ -7,8 +7,9 @@ from dotenv import load_dotenv
 
 class Settings:
     description = (
-        "Discord will only create embeds for videos and images if they are smaller than 8 mb. We can 'abuse' this"
-        " by creating a .html that contains the 'twitter:player' HTML meta tag linking to the video."
+        "Discord will only create embeds for videos and images if they are "
+        "smaller than 8 mb. We can 'abuse' this by creating a .html that "
+        "contains the 'twitter:player' HTML meta tag linking to the video."
     )
     # Load environment variables
     load_dotenv()
@@ -19,7 +20,7 @@ class Settings:
     except KeyError:
         sys.exit("discord-embed: Environment variable 'DOMAIN' is missing!")
 
-    # We check if the domain ends with a forward slash. If it does, we remove it.
+    # Remove trailing slash from domain
     if domain.endswith("/"):
         domain = domain[:-1]
 
@@ -27,12 +28,12 @@ class Settings:
     try:
         upload_folder = os.environ["UPLOAD_FOLDER"]
     except KeyError:
-        sys.exit("discord-embed: Environment variable 'UPLOAD_FOLDER' is missing!")
+        sys.exit("Environment variable 'UPLOAD_FOLDER' is missing!")
 
     # Create upload_folder if it doesn't exist.
     pathlib.Path(upload_folder).mkdir(parents=True, exist_ok=True)
 
-    # We check if the upload folder ends with a forward slash. If it does, we remove it.
+    # Remove trailing slash from upload_folder
     if upload_folder.endswith("/"):
         upload_folder = upload_folder[:-1]
 
@@ -40,6 +41,4 @@ class Settings:
     try:
         webhook_url = os.environ["WEBHOOK_URL"]
     except KeyError:
-        sys.exit(
-            "Environment variable 'WEBHOOK_URL' is missing! You may have to restart your terminal or IDE to set it."
-        )
+        sys.exit("Environment variable 'WEBHOOK_URL' is missing!")