-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit_api.py
69 lines (51 loc) · 1.78 KB
/
reddit_api.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
import json
from pprint import pprint
import requests
from praw import Reddit
from dotenv import load_dotenv
import os
import shutil
from config import dotenv_path, submission_download_dir
from log import get_logger
logger = get_logger(__name__)
load_dotenv(dotenv_path)
reddit = Reddit(client_id=os.environ.get('REDDIT_CLIENT_ID'),
client_secret=os.environ.get('REDDIT_CLIENT_SECRET'),
user_agent='glue-gun-bot',
username=os.environ.get('REDDIT_USERNAME'),
password=os.environ.get('REDDIT_PASSWORD'))
def get_video_data(post_id):
subm = reddit.submission(id=post_id)
return dict(
id=post_id,
url=subm.media['reddit_video']['fallback_url'],
duration=subm.media['reddit_video']['duration'],
height=subm.media['reddit_video']['height'],
width=subm.media['reddit_video']['width']
)
def download_video(submission_id, video_url):
dir_path = os.path.join(submission_download_dir, submission_id)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
video_fp = os.path.join(dir_path, 'video.mp4')
with requests.get(video_url, stream=True) as r:
with open(video_fp, 'wb') as f:
shutil.copyfileobj(r.raw, f)
return video_fp
else:
logger.info(f'Submission "{submission_id}" already downloaded')
def get_new_submissions(subreddit):
subs = reddit.subreddit(subreddit).hot(limit=None)
to_return = []
for sub in subs:
if sub.over_18 or not sub.is_video:
continue
to_return.append(sub)
return to_return
def main():
# download_video_from_submission('9qcbr9')
subs = get_new_submissions('diwhy')
subs = list(subs)
print(subs[0])
if __name__ == '__main__':
main()