This repository was archived by the owner on Apr 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathduplicate.py
75 lines (70 loc) · 3.91 KB
/
duplicate.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
from modules.logger import setup_logger
import logging
from datetime import datetime
import praw
import prawcore
from modules.login import reddit
from modules.footer import footer
logger = setup_logger('duplicates')
def action():
for sub_id in reddit.subreddit('all').stream.submissions():
try:
logging.debug('Starting submission {}'.format(sub_id))
blockeduser = 0
duplicates = []
submission = praw.models.Submission(reddit, id = sub_id)
with open('blockusers.txt','r') as newfile:
for line in newfile.readlines():
line = line.strip('\n')
if str(submission.author) == line or 'bot' in str(submission.author).lower():
blockeduser = 1
logger.debug('User {}\'s submission {} was blocked from posting'.format(str(submission.author),str(sub_id)))
else:
pass
if blockeduser == 0:
for duplicate in submission.duplicates():
dup_sub = praw.models.Submission(reddit, id = duplicate)
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():
time = dup_sub.created
time = str(datetime.fromtimestamp(time))
author = '/u/'+str(dup_sub.author)
if str(submission.author) == author:
author = author + ' [author of both threads]'
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)})
if len(duplicates) > 0:
message = 'Here is a list of threads in other subreddits about the same content:\n'
for dup in duplicates:
message = str(message + '\n * [{}]({}) on /r/{} with {} karma (created at {} by {})').format(dup['title'], dup['link'], dup['subreddit'], dup['karma'],dup['time'], dup['author'])
message = message + footer
try:
submission.reply(message)
logger.info('Message posted on {}'.format(sub_id))
logger.debug('Message: {}'.format(message))
message = ''
except(praw.exceptions.APIException, UnboundLocalError)as e:
logger.debug('Submission {} has been skipped due to missing text'.format(sub_id))
message = ''
except(prawcore.exceptions.Forbidden):
logger.debug('You are blocked on /r/{}'.format(str(submission.subreddit)))
message = ''
except(AssertionError):
logger.debug('Assertion Error occured! Printing message and traceback.')
logger.debug(message + str(len(message)), exc_info=True)
message = ''
except(KeyboardInterrupt):
raise KeyboardInterrupt
except:
logger.error('Error occured!', exc_info=True)
message = ''
except(KeyboardInterrupt):
raise KeyboardInterrupt
except:
logger.error('Error on submission {} occured.'.format(str(sub_id)), exc_info=True)
if __name__ == '__main__':
while True:
try:
action()
except(KeyboardInterrupt):
raise KeyboardInterrupt
except:
logger.critical('Error has occured when running main loop, please resolve asap', exc_info=True)