-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmain.py
74 lines (57 loc) · 2.25 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
import os, shutil, glob, traceback
from redvid import Downloader
import render
import reddit_scraper
import upload
import config
def main():
# Open comma separated database. If doesn't exist, make it.
try:
database = open(config.database, 'r').read().split(',')
except FileNotFoundError:
database = ''
open(config.database, 'x')
# Delete temp_clips and remake it to empty the content
if os.path.exists("temp_clips"):
shutil.rmtree("temp_clips")
os.makedirs("temp_clips")
# Get list of videos from Reddit
vid_list = reddit_scraper.scrape_reddit(config.subreddit)
# Download the first video that is less than 180s and not in the database
for vid in vid_list:
reddit = Downloader(max_q=True)
reddit.url = vid["url"]
if (reddit.duration < 180) and (vid["url"] not in database):
break
else:
vid = None
# Check if a suitable video was found
if not vid:
print("No videos less than 180s found!")
return False
print("Video chosen:", vid)
# Update the database with the video's URL
# If upload fails or succeed, video will not be picked again.
with open(config.database, "a") as f:
f.write(f"{vid['url']},")
reddit_scraper.download_vid(vid["url"], "temp_clips")
shutil.copy(glob.glob(os.path.join('temp_clips', '*.mp4'))[0], os.path.join('temp_clips', 'main_clip.mp4'))
render.render("temp_clips", "main_clip.mp4", "output.mp4", config.video["dimensions"])
# Upload the video to YouTube
config.youtube["title"] = vid["title"] + " #shorts"
config.youtube["description"] = "Video by: " + vid["author"]
print("Uploading...")
uploaded = upload.upload("temp_clips/output.mp4", config.youtube)
return uploaded
if __name__ == "__main__":
# Attempt 10 attempts before quitting
# This is due to packages this project relies on are not being reliable
success = None
for attempt in range(10):
try:
success = main()
except Exception as e:
print(f"Error occured on {attempt + 1}:\n", traceback.format_exc(), "\nRetrying...")
if success:
print("Video uploaded successfully! Check YouTube :)")
break