-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxGoldtBot.py
More file actions
executable file
·285 lines (274 loc) · 16 KB
/
MaxGoldtBot.py
File metadata and controls
executable file
·285 lines (274 loc) · 16 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
# Max Goldt Bot -- a Reddit bot that responds to bild.de links in comments
# with a quote from writer Max Goldt and an archive.is version of the linked
# bild.de article(s)
#
# Version: 0.4.0
# Author: Eric Haberstroh <eric@erixpage.de>
# License: MIT <https://opensource.org/licenses/MIT>
import archiveis
from urllib.parse import urlparse
import argparse
import configparser
import logging
import os
import praw
import prawcore.exceptions
import re
import sys
import time
class MaxGoldtBotCommentParser:
processed_comments = []
processed_comments_file = ""
config = None
reddit = None
subreddit = ""
sleeptime = 0
regex = '(?<!/)(http[s]?://(?:www|m).bild.de/(?:[-a-zA-Z0-9/_\.\?=,])+)'
def __init__(self, arguments):
self.config = configparser.ConfigParser()
logging.debug('[comments] Reading configuration file %s', arguments.config_file)
self.config.read(arguments.config_file)
logging.debug('[comments] Creating Reddit instance for username %s', self.config['MaxGoldtBot']['username'])
self.reddit = praw.Reddit(client_id=self.config['MaxGoldtBot']['client_id'],
client_secret=self.config['MaxGoldtBot']['client_secret'],
user_agent=self.config['MaxGoldtBot']['user_agent'],
username=self.config['MaxGoldtBot']['username'],
password=self.config['MaxGoldtBot']['password'])
self.sleeptime = arguments.sleeptime
self.subreddit = arguments.subreddit
if arguments.procfile:
self.processed_comments_file = arguments.procfile
else:
self.processed_comments_file = 'processed_comments_%s.txt' % arguments.subreddit
logging.debug('[comments] Storing processed comments in %s', self.processed_comments_file)
try:
with open(self.processed_comments_file) as file:
for line in file:
line = line.strip()
self.processed_comments.append(line)
logging.debug('[comments] Read %d processed comments in total', len(self.processed_comments))
except (FileNotFoundError, IOError):
logging.warning('[comments] File %s could not be read', self.processed_comments_file)
def run(self):
while True:
try:
for comment in self.reddit.subreddit(self.subreddit).stream.comments():
if comment.id not in self.processed_comments:
self.handle_comment(comment)
self.processed_comments.append(comment.id)
try:
with open(self.processed_comments_file, 'a') as file:
file.write(comment.id + '\n')
except IOError as e:
logging.error('[comments] IO error while writing to %s: %s', self.processed_comments_file, e)
if len(self.processed_comments) > 600:
logging.info('[comments] Pruning %s to 500 comments', self.processed_comments_file)
try:
with open(self.processed_comments_file, 'w') as file:
for comment in self.processed_comments[-500:]:
file.write(comment + '\n')
self.processed_comments = self.processed_comments[-500:]
except IOError as e:
logging.error('[comments] IO error while writing to %s: %s', self.processed_comments_file, e)
except (praw.exceptions.APIException,
praw.exceptions.ClientException,
prawcore.exceptions.RequestException) as e:
logging.warning('[comments] Got an exception: %s', e)
logging.warning('[comments] Will go to sleep for %d seconds', self.sleeptime)
time.sleep(self.sleeptime)
except KeyboardInterrupt:
logging.critical('[comments] Bot has been killed by keyboard interrupt. Exiting')
sys.exit(0)
def handle_comment(self, comment):
logging.debug('[comments] Processing new comment %s', comment.id)
urls = re.findall(self.regex, comment.body)
if urls:
logging.info('[comments] New comment %s with bild.de URLs found', comment.id)
archive_urls = []
bildplus = 0
for url in urls:
parsed_url = urlparse(url)
if parsed_url.path.startswith('/bild-plus/'):
logging.info('[comments] Skipping %s because it is probably a BILD+ link', url)
bildplus += 1
continue
logging.info('[comments] Capturing %s', url)
archive_url = archiveis.capture(url)
if archive_url:
archive_urls.append(archive_url)
logging.info('[comments] Captured: %s', archive_url)
else:
logging.warning('[comments] Got an empty archive.is URL back. Something is wrong')
if len(urls) != len(archive_urls) + bildplus:
logging.warning('[comments] Found %d bild.de URLs, but got only %d archive.is links', len(urls), len(archive_urls))
if archive_urls:
links = "\n- ".join(archive_urls)
body = ("> Diese Zeitung ist ein Organ der Niedertracht. Es ist falsch, sie zu lesen.\n"
"> Jemand, der zu dieser Zeitung beiträgt, ist gesellschaftlich absolut inakzeptabel.\n"
"> Es wäre verfehlt, zu einem ihrer Redakteure freundlich oder auch nur höflich zu sein.\n"
"> Man muß so unfreundlich zu ihnen sein, wie es das Gesetz gerade noch zuläßt.\n"
"> Es sind schlechte Menschen, die Falsches tun.\n\n"
"[Max Goldt](https://de.wikipedia.org/wiki/Max_Goldt), deutscher Schriftsteller\n\n"
"Du kannst diesen Artikel auf archive.is lesen, wenn du nicht auf bild.de gehen willst:\n\n- " \
+ links + \
"\n\n"
"----\n\n"
"^^[Info](https://www.reddit.com/r/MaxGoldtBot) | "
"[Autor](https://www.reddit.com/u/pille1842) | "
"[GitHub](https://github.com/pille1842/MaxGoldtBot) | "
"[Warum die Bild schlecht ist]"
"(http://www.bildblog.de/62600/warum-wir-gegen-die-bild-zeitung-kaempfen/)")
comment.reply(body)
logging.info('[comments] Replied to %s with %d links', comment.id, len(archive_urls))
else:
logging.warning('[comments] No reply to %s: %d bild.de links found, none archived', comment.id, len(urls))
else:
logging.debug('[comments] No relevant URLs found in %s', comment.id)
class MaxGoldtBotSubmissionParser:
processed_submissions = []
processed_submissions_file = ""
config = None
reddit = None
subreddit = ""
sleeptime = 0
regex = '(?<!/)(http[s]?://(?:www|m).bild.de/(?:[-a-zA-Z0-9/_\.\?=,])+)'
def __init__(self, arguments):
self.config = configparser.ConfigParser()
logging.debug('[submissions] Reading configuration file %s', arguments.config_file)
self.config.read(arguments.config_file)
logging.debug('[submissions] Creating Reddit instance for username %s', self.config['MaxGoldtBot']['username'])
self.reddit = praw.Reddit(client_id=self.config['MaxGoldtBot']['client_id'],
client_secret=self.config['MaxGoldtBot']['client_secret'],
user_agent=self.config['MaxGoldtBot']['user_agent'],
username=self.config['MaxGoldtBot']['username'],
password=self.config['MaxGoldtBot']['password'])
self.sleeptime = arguments.sleeptime
self.subreddit = arguments.subreddit
if arguments.prosfile:
self.processed_submissions_file = arguments.prosfile
else:
self.processed_submissions_file = 'processed_submissions_%s.txt' % arguments.subreddit
logging.debug('[submissions] Storing processed submissions in %s', self.processed_submissions_file)
try:
with open(self.processed_submissions_file) as file:
for line in file:
line = line.strip()
self.processed_submissions.append(line)
logging.debug('[submissions] Read %d processed submissions in total', len(self.processed_submissions))
except (FileNotFoundError, IOError):
logging.warning('[submissions] File %s could not be read', self.processed_submissions_file)
def run(self):
while True:
try:
for submission in self.reddit.subreddit(self.subreddit).stream.submissions():
if submission.id not in self.processed_submissions:
self.handle_submission(submission)
self.processed_submissions.append(submission.id)
try:
with open(self.processed_submissions_file, 'a') as file:
file.write(submission.id + '\n')
except IOError as e:
logging.error('[submissions] IO error while writing to %s: %s', self.processed_submissions_file, e)
if len(self.processed_submissions) > 600:
logging.info('[submissions] Pruning %s to 500 submissions', self.processed_submissions_file)
try:
with open(self.processed_submissions_file, 'w') as file:
for submission in self.processed_submissions[-500:]:
file.write(submission + '\n')
self.processed_submissions = self.processed_submissions[-500:]
except IOError as e:
logging.error('[submissions] IO error while writing to %s: %s', self.processed_submissions_file, e)
except (praw.exceptions.APIException,
praw.exceptions.ClientException,
prawcore.exceptions.RequestException) as e:
logging.warning('[submissions] Got an exception: %s', e)
logging.warning('[submissions] Will go to sleep for %d seconds', self.sleeptime)
time.sleep(self.sleeptime)
except KeyboardInterrupt:
logging.critical('[submissions] Bot has been killed by keyboard interrupt. Exiting')
sys.exit(0)
def handle_submission(self, submission):
logging.debug('[submissions] Processing new submission %s', submission.id)
if submission.selftext == '':
urls = re.findall(self.regex, submission.url)
else:
urls = re.findall(self.regex, submission.selftext)
if urls:
logging.info('[submissions] New submission %s with bild.de URLs found', submission.id)
archive_urls = []
bildplus = 0
for url in urls:
parsed_url = urlparse(url)
if parsed_url.path.startswith('/bild-plus/'):
logging.info('[submissions] Skipping %s because it is probably a BILD+ link', url)
bildplus += 1
continue
logging.info('[submissions] Capturing %s', url)
archive_url = archiveis.capture(url)
if archive_url:
archive_urls.append(archive_url)
logging.info('[submissions] Captured: %s', archive_url)
else:
logging.warning('[submissions] Got an empty archive.is URL back. Something is wrong')
if len(urls) != len(archive_urls) + bildplus:
logging.warning('[submissions] Found %d bild.de URLs, but got only %d archive.is links', len(urls), len(archive_urls))
if archive_urls:
links = "\n- ".join(archive_urls)
body = ("> Diese Zeitung ist ein Organ der Niedertracht. Es ist falsch, sie zu lesen.\n"
"> Jemand, der zu dieser Zeitung beiträgt, ist gesellschaftlich absolut inakzeptabel.\n"
"> Es wäre verfehlt, zu einem ihrer Redakteure freundlich oder auch nur höflich zu sein.\n"
"> Man muß so unfreundlich zu ihnen sein, wie es das Gesetz gerade noch zuläßt.\n"
"> Es sind schlechte Menschen, die Falsches tun.\n\n"
"[Max Goldt](https://de.wikipedia.org/wiki/Max_Goldt), deutscher Schriftsteller\n\n"
"Du kannst diesen Artikel auf archive.is lesen, wenn du nicht auf bild.de gehen willst:\n\n- " \
+ links + \
"\n\n"
"----\n\n"
"^^[Info](https://www.reddit.com/r/MaxGoldtBot) | "
"[Autor](https://www.reddit.com/u/pille1842) | "
"[GitHub](https://github.com/pille1842/MaxGoldtBot) | "
"[Warum die Bild schlecht ist]"
"(http://www.bildblog.de/62600/warum-wir-gegen-die-bild-zeitung-kaempfen/)")
submission.reply(body)
logging.info('[submissions] Replied to %s with %d links', submission.id, len(archive_urls))
else:
logging.warning('[submissions] No reply to %s: %d bild.de links found, none archived', submission.id, len(urls))
else:
logging.debug('[submissions] No relevant URLs found in %s', submission.id)
parser = argparse.ArgumentParser()
parser.add_argument('--config', action='store', dest='config_file', default='MaxGoldtBot.ini',
help='a configuration file to read from (default: MaxGoldtBot.ini)')
parser.add_argument('--logfile', action='store', dest='logfile',
help='a logfile to write to (default: stdout)')
parser.add_argument('--loglevel', action='store', dest='loglevel', default='WARNING',
help='a loglevel (default: WARNING)')
parser.add_argument('--procfile', action='store', dest='procfile',
help='a file to store processed comment IDs in')
parser.add_argument('--prosfile', action='store', dest='prosfile',
help='a file to store processed submission IDs in')
parser.add_argument('--sleeptime', action='store', dest='sleeptime', default=15 * 60, type=int,
help='number of seconds to sleep in case of an API exception')
parser.add_argument('subreddit', action='store',
help='subreddit to process comments from')
arguments = parser.parse_args()
numeric_level = getattr(logging, arguments.loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % arguments.loglevel)
logformat = '[%(asctime)s] %(levelname)s: %(message)s'
if arguments.logfile:
logging.basicConfig(level=numeric_level, format=logformat, filename=arguments.logfile)
logging.debug('Logging configuration: loglevel %s, logfile %s', arguments.loglevel, arguments.logfile)
else:
logging.basicConfig(level=numeric_level, format=logformat)
logging.debug('Logging configuration: loglevel %s, display output', arguments.loglevel)
logging.debug('Forking to let the child care about submissions')
newpid = os.fork()
if newpid == 0:
logging.info('Started handling submissions')
submission_parser = MaxGoldtBotSubmissionParser(arguments)
submission_parser.run()
else:
logging.info('Started handling comments (submissions given to PID %d)', newpid)
comment_parser = MaxGoldtBotCommentParser(arguments)
comment_parser.run()