Skip to content
Closed

Patch #2335

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 36 additions & 49 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
#!/usr/bin/env python
import math
import sys
from os import name
from pathlib import Path
import typing
import os
from subprocess import Popen
from typing import Dict, NoReturn
from typing import Dict, NoReturn, Optional

from prawcore import ResponseException

from reddit.subreddit import get_subreddit_threads
from utils import settings
from utils.cleanup import cleanup
from utils.console import print_markdown, print_step, print_substep
from utils.console import print_markdown, print_step, print_substep, format_ordinal
from utils.ffmpeg_install import ffmpeg_install
from utils.id import extract_id
from utils.version import checkversion
from video_creation.background import (
from utils.settings import get_config
from utils.version import check_python, checkversion
from video_creation import (
chop_background,
download_background_audio,
download_background_video,
get_background_config,
get_screenshots_of_reddit_posts,
make_final_video,
)
from video_creation.final_video import make_final_video
from video_creation.screenshot_downloader import get_screenshots_of_reddit_posts
from video_creation.voices import save_text_to_mp3

__VERSION__ = "3.4.0"
__VERSION__ = "4.0.0"

print(
"""
Expand All @@ -38,15 +38,17 @@
"""
)
print_markdown(
"### Thanks for using this tool! Feel free to contribute to this project on GitHub! If you have any questions, feel free to join my Discord server or submit a GitHub issue. You can find solutions to many common problems in the documentation: https://reddit-video-maker-bot.netlify.app/"
"### Thanks for using this tool! Feel free to contribute to this project on GitHub! If you have any questions,"
" feel free to join my Discord server or submit a GitHub issue."
" You can find solutions to many common problems in the documentation: https://reddit-video-maker-bot.netlify.app/"
)
checkversion(__VERSION__)

reddit_id: str
reddit_id: Optional[str] = None
reddit_object: Dict[str, str | list]


def main(POST_ID=None) -> None:
def make_video(POST_ID: typing.Optional[str] = None) -> None:
global reddit_id, reddit_object
reddit_object = get_subreddit_threads(POST_ID)
reddit_id = extract_id(reddit_object)
Expand All @@ -66,57 +68,37 @@

def run_many(times) -> None:
for x in range(1, times + 1):
print_step(
f'on the {x}{("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")[x % 10]} iteration of {times}'
)
main()
Popen("cls" if name == "nt" else "clear", shell=True).wait()
print_step(f"on the {format_ordinal(x)} iteration of {times}")
make_video()
Popen("cls" if os.name == "nt" else "clear", shell=True).wait()


def shutdown() -> NoReturn:
if "reddit_id" in globals():
if reddit_id is not None:
print_markdown("## Clearing temp files")
cleanup(reddit_id)

print("Exiting...")
sys.exit()


if __name__ == "__main__":
if sys.version_info.major != 3 or sys.version_info.minor not in [10, 11, 12]:
print(
"Hey! Congratulations, you've made it so far (which is pretty rare with no Python 3.10). Unfortunately, this program only works on Python 3.10. Please install Python 3.10 and try again."
)
sys.exit()
def main():
check_python()
ffmpeg_install()
directory = Path().absolute()
config = settings.check_toml(
f"{directory}/utils/.config.template.toml", f"{directory}/config.toml"
)
config is False and sys.exit()

if (
not settings.config["settings"]["tts"]["tiktok_sessionid"]
or settings.config["settings"]["tts"]["tiktok_sessionid"] == ""
) and config["settings"]["tts"]["voice_choice"] == "tiktok":
print_substep(
"TikTok voice requires a sessionid! Check our documentation on how to obtain one.",
"bold red",
)
sys.exit()
config = get_config()

try:
if config["reddit"]["thread"]["post_id"]:
for index, post_id in enumerate(config["reddit"]["thread"]["post_id"].split("+")):
post_ids = config["reddit"]["thread"]["post_id"].split("+")
if post_ids:
for index, post_id in enumerate(post_ids):
index += 1
print_step(
f'on the {index}{("st" if index % 10 == 1 else ("nd" if index % 10 == 2 else ("rd" if index % 10 == 3 else "th")))} post of {len(config["reddit"]["thread"]["post_id"].split("+"))}'
)
main(post_id)
Popen("cls" if name == "nt" else "clear", shell=True).wait()
print_step(f"on the {format_ordinal(index)} post of {len(post_ids)}")
make_video(post_id)
Popen("cls" if os.name == "nt" else "clear", shell=True).wait()
elif config["settings"]["times_to_run"]:
run_many(config["settings"]["times_to_run"])
else:
main()
make_video()
except KeyboardInterrupt:
shutdown()
except ResponseException:
Expand All @@ -128,9 +110,14 @@
config["settings"]["tts"]["elevenlabs_api_key"] = "REDACTED"
config["settings"]["tts"]["openai_api_key"] = "REDACTED"
print_step(
f"Sorry, something went wrong with this version! Try again, and feel free to report this issue at GitHub or the Discord community.\n"
f"Sorry, something went wrong with this version! Try again,"
"and feel free to report this issue at GitHub or the Discord community.\n"
f"Version: {__VERSION__} \n"
f"Error: {err} \n"
f'Config: {config["settings"]}'
)
raise err


if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion reddit/subreddit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import typing

import praw
from praw.models import MoreComments
Expand All @@ -13,7 +14,7 @@
from utils.voice import sanitize_text


def get_subreddit_threads(POST_ID: str):
def get_subreddit_threads(POST_ID: typing.Optional[str]):
"""
Returns a list of threads from the AskReddit subreddit.
"""
Expand Down
61 changes: 0 additions & 61 deletions utils/.config.template.toml

This file was deleted.

3 changes: 2 additions & 1 deletion utils/cleanup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
import shutil
from os.path import exists
from typing import Literal


def _listdir(d): # listdir with full path
return [os.path.join(d, f) for f in os.listdir(d)]


def cleanup(reddit_id) -> int:
def cleanup(reddit_id) -> None | Literal[1]:
"""Deletes all temporary assets in assets/temp

Returns:
Expand Down
Loading
Loading