-
Notifications
You must be signed in to change notification settings - Fork 0
/
Syntaxautobot.py
executable file
·242 lines (217 loc) · 8.87 KB
/
Syntaxautobot.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python
""""
[SyntaxBot] Python documentation bot <auto> for Reddit by /u/num8lock
version: v.0.9
Notes: praw4 (see requirements.txt)
[Heroku] uses env variables for all authentication credentials
Acknowledgment:
Codes based on many reddit bot creators /u/redditdev
and helps from /r/learnpython.
Thanks to:
- u/w1282 for reminding what function in programming function means
- u/bboe for authoring praw and making it easier to use reddit API
"""
import os
import re
import time
from datetime import datetime
import logging, logging.config
import praw
import ast
from sqlalchemy import create_engine, exists
from sqlalchemy.orm import sessionmaker
from docdb import Library as libdb
from docdb import RedditActivity as reddb
''' CONFIGS '''
# Retrieve (Heroku) env private variables
ua = 'Python Syntax help bot <auto> for reddit v.1 (by /u/num8lock)'
appid = os.getenv('syntaxbot_app_id')
secret = os.getenv('syntaxbot_app_secret')
botlogin = os.getenv('syntaxbot_username')
passwd = os.getenv('syntaxbot_password')
db_config = os.getenv('DATABASE_URL')
# Reddit related variables
baseurl = 'docs.python.org'
sub_name = 'learnpython'
# regex pattern for capturing definition url. Need to have everything
# captured between the identifiers
urlpattern = re.compile(r"""(?P<version>[32]/)(?P<topic>\w+/)
(?P<page>[\w\.]+\.html)(?P<syntax>[\?#\w=\.]+)""", re.I | re.X)
def check_replied(comment):
"""check if comment is already saved in profile"""
log.debug('Checking if replied... %s', comment)
saved_comments = r.user.me().saved()
for saved in saved_comments:
log.debug('Match post? saved: %s, %s', saved, comment)
if saved == comment.id:
log.info('Found as saved: %s, %s', saved, comment)
# save data to db
return True
else:
log.info('This saved not matching comment. %s, %s', saved, comment)
continue
# not saved yet
return False
def contain_url(comment):
"""Searching valid url in comment, if found, return the regex pattern match,
if not return False"""
# to find if submision or comment : stackoverflow.com/a/17431054/6882768
if not hasattr(comment, 'body'):
log.error('A submission. Searching selftext %s', comment.id)
found = re.search(urlpattern, comment.selftext)
log.debug('Contains valid URL? %s : %s', found, comment.selftext)
else:
log.debug('Comment in submission: %s', comment.submission)
found = re.search(urlpattern, comment.body)
log.debug('Contains valid URL? %s : %s', found, comment.body)
if found is None:
log.error('Error: cannot find url in %s', comment)
return False
_url = '{0}'.format( found.group(0) )
log.debug('Contains valid url: %s', found.groupdict())
return found
def querydb(data):
"""Get query definitions from libdb database"""
if not data:
log.error('querydb: No url to query.')
return None
log.info('Start querying db for %s', data)
_strip = re.compile(r'\?highlight=\w+\#|\#')
wordstrip = re.search(_strip, data.group(4))
if wordstrip is not None:
_syntax = data.group(4).replace(wordstrip.group(0), '')
log.debug('Stripped: %s', _syntax)
else:
_syntax = data.group(4)
_version = data.group(1).rstrip('/')
_topic = data.group(2).rstrip('/')
_module = data.group(3).rstrip('.html')
log.debug("Getting definition from db: %s", [_version, _module, _syntax])
# DB queries
# Data needed to process a reply:
columns = [ libdb.id, libdb.version_id, libdb.module, libdb.keywords,
libdb.header, libdb.body, libdb.footer, libdb.url ]
log.info('> Query check: Version: `%s`. Module: `%s`, Keyword: `%s`',
_version, _module, _syntax)
# check if keyword exists
check = session.query(exists().where(
libdb.module == _module).where(
libdb.keywords.contains(_syntax))).scalar()
log.debug('> Exists? check: %s.', check)
if check:
log.info('Starting query')
_query = session.query(*columns).filter(
(libdb.module == _module) &
(libdb.keywords.contains(_syntax))
).group_by(libdb.module, libdb.id).order_by(libdb.id).first()
log.info('Library returns: id: %s, vers: %s, syntax: %s',
_query.id, _query.version_id, _query.keywords)
return _query
else:
# since only version 3 stored in Heroku postgres lets replace _version
url_full = 'https://{0}/{1}/{2}/{3}.html#{4}'.format(
baseurl, '3', _topic, _module, _syntax)
log.error('Cannot found %s for constructed url %s', _syntax, url_full)
return None
def format_response(data, comment):
"""Format bot reply"""
if data is None:
return False
botreply = """Hi, I'm a bot being developed to get Python syntax
definition. I noticed you included a link to this syntax in your
post, so here's the entry from Python 3 official documentation:
\n\n{0} \n {1} \n {2}""".format(data.header, data.body, data.footer)
return botreply
def reply(comment):
"""Reply user comment"""
log.info('Start replying...{}'.format( {comment.id:
[datetime.utcfromtimestamp(comment.created_utc), comment.author.name]}))
response_data = querydb(contain_url(comment))
# pass comment too for logging purpose
bot_response = format_response(response_data, comment)
log.debug('Reply message: %s', bot_response)
if bot_response:
comment.reply(bot_response)
comment.save(category='comment_replied')
justnow = datetime.utcnow()
log.info('Pause: 5secs.')
time.sleep(5)
log.info('Checking if reply posted:...')
if check_replied(comment):
log.info('Replied and saved: %s', comment.id)
else:
log.error('Comment %s not saved at %s', comment.id, justnow)
else:
log.info('Nothing to reply for %s', comment.id)
def scan(subreddit, sort, time, limit):
''' Search for the queries in the sub using reddit search, time filtered
'''
search_result = r.subreddit(subreddit).search('{0}'.format(
baseurl), sort=sort, time_filter=time, limit=limit)
log.debug('Search result: {}'.format(search_result.__dict__))
if search_result is None:
log.info('No matching result.')
return None
for thread in search_result:
''' get OP thread / submission to iterate comment/replies '''
log.info('Iterating threads in search result : %s', thread)
# iterate every comments and their replies
submission = r.submission(id=thread)
submission.comments.replace_more(limit=0)
log.info('Processing comment tree: {} [{}]: {}'.format(
submission, submission.author, submission.comments.list() ))
# check OP
op_replied = check_replied(submission)
if op_replied:
log.info('Skipping submission %s: replied', submission)
elif not op_replied:
reply(submission)
# check comment forest
for comment in submission.comments.list():
# skip own & replied comment
if comment.author == botlogin:
log.info('Skipping own comment: %s', comment)
continue
elif check_replied(comment):
log.info('Skipping comment %s: replied', comment)
continue
# skip non-query comment
elif not contain_url(comment):
log.info('Skipping comment %s: no url found', comment)
continue
else:
reply(comment)
def whatsub_doc(subreddit):
"""Main bot activities & limit rate requests to oauth.reddit.com"""
log.info('Whatsub, doc?')
sort = 'new'
time = 'week'
limit = 100
scan(subreddit, sort, time, limit)
def login():
"""praw4 OAuth2 login procedure"""
''' praw4 only needs the first 3 for read-only mode '''
log.info('Logging started')
r = praw.Reddit(user_agent=ua, client_id=appid, client_secret=secret,
username=botlogin,
password=passwd
)
''' log connection '''
log.info('Connected. Starting bot activity')
return r
if __name__ == '__main__':
log = logging.getLogger(__name__)
logging.config.dictConfig(ast.literal_eval(os.getenv('WHATSUPDOC_CFG')))
engine = create_engine(db_config, echo=True)
Session = sessionmaker(bind=engine)
session = Session()
''' capture exceptions '''
try:
r = login()
whatsub_doc(sub_name)
except ConnectionError as no_connection:
log.error(no_connection, exc_info=True)
time.sleep(100)
log.info('Reconnecting in 10secs...')
r = login()
whatsub_doc(sub_name)