Skip to content

Commit

Permalink
Merge pull request #1093 from elebumm/develop
Browse files Browse the repository at this point in the history
2.4
  • Loading branch information
callumio authored Aug 8, 2022
2 parents 2cfd20b + 8463794 commit 3eff3e4
Show file tree
Hide file tree
Showing 24 changed files with 353 additions and 123 deletions.
22 changes: 21 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Contributing to Reddit Video Maker Bot 🎥

Thanks for taking the time to contribute! ❤️
Expand Down Expand Up @@ -105,6 +106,25 @@ Enhancement suggestions are tracked as [GitHub issues](https://github.com/elebum

You development environment should follow the requirements stated in the [README file](README.md). If you are not using the specified versions, **please reference this in your pull request**, so reviewers can test your code on both versions.

#### Setting up your development repository

These steps are only specified for beginner developers trying to contribute to this repository.
If you know how to make a fork and clone, you can skip these steps.

Before contributing, follow these steps (if you are a beginner)

- Create a fork of this repository to your personal account
- Clone the repo to your computer
- Make sure that you have all dependencies installed
- Run `python main.py` to make sure that the program is working
- Now, you are all setup to contribute your own features to this repo!

Even if you are a beginner to working with python or contributing to open source software,
don't worry! You can still try contributing even to the documentation!

("Setting up your development repository" was written by a beginner developer themselves!)


#### Making your first PR

When making your PR, follow these guidelines:
Expand All @@ -114,7 +134,7 @@ When making your PR, follow these guidelines:
- You link any issues that are resolved or fixed by your changes. (this is done by typing "Fixes #\<issue number\>") in your pull request
- Where possible, you have used `git pull --rebase`, to avoid creating unnecessary merge commits
- You have meaningful commits, and if possible, follow the commit style guide of `type: explanation`
- Here are the commit types:
- Here are the commit types:
- **feat** - a new feature
- **fix** - a bug fix
- **docs** - a change to documentation / commenting
Expand Down
30 changes: 30 additions & 0 deletions GUI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Import the server module
import http.server
import webbrowser
# Set the hostname
HOST = "localhost"
# Set the port number
PORT = 4000

# Define class to display the index page of the web server
class PythonServer(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/GUI':
self.path = 'index.html'
return http.server.SimpleHTTPRequestHandler.do_GET(self)

# Declare object of the class
webServer = http.server.HTTPServer((HOST, PORT), PythonServer)
# Print the URL of the webserver, new =2 opens in a new tab
print(f"Server started at http://{HOST}:{PORT}/GUI/")
webbrowser.open(f'http://{HOST}:{PORT}/GUI/', new = 2)
print("Website opened in new tab")
print("Press Ctrl+C to quit")
try:
# Run the web server
webServer.serve_forever()
except KeyboardInterrupt:
# Stop the web server
webServer.server_close()
print("The server is stopped.")
exit()
2 changes: 1 addition & 1 deletion GUI/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
video += '<div class="d-flex justify-content-between align-items-center">';
video += '<div class="btn-group">';
video += '<a href="https://www.reddit.com/r/'+value.subreddit+'/comments/'+value.id+'/" class="btn btn-sm btn-outline-secondary" target="_blank">View</a>';
video += '<a href="results/'+value.subreddit+'/'+value.filename+'" class="btn btn-sm btn-outline-secondary" download>Download</a>';
video += '<a href="http://localhost:4000/results/'+value.subreddit+'/'+value.filename+'" class="btn btn-sm btn-outline-secondary" download>Download</a>';
video += '</div>';
video += '<div class="btn-group">';
video += '<button type="button" data-toggle="tooltip" id="copy" data-original-title="Copy to clipboard" class="btn btn-sm btn-outline-secondary" data-clipboard-text="'+getCopyData(value.subreddit, value.reddit_title, value.filename, value.background_credit)+'"><i class="bi bi-card-text"></i></button>';
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,22 @@ On MacOS and Linux (debian, arch, fedora and centos, and based on those), you ca
This can also be used to update the installation

4. Run `python main.py`
5. Visit [the Reddit Apps page.](https://www.reddit.com/prefs/apps), and set up an app that is a "script".
5. Visit [the Reddit Apps page.](https://www.reddit.com/prefs/apps), and set up an app that is a "script". Paste any URL in redirect URL. Ex:google.com
6. The bot will ask you to fill in your details to connect to the Reddit API, and configure the bot to your liking
7. Enjoy 😎
8. If you need to reconfigure the bot, simply open the `config.toml` file and delete the lines that need to be changed. On the next run of the bot, it will help you reconfigure those options.

(Note if you got an error installing or running the bot try first rerunning the command with a three after the name e.g. python3 or pip3)

If you want to read more detailed guide about the bot, please refer to the [documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/)

## Video

https://user-images.githubusercontent.com/66544866/173453972-6526e4e6-c6ef-41c5-ab40-5d275e724e7c.mp4

## Contributing & Ways to improve 📈

In its current state, this bot does exactly what it needs to do. However, lots of improvements can be made.
In its current state, this bot does exactly what it needs to do. However, improvements can always be made!

I have tried to simplify the code so anyone can read it and start contributing at any skill level. Don't be shy :) contribute!

Expand Down
2 changes: 1 addition & 1 deletion TTS/GTTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class GTTS:
def __init__(self):
self.max_chars = 0
self.max_chars = 5000
self.voices = []

def run(self, text, filepath):
Expand Down
2 changes: 1 addition & 1 deletion TTS/aws_polly.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class AWSPolly:
def __init__(self):
self.max_chars = 0
self.max_chars = 3000
self.voices = voices

def run(self, text, filepath, random_voice: bool = False):
Expand Down
28 changes: 16 additions & 12 deletions TTS/engine_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from utils.voice import sanitize_text
from utils import settings

DEFUALT_MAX_LENGTH: int = 50 # video length variable
DEFAULT_MAX_LENGTH: int = 50 # video length variable


class TTSEngine:
Expand All @@ -35,13 +35,15 @@ def __init__(
tts_module,
reddit_object: dict,
path: str = "assets/temp/mp3",
max_length: int = DEFUALT_MAX_LENGTH,
max_length: int = DEFAULT_MAX_LENGTH,
last_clip_length: int = 0,
):
self.tts_module = tts_module()
self.reddit_object = reddit_object
self.path = path
self.max_length = max_length
self.length = 0
self.last_clip_length = last_clip_length

def run(self) -> Tuple[int, int]:

Expand All @@ -55,24 +57,24 @@ def run(self) -> Tuple[int, int]:

print_step("Saving Text to MP3 files...")

self.call_tts("title", self.reddit_object["thread_title"])
if (
self.reddit_object["thread_post"] != ""
and settings.config["settings"]["storymode"] == True
):
self.call_tts("posttext", self.reddit_object["thread_post"])
self.call_tts("title", process_text(self.reddit_object["thread_title"]))
processed_text = process_text(self.reddit_object["thread_post"])
if processed_text != "" and settings.config["settings"]["storymode"] == True:
self.call_tts("posttext", processed_text)

idx = None
for idx, comment in track(enumerate(self.reddit_object["comments"]), "Saving..."):
# ! Stop creating mp3 files if the length is greater than max length.
if self.length > self.max_length:
self.length -= self.last_clip_length
idx -= 1
break
if (
len(comment["comment_body"]) > self.tts_module.max_chars
): # Split the comment if it is too long
self.split_post(comment["comment_body"], idx) # Split the comment
else: # If the comment is not too long, just call the tts engine
self.call_tts(f"{idx}", comment["comment_body"])
self.call_tts(f"{idx}", process_text(comment["comment_body"]))

print_substep("Saved Text to MP3 files successfully.", style="bold green")
return self.length, idx
Expand All @@ -88,11 +90,12 @@ def split_post(self, text: str, idx: int):
offset = 0
for idy, text_cut in enumerate(split_text):
# print(f"{idx}-{idy}: {text_cut}\n")
if not text_cut or text_cut.isspace():
new_text = process_text(text_cut)
if not new_text or new_text.isspace():
offset += 1
continue

self.call_tts(f"{idx}-{idy - offset}.part", text_cut)
self.call_tts(f"{idx}-{idy - offset}.part", new_text)
split_files.append(AudioFileClip(f"{self.path}/{idx}-{idy - offset}.part.mp3"))

CompositeAudioClip([concatenate_audioclips(split_files)]).write_audiofile(
Expand All @@ -110,13 +113,14 @@ def split_post(self, text: str, idx: int):
# Path(f"{self.path}/{idx}-{i}.part.mp3").unlink()

def call_tts(self, filename: str, text: str):
self.tts_module.run(text=process_text(text), filepath=f"{self.path}/{filename}.mp3")
self.tts_module.run(text, filepath=f"{self.path}/{filename}.mp3")
# try:
# self.length += MP3(f"{self.path}/{filename}.mp3").info.length
# except (MutagenError, HeaderNotFoundError):
# self.length += sox.file_info.duration(f"{self.path}/{filename}.mp3")
try:
clip = AudioFileClip(f"{self.path}/{filename}.mp3")
self.last_clip_length = clip.duration
self.length += clip.duration
clip.close()
except:
Expand Down
42 changes: 42 additions & 0 deletions TTS/pyttsx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import random
import pyttsx3
from utils import settings


class pyttsx:
def __init__(self):
self.max_chars = 5000
self.voices = []

def run(
self,
text: str,
filepath: str,
random_voice=False,
):
voice_id = settings.config["settings"]["tts"]["python_voice"]
voice_num = settings.config["settings"]["tts"]["py_voice_num"]
if voice_id == "" or voice_num == "":
voice_id = 2
voice_num = 3
raise ValueError(
"set pyttsx values to a valid value, switching to defaults"
)
else:
voice_id = int(voice_id)
voice_num = int(voice_num)
for i in range(voice_num):
self.voices.append(i)
i = +1
if random_voice:
voice_id = self.randomvoice()
engine = pyttsx3.init()
voices = engine.getProperty("voices")
engine.setProperty(
"voice", voices[voice_id].id
) # changing index changes voices but ony 0 and 1 are working here
engine.save_to_file(text, f"{filepath}")
engine.runAndWait()

def randomvoice(self):
return random.choice(self.voices)
23 changes: 13 additions & 10 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,42 @@ function install_macos(){
# Function to install for arch (and other forks like manjaro)
function install_arch(){
echo "Installing required packages"
sudo pacman -S --needed python3 tk git && python3 -m ensurepip || install_fail
sudo pacman -S --needed python3 tk git && python3 -m ensurepip unzip || install_fail
}

# Function to install for debian (and ubuntu)
function install_deb(){
echo "Installing required packages"
sudo apt install python3 python3-dev python3-tk python3-pip git || install_fail
sudo apt install python3 python3-dev python3-tk python3-pip unzip || install_fail
}

# Function to install for fedora (and other forks)
function install_fedora(){
echo "Installing required packages"
sudo dnf install python3 python3-tkinter python3-pip git python3-devel || install_fail
sudo dnf install python3 python3-tkinter python3-pip python3-devel unzip || install_fail
}

# Function to install for centos (and other forks based on it)
function install_centos(){
echo "Installing required packages"
sudo yum install -y python3 || install_fail
sudo yum install -y python3-tkinter epel-release python3-pip git || install_fail
sudo yum install -y python3-tkinter epel-release python3-pip unzip|| install_fail
}

function get_the_bot(){
echo "Downloading the bot"
git clone https://github.com/elebumm/RedditVideoMakerBot.git
rm -rf RedditVideoMakerBot-master
curl -sL https://github.com/elebumm/RedditVideoMakerBot/archive/refs/heads/master.zip -o master.zip
unzip master.zip
rm -rf master.zip
}

#install python dependencies
function install_python_dep(){
# tell the user that the script is going to install the python dependencies
echo "Installing python dependencies"
# cd into the directory
cd RedditVideoMakerBot
cd RedditVideoMakerBot-master
# install the dependencies
pip3 install -r requirements.txt
# cd out
Expand All @@ -112,7 +115,7 @@ function install_playwright(){
# tell the user that the script is going to install playwright
echo "Installing playwright"
# cd into the directory where the script is downloaded
cd RedditVideoMakerBot
cd RedditVideoMakerBot-master
# run the install script
python3 -m playwright install
python3 -m playwright install-deps
Expand Down Expand Up @@ -198,9 +201,9 @@ function install_main(){
install_playwright
fi

DIR="./RedditVideoMakerBot"
DIR="./RedditVideoMakerBot-master"
if [ -d "$DIR" ]; then
printf "\nThe bot is already installed, want to run it?"
printf "\nThe bot is installed, want to run it?"
# if -y (assume yes) continue
if [[ ASSUME_YES -eq 1 ]]; then
echo "Assuming yes"
Expand All @@ -214,7 +217,7 @@ function install_main(){
exit 1
fi
fi
cd RedditVideoMakerBot
cd RedditVideoMakerBot-master
python3 main.py
fi
}
Expand Down
Loading

0 comments on commit 3eff3e4

Please sign in to comment.