/
samacharbot2.py
266 lines (203 loc) · 7.94 KB
/
samacharbot2.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# -*- coding: utf-8 -*-
__author__ = 'salmaan'
import praw
import smrzr
from altsummary import summary
import os
import find_other_news_sources
import itertools
from prawoauth2 import PrawOAuth2Mini
import blacklist
from time import sleep
import subreddits
from newspaper import Article
import message_templates as mt
def init():
r = praw.Reddit(user_agent="Samachar Bot for /r/india by /u/sallurocks")
scopes = {u'edit', u'submit', u'read', u'privatemessages', u'identity', u'history'}
oauth_helper = PrawOAuth2Mini(r, app_key=os.environ['app_key'],
app_secret=os.environ['app_secret'],
access_token=os.environ['access_token'],
refresh_token=os.environ['refresh_token'], scopes=scopes)
subreddit = r.get_subreddit("+".join(subreddits.subreddits))
return r, oauth_helper, subreddit
def postTracker():
"""File to keep track of posts already looked, gets refreshed if bot is restarted.
put pointer at start, then reads the file to string and then puts the pointer back at the end."""
fo = open("looked.txt", "a+")
position = fo.tell()
fo.seek(0, 0)
str1 = fo.read()
fo.seek(position)
return fo, str1
def addSubmission(file_handler, submission):
file_handler.write(submission.id + " ")
def isPosted(comments):
for comment in comments:
if str(comment.author) == 'samacharbot2':
return True
else:
return False
def summarizeSMRZR(url):
"""get post information and summary using Smrzr. Format properly."""
summ_article = smrzr.Summarizer(url, 4, 'default', 'newspaper')
keypoints = summ_article.keypoints
summ = summ_article.summary
message = "\n\n> * ".join(keypoints)
message = "> * " + message
message = message.replace("`", "")
message = message.replace("#", "\#")
return summ, message, summ_article.text
def processSummarization(file_handler, submission, method):
if method == 'smrzr':
addSubmission(file_handler, submission)
if isPosted(submission.comments):
print "already posted"
return
if method == 'smrzr':
summ, keypoints, article_text = summarizeSMRZR(submission.url)
print "Finished " + str(submission.id) + " with smrzr"
elif method == 'other':
summ, keypoints, article_text = summarizeOther(submission.url)
print "Finished " + str(submission.id) + " with other"
else:
return
if len(keypoints) < 150:
return
relevant_message = getRelevant(submission.url)
if finalChecks(article_text):
new_comment = summ + mt.br + keypoints.encode('ascii', 'ignore') + mt.br + relevant_message + mt.br + mt.endmsg
postComment(submission, new_comment)
def summarizeOther(url):
article = Article(url)
article.download()
article.parse()
text = article.text
keypoints = summary(text)
summ = article.title
keypoints = keypoints.replace("`", "")
keypoints = keypoints.replace("#", "\#")
return summ, keypoints, text
def finalChecks(text):
if text == 'Never miss a great news story!\n\nGet instant notifications from Economic Times\n\nAllowNot ' \
'now\n\nYou can switch off notifications anytime using browser settings.':
print "check failed"
return False
else:
return True
def postComment(submission, comment):
try:
submission.add_comment(comment)
except Exception as e:
print "Error posting comment\n"
print type(e)
print e.args
print e
print submission.id
print "\n"
def getRelevant(link):
relevant_list = find_other_news_sources.find_other_news_sources(url=link)
if relevant_list is None or len(relevant_list) == 0:
relevant_message = mt.relevant_message
else:
relevant_title = []
relevant_link = []
relevant_message = mt.relevant_message
# temp variable to see if new link isn't same as last one.
temp_blink = ""
for a_link in relevant_list:
relevant_title.append(a_link[0])
relevant_link.append(a_link[1])
for (a_link, b_link) in itertools.izip(relevant_title, relevant_link):
# if no title found, name the link.
if a_link is None:
a_link = "This"
if b_link is None or b_link == link:
continue
if temp_blink == b_link:
continue
try:
relevant_message = relevant_message + "\n\n" + "> * " + "[" + a_link + "]" + "(" + b_link + ")"
temp_blink = b_link
except Exception as e:
print e
return relevant_message
def checkDelete(r):
unread = r.get_unread(limit=None)
for msg in unread:
# only works if the word delete is posted as it is, without edit.
if msg.body.lower() == 'delete':
try:
# get comment id from message.
idd = msg.id
idd = 't1_' + idd
# find comment from id
comment = r.get_info(thing_id=idd)
# find parent comment i.e samachar bot comment
parentid = comment.parent_id
comment_parent = r.get_info(thing_id=parentid)
# get submission author through submission link id
sublink = comment_parent.link_id
author1 = r.get_info(thing_id=sublink)
# verify author of message is OP, then delete message and mark unread.
if str(msg.author.name) == str(author1.author):
comment_parent.delete()
msg.mark_as_read()
else:
msg.mark_as_read()
except Exception as e:
print "Error in delete"
print type(e)
print e.args
print e
print "\n"
msg.mark_as_read()
continue
def checkConditions(submission, ids):
return str(submission.domain) != str("self." + str(submission.subreddit)) \
and submission.domain not in blacklist.blocked and submission.id not in ids
def start():
r, oauth_helper, subreddit = init()
while True:
try:
submissions = subreddit.get_new(limit=50)
except praw.errors.HTTPException:
print "HTTP Exception"
sleep(300)
continue
nothing = True
for submission in submissions:
print "Working on - " + str(submission.id),
fo, ids = postTracker()
oauth_helper.refresh()
if int(submission.score) >= 0:
if checkConditions(submission, ids):
nothing = False
try:
processSummarization(fo, submission, 'smrzr')
except smrzr.ArticleExtractionFail:
print "Article Extraction Failed"
continue
# in case Smrzr fails to get summary, try another method
except AssertionError:
print "Assertion Error"
processSummarization(fo, submission, 'other')
except Exception as e:
print "Unknown ERROR"
print type(e)
print e.args
print e
print submission.id
print "\n"
continue
fo.close()
else:
print "Nothing to do, checking messages"
# if no more posts to summarize, go through unread messages and see if any posts to delete.
checkDelete(r)
continue
if nothing:
print "Nothing to do, sleeping for 1 minute"
sleep(60)
if __name__ == '__main__':
start()