refactor: provide a proper TUI interface with textual
This commit is contained in:
@@ -67,7 +67,6 @@ def collect_files(
|
||||
collected_files.append(file_path)
|
||||
return collected_files
|
||||
|
||||
|
||||
def upload_file(file_path: pathlib.Path) -> None:
|
||||
"""
|
||||
Upload the specified file to a Discord webhook.
|
||||
@@ -75,14 +74,13 @@ def upload_file(file_path: pathlib.Path) -> None:
|
||||
:param file_path: The path of the file to be uploaded.
|
||||
"""
|
||||
if WEBHOOK_URL is None:
|
||||
print("Error: RANDOM_FILE_PICKER_WEBHOOK_URL environment variable is not set.")
|
||||
return
|
||||
raise ValueError("RANDOM_FILE_PICKER_WEBHOOK_URL environment variable is not set.")
|
||||
|
||||
filename = file_path.name
|
||||
with open(file_path, "rb") as image_file:
|
||||
with file_path.open("rb") as image_file:
|
||||
image_data = image_file.read()
|
||||
files = {"file": (filename, io.BytesIO(image_data))}
|
||||
response = requests.post(WEBHOOK_URL, files=files, data={})
|
||||
response = requests.post(WEBHOOK_URL, files=files, data={}, timeout=60*5)
|
||||
response.raise_for_status() # Raise HTTPError for bad responses
|
||||
|
||||
def move_file_to_meme_folder(file_path: pathlib.Path) -> None:
|
||||
@@ -92,19 +90,16 @@ def move_file_to_meme_folder(file_path: pathlib.Path) -> None:
|
||||
:param file_path: The path of the file to be moved.
|
||||
"""
|
||||
if MEME_FOLDER is None:
|
||||
print("Error: RANDOM_FILE_PICKER_MEME_FOLDER environment variable is not set.")
|
||||
return
|
||||
raise ValueError("RANDOM_FILE_PICKER_MEME_FOLDER environment variable is not set.")
|
||||
meme_dir_path = pathlib.Path(MEME_FOLDER)
|
||||
|
||||
# Make sure the meme folder exists
|
||||
if not meme_dir_path.exists():
|
||||
print(f"Error: Meme folder '{meme_dir_path}' does not exist.")
|
||||
return
|
||||
raise RuntimeError(f"Meme folder '{meme_dir_path}' does not exist.")
|
||||
|
||||
# Make sure the file being moved is not already in the meme folder
|
||||
if file_path.parent.resolve() == meme_dir_path.resolve():
|
||||
print("File is already in the meme folder.")
|
||||
return
|
||||
raise RuntimeError("File is already in the meme folder.")
|
||||
|
||||
# Formulate the destination path and avoid overwriting existing files
|
||||
base_name = file_path.name
|
||||
@@ -115,9 +110,4 @@ def move_file_to_meme_folder(file_path: pathlib.Path) -> None:
|
||||
while destination_path.exists():
|
||||
destination_path = meme_dir_path / f"{name}_{counter}{ext}"
|
||||
counter += 1
|
||||
|
||||
ok = file_path.rename(destination_path)
|
||||
if not ok:
|
||||
print(f"Error moving {file_path} to {destination_path}")
|
||||
else:
|
||||
print(f"Moved {file_path} to {destination_path}")
|
||||
_ = file_path.rename(destination_path)
|
||||
|
||||
Reference in New Issue
Block a user