Replace resolution tuple with dataclass
This commit is contained in:
@ -61,10 +61,6 @@ async def upload_file(file: UploadFile = File(...)) -> Dict[str, str]:
|
|||||||
async def remove_illegal_chars(filename: str) -> str:
|
async def remove_illegal_chars(filename: str) -> str:
|
||||||
"""Remove illegal characters from the filename.
|
"""Remove illegal characters from the filename.
|
||||||
|
|
||||||
Space is replaced with a dot.
|
|
||||||
"*, ", <, >, △, 「, 」, {, }, |, ^, ;, /, ?, :, @, &, =, +, $, ,," are removed.
|
|
||||||
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filename: The filename to remove illegal characters from.
|
filename: The filename to remove illegal characters from.
|
||||||
|
|
||||||
|
@ -1,11 +1,24 @@
|
|||||||
"""Stuff that has to do with videos."""
|
"""Stuff that has to do with videos."""
|
||||||
import ffmpeg
|
|
||||||
import sys
|
import sys
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import ffmpeg
|
||||||
|
|
||||||
from discord_embed import settings
|
from discord_embed import settings
|
||||||
|
|
||||||
|
|
||||||
def video_resolution(path_to_video: str) -> tuple[int, int]:
|
@dataclass
|
||||||
|
class Resolution:
|
||||||
|
"""Video resolution.
|
||||||
|
|
||||||
|
height: Height of video.
|
||||||
|
width: Width of video.
|
||||||
|
"""
|
||||||
|
height: int
|
||||||
|
width: int
|
||||||
|
|
||||||
|
|
||||||
|
def video_resolution(path_to_video: str) -> Resolution:
|
||||||
"""Find video resolution.
|
"""Find video resolution.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -23,7 +36,7 @@ def video_resolution(path_to_video: str) -> tuple[int, int]:
|
|||||||
width = int(video_stream["width"])
|
width = int(video_stream["width"])
|
||||||
height = int(video_stream["height"])
|
height = int(video_stream["height"])
|
||||||
|
|
||||||
return height, width
|
return Resolution(height, width)
|
||||||
|
|
||||||
|
|
||||||
def make_thumbnail(path_video: str, file_filename: str) -> str:
|
def make_thumbnail(path_video: str, file_filename: str) -> str:
|
||||||
|
@ -8,7 +8,7 @@ from fastapi import UploadFile
|
|||||||
|
|
||||||
from discord_embed import settings
|
from discord_embed import settings
|
||||||
from discord_embed.generate_html import generate_html_for_videos
|
from discord_embed.generate_html import generate_html_for_videos
|
||||||
from discord_embed.video import make_thumbnail, video_resolution
|
from discord_embed.video import Resolution, make_thumbnail, video_resolution
|
||||||
from discord_embed.webhook import send_webhook
|
from discord_embed.webhook import send_webhook
|
||||||
|
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ def save_to_disk(file: UploadFile) -> VideoFile:
|
|||||||
file: Our uploaded file.
|
file: Our uploaded file.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
VideoFile object with filename and location.
|
VideoFile object with the filename and location.
|
||||||
"""
|
"""
|
||||||
# Create the folder where we should save the files
|
# Create the folder where we should save the files
|
||||||
folder_video = os.path.join(settings.upload_folder, "video")
|
folder_video = os.path.join(settings.upload_folder, "video")
|
||||||
@ -62,12 +62,12 @@ async def do_things(file: UploadFile) -> Dict[str, str]:
|
|||||||
video_file: VideoFile = save_to_disk(file)
|
video_file: VideoFile = save_to_disk(file)
|
||||||
|
|
||||||
file_url = f"{settings.serve_domain}/video/{video_file.filename}"
|
file_url = f"{settings.serve_domain}/video/{video_file.filename}"
|
||||||
height, width = video_resolution(video_file.location)
|
res: Resolution = video_resolution(video_file.location)
|
||||||
screenshot_url = make_thumbnail(video_file.location, video_file.filename)
|
screenshot_url = make_thumbnail(video_file.location, video_file.filename)
|
||||||
html_url = generate_html_for_videos(
|
html_url = generate_html_for_videos(
|
||||||
url=file_url,
|
url=file_url,
|
||||||
width=width,
|
width=res.width,
|
||||||
height=height,
|
height=res.height,
|
||||||
screenshot=screenshot_url,
|
screenshot=screenshot_url,
|
||||||
filename=video_file.filename,
|
filename=video_file.filename,
|
||||||
)
|
)
|
||||||
|
@ -2,14 +2,14 @@ import imghdr
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from discord_embed import settings
|
from discord_embed import settings
|
||||||
from discord_embed.video import make_thumbnail, video_resolution
|
from discord_embed.video import Resolution, make_thumbnail, video_resolution
|
||||||
|
|
||||||
TEST_FILE = "tests/test.mp4"
|
TEST_FILE = "tests/test.mp4"
|
||||||
|
|
||||||
|
|
||||||
def test_video_resolution():
|
def test_video_resolution():
|
||||||
"""Test video_resolution() works."""
|
"""Test video_resolution() works."""
|
||||||
assert video_resolution(TEST_FILE) == (422, 422)
|
assert video_resolution(TEST_FILE) == Resolution(height=422, width=422)
|
||||||
|
|
||||||
|
|
||||||
def test_make_thumbnail():
|
def test_make_thumbnail():
|
||||||
|
Reference in New Issue
Block a user