1- from modules .logger import setup_logger
21import logging
32from datetime import datetime
3+
44import praw
55import prawcore
6+ from pokestarfansloggingsetup import setup_logger
7+
68from modules .login import reddit
7- from modules .footer import footer
9+ from modules .table import starter
810
911logger = 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+
1173def 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
67109def action ():
68110 for sub_id in reddit .subreddit ('all' ).stream .submissions ():
69111 run_bot (sub_id )
70-
112+
71113
72114if __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 )
0 commit comments