Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/main.py
326 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 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 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.3.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
46
def main(POST_ID=None) -> None:
47
global redditid, reddit_object
48
reddit_object = get_subreddit_threads(POST_ID)
49
redditid = id(reddit_object)
50
length, number_of_comments = save_text_to_mp3(reddit_object)
51
length = math.ceil(length)
52
get_screenshots_of_reddit_posts(reddit_object, number_of_comments)
53
bg_config = {
54
"video": get_background_config("video"),
55
"audio": get_background_config("audio"),
56
}
57
download_background_video(bg_config["video"])
58
download_background_audio(bg_config["audio"])
59
chop_background(bg_config, length, reddit_object)
60
make_final_video(number_of_comments, length, reddit_object, bg_config)
61
62
63
def run_many(times) -> None:
64
for x in range(1, times + 1):
65
print_step(
66
f'on the {x}{("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")[x % 10]} iteration of {times}'
67
) # correct 1st 2nd 3rd 4th 5th....
68
main()
69
Popen("cls" if name == "nt" else "clear", shell=True).wait()
70
71
72
def shutdown() -> NoReturn:
73
if "redditid" in globals():
74
print_markdown("## Clearing temp files")
75
cleanup(redditid)
76
77
print("Exiting...")
78
sys.exit()
79
80
81
if __name__ == "__main__":
82
if sys.version_info.major != 3 or sys.version_info.minor not in [10, 11]:
83
print(
84
"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."
85
)
86
sys.exit()
87
ffmpeg_install()
88
directory = Path().absolute()
89
config = settings.check_toml(
90
f"{directory}/utils/.config.template.toml", f"{directory}/config.toml"
91
)
92
config is False and sys.exit()
93
94
if (
95
not settings.config["settings"]["tts"]["tiktok_sessionid"]
96
or settings.config["settings"]["tts"]["tiktok_sessionid"] == ""
97
) and config["settings"]["tts"]["voice_choice"] == "tiktok":
98
print_substep(
99
"TikTok voice requires a sessionid! Check our documentation on how to obtain one.",
100
"bold red",
101
)
102
sys.exit()
103
try:
104
if config["reddit"]["thread"]["post_id"]:
105
for index, post_id in enumerate(config["reddit"]["thread"]["post_id"].split("+")):
106
index += 1
107
print_step(
108
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("+"))}'
109
)
110
main(post_id)
111
Popen("cls" if name == "nt" else "clear", shell=True).wait()
112
elif config["settings"]["times_to_run"]:
113
run_many(config["settings"]["times_to_run"])
114
else:
115
main()
116
except KeyboardInterrupt:
117
shutdown()
118
except ResponseException:
119
print_markdown("## Invalid credentials")
120
print_markdown("Please check your credentials in the config.toml file")
121
shutdown()
122
except Exception as err:
123
config["settings"]["tts"]["tiktok_sessionid"] = "REDACTED"
124
config["settings"]["tts"]["elevenlabs_api_key"] = "REDACTED"
125
print_step(
126
f"Sorry, something went wrong with this version! Try again, and feel free to report this issue at GitHub or the Discord community.\n"
127
f"Version: {__VERSION__} \n"
128
f"Error: {err} \n"
129
f'Config: {config["settings"]}'
130
)
131
raise err
132
133