Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/main.py
493 views
1
#!/usr/bin/env python
2
import math
3
import sys
4
from os import name
5
from pathlib import Path
6
from subprocess import Popen
7
from typing import Dict, NoReturn
8
9
from prawcore import ResponseException
10
11
from reddit.subreddit import get_subreddit_threads
12
from utils import settings
13
from utils.cleanup import cleanup
14
from utils.console import print_markdown, print_step, print_substep
15
from utils.ffmpeg_install import ffmpeg_install
16
from utils.id import extract_id
17
from utils.version import checkversion
18
from video_creation.background import (
19
chop_background,
20
download_background_audio,
21
download_background_video,
22
get_background_config,
23
)
24
from video_creation.final_video import make_final_video
25
from video_creation.screenshot_downloader import get_screenshots_of_reddit_posts
26
from video_creation.voices import save_text_to_mp3
27
28
__VERSION__ = "3.4.0"
29
30
print(
31
"""
32
██████╗ ███████╗██████╗ ██████╗ ██╗████████╗ ██╗ ██╗██╗██████╗ ███████╗ ██████╗ ███╗ ███╗ █████╗ ██╗ ██╗███████╗██████╗
33
██╔══██╗██╔════╝██╔══██╗██╔══██╗██║╚══██╔══╝ ██║ ██║██║██╔══██╗██╔════╝██╔═══██╗ ████╗ ████║██╔══██╗██║ ██╔╝██╔════╝██╔══██╗
34
██████╔╝█████╗ ██║ ██║██║ ██║██║ ██║ ██║ ██║██║██║ ██║█████╗ ██║ ██║ ██╔████╔██║███████║█████╔╝ █████╗ ██████╔╝
35
██╔══██╗██╔══╝ ██║ ██║██║ ██║██║ ██║ ╚██╗ ██╔╝██║██║ ██║██╔══╝ ██║ ██║ ██║╚██╔╝██║██╔══██║██╔═██╗ ██╔══╝ ██╔══██╗
36
██║ ██║███████╗██████╔╝██████╔╝██║ ██║ ╚████╔╝ ██║██████╔╝███████╗╚██████╔╝ ██║ ╚═╝ ██║██║ ██║██║ ██╗███████╗██║ ██║
37
╚═╝ ╚═╝╚══════╝╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝╚═════╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
38
"""
39
)
40
print_markdown(
41
"### 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/"
42
)
43
checkversion(__VERSION__)
44
45
reddit_id: str
46
reddit_object: Dict[str, str | list]
47
48
49
def main(POST_ID=None) -> None:
50
global reddit_id, reddit_object
51
reddit_object = get_subreddit_threads(POST_ID)
52
reddit_id = extract_id(reddit_object)
53
print_substep(f"Thread ID is {reddit_id}", style="bold blue")
54
length, number_of_comments = save_text_to_mp3(reddit_object)
55
length = math.ceil(length)
56
get_screenshots_of_reddit_posts(reddit_object, number_of_comments)
57
bg_config = {
58
"video": get_background_config("video"),
59
"audio": get_background_config("audio"),
60
}
61
download_background_video(bg_config["video"])
62
download_background_audio(bg_config["audio"])
63
chop_background(bg_config, length, reddit_object)
64
make_final_video(number_of_comments, length, reddit_object, bg_config)
65
66
67
def run_many(times) -> None:
68
for x in range(1, times + 1):
69
print_step(
70
f'on the {x}{("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")[x % 10]} iteration of {times}'
71
)
72
main()
73
Popen("cls" if name == "nt" else "clear", shell=True).wait()
74
75
76
def shutdown() -> NoReturn:
77
if "reddit_id" in globals():
78
print_markdown("## Clearing temp files")
79
cleanup(reddit_id)
80
81
print("Exiting...")
82
sys.exit()
83
84
85
if __name__ == "__main__":
86
if sys.version_info.major != 3 or sys.version_info.minor not in [10, 11, 12]:
87
print(
88
"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."
89
)
90
sys.exit()
91
ffmpeg_install()
92
directory = Path().absolute()
93
config = settings.check_toml(
94
f"{directory}/utils/.config.template.toml", f"{directory}/config.toml"
95
)
96
config is False and sys.exit()
97
98
if (
99
not settings.config["settings"]["tts"]["tiktok_sessionid"]
100
or settings.config["settings"]["tts"]["tiktok_sessionid"] == ""
101
) and config["settings"]["tts"]["voice_choice"] == "tiktok":
102
print_substep(
103
"TikTok voice requires a sessionid! Check our documentation on how to obtain one.",
104
"bold red",
105
)
106
sys.exit()
107
try:
108
if config["reddit"]["thread"]["post_id"]:
109
for index, post_id in enumerate(config["reddit"]["thread"]["post_id"].split("+")):
110
index += 1
111
print_step(
112
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("+"))}'
113
)
114
main(post_id)
115
Popen("cls" if name == "nt" else "clear", shell=True).wait()
116
elif config["settings"]["times_to_run"]:
117
run_many(config["settings"]["times_to_run"])
118
else:
119
main()
120
except KeyboardInterrupt:
121
shutdown()
122
except ResponseException:
123
print_markdown("## Invalid credentials")
124
print_markdown("Please check your credentials in the config.toml file")
125
shutdown()
126
except Exception as err:
127
config["settings"]["tts"]["tiktok_sessionid"] = "REDACTED"
128
config["settings"]["tts"]["elevenlabs_api_key"] = "REDACTED"
129
config["settings"]["tts"]["openai_api_key"] = "REDACTED"
130
print_step(
131
f"Sorry, something went wrong with this version! Try again, and feel free to report this issue at GitHub or the Discord community.\n"
132
f"Version: {__VERSION__} \n"
133
f"Error: {err} \n"
134
f'Config: {config["settings"]}'
135
)
136
raise err
137
138