-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
139 lines (116 loc) · 4.42 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import praw
import os
import random
import time
from uploader import uploadVideo
from texttospeech import createTTS, wcreateTTS
from subtitles import createsrt, format_time, get_screen_size, subtitle_generator, add_subtitles
from videocreation import createVideo, createVideoMov
from textprocess import createPostTextFile, preprocessText
from dotenv import load_dotenv
#choices for randomlym selected captions
TITLES = ["thoughts?", "opinions?", "what do you think?", "let's discuss"]
#randomizes the choices
TITLE = random.choice(TITLES)
#list of hashtags for the post
TAGS = ["aita", "askreddit", "redditstories","tifu","redditreadings"]
#randomizes the order of the hashtags to seem more human
random.shuffle(TAGS)
USERS = []
SESSION_ID = os.getenv('SESSION_ID')
url_prefix = "us"
schedule_time = 1800 # uploads every 30 minutes to not get shadowbanned, you can change it but its not gonna be my problem if you do
# create directories if they do not exist
directories = ["srt", "posts", "mp3", "mp4", "wav", "results"]
for dir in directories:
if not os.path.exists(dir):
os.makedirs(dir)
print(f"{dir} directory does not exist, creating.")
# create text files if they do not exist
text_files = ['post_ids.txt', 'uploaded_ids.txt']
for text_file in text_files:
if not os.path.exists(text_file):
with open(text_file, 'w') as file:
pass
print(f"'{text_file}' does not exist, creating.")
reddit = praw.Reddit(
client_id=os.environ["REDDIT_CLIENT_ID"],
client_secret=os.environ["REDDIT_CLIENT_SECRET"],
password=os.environ["REDDIT_PASSWORD"],
user_agent=os.environ["REDDIT_USER_AGENT"],
username=os.environ["REDDIT_USERNAME"],
)
subreddit_name = os.environ["SUBREDDIT_NAME"]
post_limit = int(os.environ["POST_LIMIT"])
time_filter = os.environ["TIME_FILTER"]
posts = reddit.subreddit(subreddit_name).top(time_filter=time_filter, limit=post_limit)
# Define functions
def checkPostId(post_id):
with open('post_ids.txt', 'r') as f:
if post_id in f.read():
return True
return False
def addPostId(post_id):
try:
with open('post_ids.txt', 'a') as f:
f.write(post_id + '\n')
except FileNotFoundError:
with open('post_ids.txt', 'w') as f:
f.write(post_id + '\n')
def checkUploadId(post_id):
with open('uploaded_ids.txt', 'r') as f:
if post_id in f.read():
return True
return False
def addUploadId(post_id):
try:
with open('uploaded_ids.txt', 'a') as f:
f.write(post_id + '\n')
except FileNotFoundError:
with open('uploaded_ids.txt', 'w') as f:
f.write(post_id + '\n')
def check_files_exist(post_id):
filenames = [
os.path.join("posts", f"{post_id}.txt"),
os.path.join("srt", f"{post_id}.srt"),
os.path.join("results", f'subtitled{post_id}.mp4'),
os.path.join("mp3", f"{post_id}.mp3"),
]
for filename in filenames:
if not os.path.exists(filename):
print(f"{post_id} - File {filename} does not exist.")
return False
print(f"All required files for post ID {post_id} exist.")
return True
# Iterate through each post and create TTS audio and text files
for post in posts:
start_time = time.time()
title = post.title
body = post.selftext
author = post.author
post_id = post.id
post_url = post.url
input_text = f"{title} {body}"
if checkUploadId(post_id):
print("Post ID", post_id, "has already been processed and uploaded.")
continue # Skip to next post
if checkPostId(post_id):
print("Post ID", post_id, "has already been processed.")
file = 'results/' + 'subtitled' + f'{post_id}.mp4'
if not check_files_exist(post_id):
print(f"{post_id} - Post link:", post_url)
createPostTextFile(title, body, author, post_id,input_text)
preprocessText(post_id)
wcreateTTS(post_id,input_text)
createVideo(post_id)
createsrt("mp4",post_id)
add_subtitles(post_id)
print(f"{post_id} - Post created successfully")
print("******Post Completed Successfully*****")
addPostId(post_id)
schedule_time = schedule_time + 600
uploadVideo(SESSION_ID, file, TITLE, TAGS, USERS, url_prefix, schedule_time)
addUploadId(post_id)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Total time taken for post ID {post_id}: {elapsed_time} seconds")