-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_post.py
79 lines (63 loc) · 2.49 KB
/
auto_post.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
import json
import numpy as np
from time import sleep
from reddit_fetcher import RedditStoryFetcher
from gen_audio_subtitle import AudioSubtitleGenerator
from post_to_insta import InstagramUploader
from gen_vid import VideoGenerator
from dotenv import load_dotenv
import os
load_dotenv()
USERNAME = os.getenv('USERNAME')
PASSWORD = os.getenv('PASSWORD')
MIN_SLEEP_TIME_HOURS = os.getenv("MIN_SLEEP_TIME_HOURS")
MAX_SLEEP_TIME_HOURS = os.getenv("MAX_SLEEP_TIME_HOURS")
AVG_SLEEP_TIME_HOURS = os.getenv("AVG_SLEEP_TIME_HOURS")
VARIANCE_SLEEP_TIME_HOURS = os.getenv("VARIANCE_SLEEP_TIME_HOURS")
SECONDS_IN_A_HOUR = 3600
if __name__ == "__main__":
uploader = InstagramUploader(
username=USERNAME,
password=PASSWORD,
session_file="session.json",
video_file="video.mp4",
thumbnail_file="thumbnail.png",
caption_file="story_title.txt",
hashtags_file="hashtags.txt"
)
uploader.login_user()
while True:
# fetch stories if there are none to be processed
with open("to_be_processed.json", "r") as f:
data = json.load(f)
if len(data) == 0:
print("No stories to be processed. Fetching new stories...")
fetcher = RedditStoryFetcher("subreddits.txt", "stories.json")
fetcher.run("to_be_processed.json")
# generate audio and subtitles
print("Generating audio and subtitles...")
generator = AudioSubtitleGenerator(mode="system")
generator.run()
print("Generating video...")
# generate video
processor = VideoGenerator(
video_file="bg_vid.mp4",
audio_file="output_audio.mp3",
subtitle_file="output_captions.vtt",
title_file="story_title.txt",
replacements_file="replacements.json",
output_video="video.mp4",
thumbnail_file="thumbnail.png"
)
processor.process_video()
print("Posting to Instagram")
# post video to IG
uploader.upload_clip()
print("Successful post!")
# sleep time is normal distribution N(8, 2) hours
# minumum 4 hours, maximum 12 hours
sleep_time = np.random.normal(AVG_SLEEP_TIME_HOURS, VARIANCE_SLEEP_TIME_HOURS)
sleep_time = np.clip(sleep_time, MIN_SLEEP_TIME_HOURS, MAX_SLEEP_TIME_HOURS)
print(f"Sleeping for {round(sleep_time, 2)} hours...")
sleep(sleep_time * SECONDS_IN_A_HOUR) # convert to seconds
print("\n\n\n")