-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_subscriber.py
125 lines (110 loc) · 4.41 KB
/
auto_subscriber.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
119
120
121
122
123
124
125
# oauth PRAW template by /u/The1RGood #
#==================================================Config stuff====================================================
import configparser
import time, praw
import webbrowser
import pymongo
import json
from flask import Flask, request
from threading import Thread
Config = configparser.ConfigParser()
Config.read('sn_info.cfg')
client = pymongo.MongoClient(Config.get('Mongo Info','conn_str'))
db = client[Config.get('Mongo Info','database')]
coll = db[Config.get('Mongo Info','collection')]
subs = {}
access_information = None
scope = "identity privatemessages read"
access_information = ''
scope = "identity privatemessages submit read" #SET THIS. SEE http://praw.readthedocs.org/en/latest/pages/oauth.html#oauth-scopes FOR DETAILS.
#==================================================End Config======================================================
#==================================================OAUTH APPROVAL==================================================
app = Flask(__name__)
CLIENT_ID = Config.get('Reddit Access','cid')
CLIENT_SECRET = Config.get('Reddit Access','csec')
REDIRECT_URI = 'http://127.0.0.1:65010/authorize_callback'
def kill():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
return "Shutting down..."
@app.route('/authorize_callback')
def authorized():
global access_information
state = request.args.get('state', '')
code = request.args.get('code', '')
access_information = r.get_access_information(code)
user = r.get_me()
text = 'Bot successfully started.'
kill()
return text
def refresh_access():
while(True):
time.sleep(1800)
r.refresh_access_information(access_information['refresh_token'])
r = praw.Reddit('OAuth2 SubNotifications Subscriber Bot')
r.set_oauth_app_info(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI)
webbrowser.open(r.get_authorize_url('DifferentUniqueKey',scope,True))
app.run(debug=False, port=65010)
#amt = Thread(target=refresh_access,args=())
#amt.daemon=True
#amt.start()
#==================================================END OAUTH APPROVAL-=============================================
def push_to_seen(m):
seen.insert(0,m)
if(len(seen)>100):
seen.pop()
print('Buffering old mail...')
seen = []
mail = r.get_messages(limit=50)
for m in mail:
push_to_seen(m.name)
print('Scanning.')
running = True
err_count = 0
while(running):
try:
r.refresh_access_information(access_information['refresh_token'],update_session=True)
mail = r.get_messages(limit=25)
for m in mail:
if(m.name not in seen):
push_to_seen(m.name)
if m.subject == "Action: Unsubscribe" and m.parent_id == None:
body = None
try:
body = json.loads(m.body)
target = ("/r/" + m.subreddit.display_name) if (m.subreddit != None) else (m.author.name)
print("Unsubscribing " + target + " from " + body['subreddit'])
coll.update({'sub':"/r/"+body['subreddit'].lower()},{'$unset' : {'filters.'+target:""}})
m.reply("You have been successfully unsubscribed.")
except:
print("Error parsing unsubscribe request.")
m.reply("There was an error processing your request. Please check the JSON syntax and try again.\n\nIf you cannot resolve the problem, please message /u/The1RGood.")
elif m.subject == "Action: Subscribe" and m.parent_id == None:
body = None
try:
body = json.loads(m.body)
filters = {}
inc_filters = {}
out_filters = {}
inc_filters['not_user'] = body['filter-users']
inc_filters['not_subreddit'] = body['filter-subreddits']
out_filters['karma'] = body['karma']
filters['inc_filters'] = inc_filters
filters['out_filters'] = out_filters
print("Filters made")
target = ("/r/" + m.subreddit.display_name) if (m.subreddit != None) else (m.author.name)
print("Subscribing " + target + " to " + body['subreddit'])
coll.find_one_and_update({'sub':"/r/"+body['subreddit'].lower()},{'$set': {'filters.'+target : filters}}, upsert=True)
m.reply("You have been successfully subscribed.")
except:
print("Error parsing subscribe request.")
m.reply("There was an error processing your request. Please check the JSON syntax and try again.\n\nIf you cannot resolve the problem, please message /u/The1RGood.")
except KeyboardInterrupt:
print("Break.")
break
except Exception as e:
err_count+=1
print(err_count)
print(e)