-
Notifications
You must be signed in to change notification settings - Fork 1
/
redditbot.py
102 lines (79 loc) · 3.65 KB
/
redditbot.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
# Derived from https://github.com/voussoir/reddit/commit/bf3903249fe2063d174268511cb5d76b590bc723#diff-1bae6b687c5d1e9ab80ba0bfe3ebd96a
# /u/GoldenSights, /u/andytuba
import praw
#import sqlite3
import time
import traceback
import os
import sys
from logger import VerboseLogger
class RedditBot(object): #, VerboseLogger):
# Bot OAuth config
# https://www.reddit.com/comments/3cm1p8/how_to_make_your_bot_use_oauth2/
useragent = 'generic-script-by-u-andytuba'
app_id = ''
app_secret = ''
app_uri = 'https://localhost/auth/'
#https://www.reddit.com/r/GoldTesting/comments/3chbrm/all_oauth_scopes/
app_scopes = 'account creddits edit flair history identity livemanage modconfig modcontributors modflair modlog modothers modposts modself modwiki mysubreddits privatemessages read report save submit subscribe vote wikiedit wikiread'
# User-specific OAuth data
app_refresh_token = None
# Bot config
loops = -1
wait = 60
r = None #praw
attr_env_mapping = {
'app_secret': 'REDDIT_APP_SECRET',
'app_uri': 'REDDIT_APP_URI',
'app_refresh_token': 'REDDIT_APP_REFRESH_TOKEN',
'loops': 'REDDIT_APP_LOOPS', # The number of times the bot should run before quitting. -1 for infinite, 1 for once, 2 for twice, etc. Runs at least once
'wait': 'REDDIT_APP_WAIT', # The number of seconds between each cycle. The bot is completely inactive during this time.
}
def set_attr_from_env(self):
for attr, env in self.attr_env_mapping.iteritems():
value = os.environ.get(env)
if value is not None:
print('Set self.%s = environ["%s"]' % (attr, env)) #log_debug
setattr(self, attr, value)
def main(self):
raise NotImplementedError
def __str__(self):
return '<RedditBot ua="%s", id="%s", scopes="%s">' % (self.useragent, self.app_id, self.app_scopes)
def __init__(self):
self.set_attr_from_env()
def run(self):
self.r = self.authenticate()
self.loop()
def authenticate(self):
print('Config: %s' % self) #log_debug
#sql = sqlite3.connect('filename.db')
#cur = sql.cursor()
#cur.execute('CREATE TABLE IF NOT EXISTS tablename(column TEXT)')
print('Logging in %s (%s)' % (self.useragent, self.app_id)) #self.log_debug
r = praw.Reddit(self.useragent)
r.set_oauth_app_info(client_id=self.app_id, client_secret=self.app_secret, redirect_uri=self.app_uri)
if self.app_refresh_token is None:
authorize_url = r.get_authorize_url('...', self.app_scopes, True)
print ('!!! Visit %s\n!!! and copy that code here: ' % authorize_url)
self.app_account_code = sys.stdin.readline().strip()
access_information = r.get_access_information(self.app_account_code)
self.app_refresh_token = access_information['refresh_token']
print("!!! To avoid this when invoking this script in the future,\n export %s=\'%s\'" % (self.attr_env_mapping['app_refresh_token'], self.app_refresh_token))
r.refresh_access_information(self.app_refresh_token)
return r
def loop(self):
while True:
print('Running main') #self.log_debug
try:
self.main(self.r)
except Exception as e:
#if self.verbosity >= self.VERBOSITY.WARNING
traceback.print_exc()
if self.loops == 0:
break
self.loops -= 1
time.sleep(sleep.wait)
print('Running again in %d seconds\n' % sleep.wait) #self.log_info
if __name__ == '__main__':
bot = RedditBot()
bot.run()