diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1d1fe94 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +Dockerfile \ No newline at end of file diff --git a/env.template b/.env.template similarity index 73% rename from env.template rename to .env.template index 9f14b65..bd0a79e 100644 --- a/env.template +++ b/.env.template @@ -10,6 +10,3 @@ THEME="" # Enter a subreddit, e.g. "AskReddit" SUBREDDIT="" - -Valid options are "male" and "female" for the variable below -VoiceGender="" diff --git a/.gitignore b/.gitignore index 95aa7c3..23a0507 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ assets/ .env reddit-bot-351418-5560ebc49cac.json __pycache__ -venv \ No newline at end of file +out +venv +old diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1f68ea0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM mcr.microsoft.com/playwright + +RUN apt update +RUN apt install python3-pip -y + +RUN mkdir /app +ADD . /app +WORKDIR /app +RUN pip install -r requirements.txt + +# tricks for pytube : https://github.com/elebumm/RedditVideoMakerBot/issues/142 +# (NOTE : This is no longer useful since pytube was removed from the dependencies) +# RUN sed -i 's/re.compile(r"^\\w+\\W")/re.compile(r"^\\$*\\w+\\W")/' /usr/local/lib/python3.8/dist-packages/pytube/cipher.py + +CMD ["python3", "main.py"] diff --git a/README.md b/README.md index 00e30da..dbb0250 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,10 @@ These videos on TikTok, YouTube and Instagram get MILLIONS of views across all p 3. Run `pip3 install -r requirements.txt` 4. Run `playwright install` and `playwright install-deps`. 5. Run `python3 main.py` -6. ... -7. Enjoy 😎 +6. Enjoy 😎 + +If you want to see more detailed guide, please refer to the official [documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/). +*The Documentation is still being developed and worked on, please be patient as we change / add new knowledge! ## Contributing & Ways to improve 📈 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..7d4dfc6 --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +docker build -t rvmt . \ No newline at end of file diff --git a/main.py b/main.py index a5b64c6..b813c01 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,13 @@ from utils.console import print_markdown -import time - from reddit.subreddit import get_subreddit_threads from video_creation.background import download_background, chop_background_video from video_creation.voices import save_text_to_mp3 from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts from video_creation.final_video import make_final_video +from dotenv import load_dotenv +import os, time, shutil + +REQUIRED_VALUES = ["REDDIT_CLIENT_ID","REDDIT_CLIENT_SECRET","REDDIT_USERNAME","REDDIT_PASSWORD"] print_markdown( "### Thanks for using this tool! [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com) If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue." @@ -13,11 +15,23 @@ print_markdown( time.sleep(3) +load_dotenv() + +configured = True + +if not os.path.exists(".env"): + shutil.copy(".env.template", ".env") + configured = False -reddit_object = get_subreddit_threads() +for val in REQUIRED_VALUES: + if val not in os.environ or not os.getenv(val): + print(f"Please set the variable \"{val}\" in your .env file.") + configured = False -length, number_of_comments = save_text_to_mp3(reddit_object) -download_screenshots_of_reddit_posts(reddit_object, number_of_comments) -download_background() -chop_background_video(length) -final_video = make_final_video(number_of_comments) +if configured: + reddit_object = get_subreddit_threads() + length, number_of_comments = save_text_to_mp3(reddit_object) + download_screenshots_of_reddit_posts(reddit_object, number_of_comments, os.getenv("THEME", "light")) + download_background() + chop_background_video(length) + final_video = make_final_video(number_of_comments) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index ed83b91..1d47974 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -1,8 +1,6 @@ from utils.console import print_markdown, print_step, print_substep -import praw -import random from dotenv import load_dotenv -import os +import os, random, praw, re def get_subreddit_threads(): @@ -12,10 +10,10 @@ def get_subreddit_threads(): """ load_dotenv() - _SUBREDDIT = os.getenv("SUBREDDIT") - print_step(f"Getting {_SUBREDDIT} threads...") - if os.getenv("REDDIT_2FA").lower() == "yes": + print_step("Getting AskReddit threads...") + + if os.getenv("REDDIT_2FA", default="no").casefold() == "yes": print( "\nEnter your two-factor authentication code from your authenticator app.\n" ) @@ -37,12 +35,12 @@ def get_subreddit_threads(): ) if os.getenv("SUBREDDIT"): - subreddit = reddit.subreddit(os.getenv("SUBREDDIT")) + subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT"))) else: # ! Prompt the user to enter a subreddit try: subreddit = reddit.subreddit( - input("What subreddit would you like to pull from? ") + re.sub(r"r\/", "", input("What subreddit would you like to pull from? ")) ) except ValueError: subreddit = reddit.subreddit("askreddit") diff --git a/requirements.txt b/requirements.txt index eb5b22e..fd28b7f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -librosa==0.9.1 +gTTS==2.2.4 moviepy==1.0.3 +mutagen==1.45.1 playwright==1.22.0 praw==7.6.0 python-dotenv==0.20.0 -pyttsx3==2.90 rich==12.4.4 yt_dlp==2022.5.18 diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..4dcb69a --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +docker run -v $(pwd)/out/:/app/assets -v $(pwd)/.env:/app/.env -it rvmt \ No newline at end of file diff --git a/video_creation/cookies.json b/video_creation/cookies.json new file mode 100644 index 0000000..2e4e116 --- /dev/null +++ b/video_creation/cookies.json @@ -0,0 +1,8 @@ +[ + { + "name": "USER", + "value": "eyJwcmVmcyI6eyJ0b3BDb250ZW50RGlzbWlzc2FsVGltZSI6MCwiZ2xvYmFsVGhlbWUiOiJSRURESVQiLCJuaWdodG1vZGUiOnRydWUsImNvbGxhcHNlZFRyYXlTZWN0aW9ucyI6eyJmYXZvcml0ZXMiOmZhbHNlLCJtdWx0aXMiOmZhbHNlLCJtb2RlcmF0aW5nIjpmYWxzZSwic3Vic2NyaXB0aW9ucyI6ZmFsc2UsInByb2ZpbGVzIjpmYWxzZX0sInRvcENvbnRlbnRUaW1lc0Rpc21pc3NlZCI6MH19", + "domain": ".reddit.com", + "path": "/" + } +] diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 91fed8a..d3d32ef 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -1,10 +1,11 @@ -from playwright.sync_api import sync_playwright +from playwright.sync_api import sync_playwright, ViewportSize from pathlib import Path from rich.progress import track from utils.console import print_step, print_substep +import json -def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): +def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): """Downloads screenshots of reddit posts as they are seen on the web. Args: @@ -20,11 +21,17 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): print_substep("Launching Headless Browser...") browser = p.chromium.launch() + context = browser.new_context() + + if theme.casefold() == "dark": + cookie_file = open('video_creation/cookies.json') + cookies = json.load(cookie_file) + context.add_cookies(cookies) # Get the thread screenshot - page = browser.new_page() + page = context.new_page() page.goto(reddit_object["thread_url"]) - + page.set_viewport_size(ViewportSize(width=1920, height=1080)) if page.locator('[data-testid="content-gate"]').is_visible(): # This means the post is NSFW and requires to click the proceed button. @@ -50,4 +57,6 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): page.locator(f"#t1_{comment['comment_id']}").screenshot( path=f"assets/png/comment_{idx}.png" ) - print_substep("Screenshots downloaded successfully.", style="bold green") + + print_substep("Screenshots downloaded Successfully.", + style="bold green")