-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathra.py
executable file
·118 lines (97 loc) · 3.94 KB
/
ra.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
#!/bin/python3
import requests
from dotenv import load_dotenv
import os
from gtts import gTTS
import argparse
from time import sleep
from app.generate import concat_all
from video import convert_video, download_file
parser = argparse.ArgumentParser(
description='Get subreddit posts, and save them in varius formats(mp3, txt, mp4)')
parser.add_argument('-s',
'--subreddit',
help='subreddit to get posts from',
type=str,
required=True)
parser.add_argument('-l',
'--language',
help='language to save the posts in',
type=str,
required=True)
parser.add_argument('-m', '--meme', type=bool, default=False,
help='generate only meme without text, just video and audio', required=False)
args = parser.parse_args()
def main():
# create the folder if doesent exists
load_dotenv()
os.system("mkdir -p data audio images video videos") # create the folder if doesent exists
auth = requests.auth.HTTPBasicAuth(os.getenv("ID"), os.getenv("SECRET"))
data = {
'grant_type': 'password',
'username': os.getenv("REDDIT_USERNAME"),
'password': os.getenv("PASSWORD")
}
headers = {'User-Agent': 'test/0.0.1'} # the name of the bot/app
res = requests.post('https://www.reddit.com/api/v1/access_token',
auth=auth,
data=data,
headers=headers)
TOKEN = res.json()['access_token']
headers = {**headers, **{'Authorization': f"bearer {TOKEN}"}}
# while the token is valid (~2 hours) we just add headers=headers to our requests
requests.get('https://oauth.reddit.com/api/v1/me', headers=headers)
# ./ra.py -s subreddit -l language -m true
# ./ra.py -s memes -l en
res = requests.get(f"https://oauth.reddit.com/r/{args.subreddit}",
headers=headers)
for post in res.json()["data"]["children"]:
x = post['data']['title']
print(f"Title: {x}")
print(f"""upvotes:{post['data']['ups']}""")
body = post['data']['selftext']
if body == "":
print("No body")
body = "No body"
history = x
else:
print(f"Body length: {len(body)}")
body = post['data']['selftext']
history = x + " " + body
url_img = post['data']['url']
allow_format = ["jpg", "png", "gif", "jpeg"]
if not url_img.endswith(tuple(allow_format)):
print("allow formats:", tuple(allow_format))
print("url_img:", url_img)
print("No image,using tyler")
url_img = "https://media.discordapp.net/attachments/744419261086433282/928387662283427930/tyler.jpg"
else:
url_img = post['data']['url']
print(f"url: {url_img}")
res = requests.get(url_img, headers=headers)
img = requests.get(url_img,
stream=True) # download the image from the url
file = f'data/{x.replace("/"," ")}.{url_img.split(".")[-1]}'
download_file(url_img, file)
f = open(f'data/{x.replace("/"," ")}.txt', "w")
f.write(history)
f.close
t = gTTS(text=history, lang=args.language)
if body != "":
t.save(f'data/{x.replace("/"," ")}.mp3')
else:
print("no text")
# if ./ra.py -s subreddit -l language -m true
if args.meme:
convert_video(file,
f'data/{x.replace("/"," ")}.mp3',
f'data/{x.replace("/"," ")}.mp4')
else:
concat_all(history, 10, f'{x.replace("/"," ")}',
400, 600, language=args.language)
print("---------------------------------------------------------")
for i in range(3):
sleep(1)
print(f"new video in:{i+1}")
if __name__ == '__main__':
main()