-
Notifications
You must be signed in to change notification settings - Fork 7
/
newpost.py
114 lines (95 loc) · 3.52 KB
/
newpost.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
import sys
import praw
import time
import requests
import json
CONFIG_FILE='config.json'
def handle_post(submission):
url = submission.shortlink
title = submission.title
sub = submission.subreddit.display_name
if config['keywords']['enabled']:
if any(x.lower() in title.lower() for x in config['keywords']['list']):
notify(sub, title, url)
else:
notify(sub, title, url)
def handle_modqueue(item):
url = 'https://reddit.com' + item.permalink
sub = item.subreddit.display_name
notify(sub, 'Modqueue', url)
def notify(subreddit, title, url):
if first: return
if config['discord']['enabled']:
notify_discord(subreddit, title, url)
if config['slack']['enabled']:
notify_slack(subreddit, title, url)
if config['reddit_pm']['enabled']:
notify_reddit(subreddit, title, url)
if config['telegram']['enabled']:
notify_telegram(subreddit, title, url)
if config['debug']:
print(subreddit + ' | ' + title + ' | ' + url)
def notify_discord(subreddit, title, url):
message = title + " | <" + url + ">"
payload = { 'content': message }
headers = { 'Content-Type': 'application/json', }
requests.post(config['discord']['webhook'], data=json.dumps(payload), headers=headers)
def notify_slack(subreddit, title, url):
message = title + " | " + url
payload = { 'text': message }
headers = { 'Content-Type': 'application/json', }
requests.post(config['slack']['webhook'], data=json.dumps(payload), headers=headers)
def notify_reddit(subreddit, title, url):
if title == 'Modqueue':
subject = 'New item in modqueue on /r/' + subreddit + '!'
else:
subject = 'New post on /r/' + subreddit + '!'
message = '[' + title + '](' + url + ')'
for user in config['reddit_pm']['users']:
r.redditor(user).message(subject, message)
def notify_telegram(subreddit, title, url):
message = '<b>[/r/{}]</b> {} - {}'.format(subreddit, title, url)
payload = {
'chat_id': config['telegram']['chat_id'],
'text': message,
'parse_mode': 'HTML'
}
requests.post("https://api.telegram.org/bot{}/sendMessage".format(config['telegram']['token']),
data=payload)
def start_streams():
modqueue_stream = (r.subreddit('mod').mod.stream.modqueue(pause_after=-1)
if config['modqueue'] else [])
submission_stream = (r.subreddit(subreddits).stream.submissions(pause_after=-1)
if config['new_posts'] else [])
return modqueue_stream, submission_stream
with open(CONFIG_FILE) as config_file:
config = json.load(config_file)
r = praw.Reddit(
user_agent = config['reddit']['user_agent'],
client_id = config['reddit']['client_id'],
client_secret = config['reddit']['client_secret'],
username = config['reddit']['username'],
password = config['reddit']['password']
)
first = True
subreddits = '+'.join(config['subreddits'])
(modqueue_stream, submission_stream) = start_streams()
while True:
try:
for item in modqueue_stream:
if item is None:
break
handle_modqueue(item)
for submission in submission_stream:
if submission is None:
break
handle_post(submission)
first = False
time.sleep(1)
except KeyboardInterrupt:
print('\n')
sys.exit(0)
except Exception as e:
print('Error:', e)
time.sleep(30)
(modqueue_stream, submission_stream) = start_streams()