-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (40 loc) · 1.46 KB
/
main.py
File metadata and controls
49 lines (40 loc) · 1.46 KB
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
import praw
import datetime
from config import (
CLIENT_ID,
CLIENT_SECRET,
USER_AGENT,
REDDIT_USERNAME,
REDDIT_PASSWORD,
SUBREDDITS,
TARGET_SUBREDDIT,
)
def fetch_top_posts(reddit, subreddit):
top_posts = []
# Fetch top posts from the last 24 hours (daily)
for submission in reddit.subreddit(subreddit).top("day", limit=10):
top_posts.append((submission.title, submission.url, submission.author.name))
return top_posts
def compile_digest(reddit, subreddits):
compiled_content = ""
for subreddit in subreddits:
top_posts = fetch_top_posts(reddit, subreddit)
subreddit_url = f"https://www.reddit.com/r/{subreddit}"
compiled_content += f"**[{subreddit}](https://www.reddit.com/r/{subreddit}) Top Posts - {datetime.date.today()}**\n\n"
for i, (title, url, author) in enumerate(top_posts, start=1):
compiled_content += f"{i}. [{title}]({url}) by u/{author}\n\n"
return compiled_content
reddit = praw.Reddit(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
user_agent=USER_AGENT,
username=REDDIT_USERNAME,
password=REDDIT_PASSWORD,
)
daily_digest = compile_digest(reddit, SUBREDDITS)
target_subreddit = TARGET_SUBREDDIT
subreddit = reddit.subreddit(target_subreddit)
submission = subreddit.submit(
title=f"Daily Digest - {datetime.date.today()}", selftext=daily_digest
)
print(f"Posted to: {submission.url}")