Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit c7ad323

Browse files
author
PokestarFan
committed
Add
1 parent 5f8124c commit c7ad323

File tree

4 files changed

+148
-59
lines changed

4 files changed

+148
-59
lines changed

duplicate.py

+100-58
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,121 @@
1-
from modules.logger import setup_logger
21
import logging
32
from datetime import datetime
3+
44
import praw
55
import prawcore
6+
from pokestarfansloggingsetup import setup_logger
7+
68
from modules.login import reddit
7-
from modules.footer import footer
9+
from modules.table import starter
810

911
logger = setup_logger('duplicates')
1012

13+
14+
# noinspection PyBroadException
15+
def generate_and_reply(submission):
16+
footer = '\n\n----\n\n ^^I ^^am ^^a ^^bot ^^[FAQ](https://www.reddit.com/r/DuplicatesBot/wiki/index)-[' \
17+
'Code](https://github.com/PokestarFan/DuplicateBot)-[Bugs](' \
18+
'https://www.reddit.com/r/DuplicatesBot/comments/6ypgmx/bugs_and_problems/)-[Suggestions](' \
19+
'https://www.reddit.com/r/DuplicatesBot/comments/6ypg85/suggestion_for_duplicatesbot/)-[Block ' \
20+
'user (op only)' \
21+
'](' \
22+
'https://www.reddit.com/message/compose/?to=DuplicatesBotBlocker&subject=remove%20user&message' \
23+
'={user})-[Block from subreddit (mods only)](' \
24+
'https://www.reddit.com/message/compose/?to=DuplicatesBotBlocker&subject=remove%20subreddit' \
25+
'&message={sub})\n' \
26+
'\n^^Now ^^you ^^can ^^remove ^^the ^^comment ^^by ^^replying ^^delete! '.format(user=
27+
str(
28+
submission.author), sub=str(submission.subreddit))
29+
global message
30+
sub_id = submission.subreddit
31+
for dup_sub in submission.duplicates():
32+
duplicates = []
33+
time = dup_sub.created
34+
time = str(datetime.fromtimestamp(time))
35+
author = '/u/' + str(dup_sub.author)
36+
if str(submission.author) == author:
37+
author = author + ' [author of both threads]'
38+
duplicates.append(['[{}]({})'.format(str(dup_sub.title), 'https://www.reddit.com' + str(dup_sub.permalink)),
39+
str(dup_sub.subreddit), author, str(time), str(dup_sub.score)])
40+
if len(duplicates) > 0:
41+
message = 'Here is a list of threads in other subreddits about the same content:\n'
42+
for dup in duplicates:
43+
starter.add_row_with_list(dup)
44+
message += '\n' + starter.table
45+
message += '\n' + footer
46+
try:
47+
submission.reply(message)
48+
logger.info('Message posted on {}'.format(sub_id))
49+
logger.debug('Message: {}'.format(message))
50+
message = ''
51+
except praw.exceptions.APIException:
52+
logger.debug('Submission {} has been skipped due to missing text.'.format(sub_id))
53+
message = ''
54+
except prawcore.exceptions.Forbidden:
55+
logger.debug('You are blocked on /r/{}'.format(str(submission.subreddit)))
56+
message = ''
57+
except AssertionError:
58+
logger.debug('Assertion Error occured! Printing message and traceback.')
59+
logger.debug(message + str(len(message)), exc_info=True)
60+
message = ''
61+
except(KeyboardInterrupt, SystemExit):
62+
raise
63+
except Exception:
64+
logger.error('Error occurred!', exc_info=True)
65+
message = ''
66+
except:
67+
logger.critical(
68+
'Massive Error occurred! Not part of the Exception, KeyboardInterrupt or SystemExit exceptions. Fix ASAP.',
69+
exc_info=True)
70+
message = ''
71+
72+
1173
def run_bot(sub_id):
74+
global blocked_sub
1275
if True:
1376
try:
14-
logging.debug('Starting submission {}'.format(sub_id))
15-
blockeduser = 0
16-
duplicates = []
17-
submission = praw.models.Submission(reddit, id = sub_id)
18-
with open('blockusers.txt','r') as newfile:
19-
for line in newfile.readlines():
20-
line = line.strip('\n')
21-
if str(submission.author) == line or 'bot' in str(submission.author).lower():
22-
blockeduser = 1
23-
logger.debug('User {}\'s submission {} was blocked from posting'.format(str(submission.author),str(sub_id)))
24-
else:
25-
pass
26-
if blockeduser == 0:
27-
for duplicate in submission.duplicates():
28-
dup_sub = praw.models.Submission(reddit, id = duplicate)
29-
if 'imagesof' not in str(dup_sub.subreddit).lower() and 'auto' not in str(dup_sub.subreddit).lower() and 'bot' not in str(dup_sub.author).lower() and 'mistyfront' not in str(dup_sub.subreddit).lower() and 'unitedfederation' not in str(dup_sub.subreddit).lower():
30-
time = dup_sub.created
31-
time = str(datetime.fromtimestamp(time))
32-
author = '/u/'+str(dup_sub.author)
33-
if str(submission.author) == author:
34-
author = author + ' [author of both threads]'
35-
duplicates.append({'title':str(dup_sub.title), 'subreddit':str(dup_sub.subreddit), 'link':'https://www.reddit.com'+str(dup_sub.permalink), 'time':str(time), 'author':author, 'karma': str(dup_sub.score)})
36-
if len(duplicates) > 0:
37-
message = 'Here is a list of threads in other subreddits about the same content:\n'
38-
for dup in duplicates:
39-
message = str(message + '\n * [{}]({}) on /r/{} with {} karma (created at {} by {})').format(dup['title'], dup['link'], dup['subreddit'], dup['karma'],dup['time'], dup['author'])
40-
message = message + footer
41-
try:
42-
submission.reply(message)
43-
logger.info('Message posted on {}'.format(sub_id))
44-
logger.debug('Message: {}'.format(message))
45-
message = ''
46-
except(praw.exceptions.APIException, UnboundLocalError)as e:
47-
logger.debug('Submission {} has been skipped due to missing text'.format(sub_id))
48-
message = ''
49-
except(prawcore.exceptions.Forbidden):
50-
logger.debug('You are blocked on /r/{}'.format(str(submission.subreddit)))
51-
message = ''
52-
except(AssertionError):
53-
logger.debug('Assertion Error occured! Printing message and traceback.')
54-
logger.debug(message + str(len(message)), exc_info=True)
55-
message = ''
56-
except(KeyboardInterrupt):
57-
raise KeyboardInterrupt
58-
except:
59-
logger.error('Error occured!', exc_info=True)
60-
message = ''
61-
except(KeyboardInterrupt):
62-
raise KeyboardInterrupt
63-
except:
64-
logger.error('Error on submission {} occured.'.format(str(sub_id)), exc_info=True)
77+
logging.debug('Starting submission {}'.format(str(sub_id)))
78+
blocked_user = 0
79+
blocked_sub = 0
80+
submission = sub_id
81+
try:
82+
with open('blockusers.txt', 'r') as new_file:
83+
for line in new_file.readlines():
84+
line = line.strip('\n')
85+
if str(submission.author).lower() == line.lower() or 'bot' in str(submission.author).lower():
86+
blocked_user = 1
87+
break
88+
except FileNotFoundError:
89+
with open('blockusers.txt', 'w'):
90+
blocked_user = 0
91+
try:
92+
with open('blockedsubs.txt', 'r') as new_file:
93+
for line in new_file.readlines():
94+
line = line.strip('\n')
95+
if str(submission.subreddit).lower() == line.lower():
96+
blocked_sub = 1
97+
break
98+
except FileNotFoundError:
99+
with open('blockedsubs.txt', 'w'):
100+
blocked_sub = 0
101+
if blocked_user == 0 and blocked_sub == 0:
102+
generate_and_reply(sub_id)
103+
except(KeyboardInterrupt, SystemExit):
104+
raise
105+
except Exception:
106+
logger.error('Error on submission {} occurred.'.format(str(sub_id)), exc_info=True)
65107

66108

67109
def action():
68110
for sub_id in reddit.subreddit('all').stream.submissions():
69111
run_bot(sub_id)
70-
112+
71113

72114
if __name__ == '__main__':
73115
while True:
74116
try:
75117
action()
76-
except(KeyboardInterrupt):
77-
raise KeyboardInterrupt
78-
except:
79-
logger.critical('Error has occured when running main loop, please resolve asap', exc_info=True)
118+
except(KeyboardInterrupt, SystemExit):
119+
raise
120+
except Exception:
121+
logger.critical('Error has occured when running main loop, please resolve asap', exc_info=True)

entriesadder.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import praw
2+
from pokestarfansloggingsetup import setup_logger
3+
import logging
4+
from modules.entrylogin import reddit
5+
6+
logger = setup_logger('usersubblocker')
7+
8+
9+
def write_to_user_file(text):
10+
with open('blockusers.txt', 'a') as file:
11+
file.write(text)
12+
13+
14+
def write_to_sub_file(text):
15+
with open('blockedsubs.txt', 'a') as file:
16+
file.write(text)
17+
18+
19+
def strip_message(message):
20+
try:
21+
if message.subject == 'remove subreddit':
22+
subreddit = reddit.subreddit(message)
23+
mod = False
24+
for moderator in reddit.subreddit(subreddit).moderator():
25+
if str(message.author) == str(moderator):
26+
mod = True
27+
break
28+
if mod:
29+
write_to_sub_file(message.body)
30+
else:
31+
message.reply('You are not a moderator of the subreddit so your request has not been preformed.')
32+
elif message.subject == 'remove user':
33+
if str(message.author) == message.body:
34+
write_to_user_file(message.body)
35+
else:
36+
message.reply('You are not the OP of the submission so your request has not been preformed.')
37+
except Exception:
38+
logging.warning('', exc_info=True)
39+
40+
41+
def check_for_messages(reddit):
42+
for message in reddit.inbox.unread(mark_read=True):
43+
strip_message(message)
44+
reddit.inbox.mark_read(message)

modules/footer.py

-1
This file was deleted.

modules/table.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from markdowntable import Table as ta
2+
3+
starter = ta('Title')
4+
starter.all_columns('Subreddit','Author','Time','Karma')

0 commit comments

Comments
 (0)