-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreddit_bot.py
More file actions
190 lines (158 loc) · 6.57 KB
/
Copy pathreddit_bot.py
File metadata and controls
190 lines (158 loc) · 6.57 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import praw
import pymongo
import time
from dotenv import load_dotenv
import requests # <-- New import
load_dotenv() # Load variables from .env into os.environ
# -----------------------------------
# 1. Configure your Reddit credentials
# -----------------------------------
REDDIT_CLIENT_ID = os.environ.get("REDDIT_CLIENT_ID")
REDDIT_CLIENT_SECRET = os.environ.get("REDDIT_CLIENT_SECRET")
REDDIT_USER_AGENT = os.environ.get("REDDIT_USER_AGENT")
MONGODB_URI = os.environ.get("MONGODB_URI")
MONGODB_NAME = os.environ.get("MONGODB_NAME")
# #reddit-notifications
SLACK_WEBHOOK_URL = os.environ.get("SLACK_WEBHOOK_URL")
# Slack incoming webhook URL (you can store it in .env as well)
# -----------------------------------
# 2. Create a PRAW Reddit instance
# -----------------------------------
reddit = praw.Reddit(
client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_CLIENT_SECRET,
user_agent=REDDIT_USER_AGENT
)
# Ensure we’re read-only if not authenticated with username/password
reddit.read_only = True
# -----------------------------------
# 3. Set up MongoDB connection
# -----------------------------------
mongo_client = pymongo.MongoClient(MONGODB_URI)
db = mongo_client[MONGODB_NAME] # Database name
collection = db["processed_submissions"] # Collection name
# -----------------------------------
# 4. Define your target subreddits
# -----------------------------------
target_subreddits = ["theoverload","techno", "festivals", "musicfestivals", "norway", "norge", "oslo"]
# -----------------------------------
# 5. Define keywords to look for
# -----------------------------------
keywords = ["mnmt", "monument"]
# -----------------------------------
# 6. Main logic to fetch new submissions and check keywords
# -----------------------------------
import os
import praw
import pymongo
import time
from dotenv import load_dotenv
import requests # <-- New import
load_dotenv() # Load variables from .env into os.environ
# -----------------------------------
# 1. Configure your Reddit credentials
# -----------------------------------
REDDIT_CLIENT_ID = os.environ.get("REDDIT_CLIENT_ID")
REDDIT_CLIENT_SECRET = os.environ.get("REDDIT_CLIENT_SECRET")
REDDIT_USER_AGENT = os.environ.get("REDDIT_USER_AGENT")
MONGODB_URI = os.environ.get("MONGODB_URI")
MONGODB_NAME = os.environ.get("MONGODB_NAME")
SLACK_WEBHOOK_URL = os.environ.get("SLACK_WEBHOOK_URL")
# Slack incoming webhook URL (you can store it in .env as well)
# -----------------------------------
# 2. Create a PRAW Reddit instance
# -----------------------------------
reddit = praw.Reddit(
client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_CLIENT_SECRET,
user_agent=REDDIT_USER_AGENT
)
# Ensure we’re read-only if not authenticated with username/password
reddit.read_only = True
# -----------------------------------
# 3. Set up MongoDB connection
# -----------------------------------
mongo_client = pymongo.MongoClient(MONGODB_URI)
db = mongo_client[MONGODB_NAME] # Database name
collection = db["processed_submissions"] # Collection name
# -----------------------------------
# 4. Define your target subreddits
# -----------------------------------
target_subreddits = ["techno", "festivals", "musicfestivals"]
# -----------------------------------
# 5. Define keywords to look for
# -----------------------------------
keywords = ["mnmt", "monument"]
# -----------------------------------
# 6. Main logic to fetch new submissions and check keywords
# -----------------------------------
def check_submissions():
"""
Fetch recent submissions from target subreddits,
checks for matching keywords,
prints thread info if matched and not yet processed,
stores them in MongoDB,
and sends Slack notification for each match.
"""
print(f"Checking subreddits: {target_subreddits} for keywords {keywords}...")
for subreddit_name in target_subreddits:
subreddit = reddit.subreddit(subreddit_name)
for submission in subreddit.new(limit=50):
submission_id = submission.id
# Check if we already processed this submission
if collection.find_one({"_id": submission_id}):
continue
# Prepare text to search
title_text = submission.title.lower() if submission.title else ""
selftext_text = submission.selftext.lower() if submission.selftext else ""
# Check for keywords
if any(keyword in title_text or keyword in selftext_text for keyword in keywords):
# Print to terminal
print("\n--- MATCH FOUND ---")
print(f"Subreddit: r/{subreddit_name}")
print(f"Title: {submission.title}")
print(f"URL: {submission.url}")
print(f"Reddit Link: https://reddit.com{submission.permalink}")
print(f"Author: {submission.author}")
print("-------------------\n")
# 1. Store in MongoDB
collection.insert_one({
"_id": submission_id,
"title": submission.title,
"url": submission.url,
"permalink": submission.permalink,
"subreddit": subreddit_name,
"author": str(submission.author),
"created_utc": submission.created_utc
})
# 2. Send Slack message
if SLACK_WEBHOOK_URL:
slack_message = (
f"*MATCH FOUND!*\n"
f"*Subreddit:* r/{subreddit_name}\n"
f"*Title:* {submission.title}\n"
f"*URL:* {submission.url}\n"
f"<https://reddit.com{submission.permalink}|Reddit Link>\n"
)
try:
response = requests.post(
SLACK_WEBHOOK_URL,
json={"text": slack_message},
timeout=10
)
# Optional: check Slack response
if response.status_code != 200:
print(f"Slack webhook failed: {response.text}")
except Exception as e:
print(f"Error sending Slack notification: {e}")
# -----------------------------------
# 7. Run once or schedule
# -----------------------------------
if __name__ == "__main__":
# Option A: Run it once
check_submissions()
# Option B: Run in a loop with sleep
# while True:
# check_submissions()
# time.sleep(43200)