1- import re
1+ import datetime
22import praw
33from login import reddit
44
55def action ():
66 for sub_id in reddit .subreddit ('all' ).stream .submissions ():
77 blockeduser = 0
8- item_written_to = 0
9- for item in reddit .inbox .unread (limit = None ):
8+ for item in reddit .inbox .messages (limit = 5 ):
109 if str (item .subject ) == 'Remove me from posts' :
1110 body = str (item .body )
12- yes = re .search ('\/u\/[a-zA-Z0-9]+' ,body )
13- username = yes .group (0 )
14- if str (item .author ) == username :
15- with open ('blockedusers.txt' , 'a' ) as file :
11+ with open ('blockusers.txt' , 'a+' ) as file :
12+ if str (item .author ) == body and body not in file .read ():
1613 newstring = username .replace (r'/u/' ,'' )
14+ newstring = newstring = '\n '
1715 file .write (newstring )
18- with open ('posts_written_to.txt' ,'r' ) as writefile :
19- for id_to_use in writefile .readlines ():
20- if item_written_to == 0 :
21- if sub_id == id_to_use :
22- item_written_to = 1
2316 duplicates = []
2417 submission = praw .models .Submission (reddit , id = sub_id )
2518 with open ('blockusers.txt' ,'r' ) as newfile :
2619 for line in newfile .readlines ():
27- if submission .author == line and blockeduser == 0 :
28- pass
29- else :
20+ line = line .strip ('\n ' )
21+ if str (submission .author ) == line :
3022 blockeduser = 1
31- if blockeduser == 0 and item_written_to == 0 :
23+ else :
24+ pass
25+ if blockeduser == 0 :
3226 for duplicate in submission .duplicates ():
3327 dup_sub = praw .models .Submission (reddit , id = duplicate )
34- duplicates .append ({'title' :str (dup_sub .title ), 'subreddit' :str (dup_sub .subreddit ), 'link' :str (
35- dup_sub .shortlink )})
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/{}' ).format (dup ['title' ], dup ['link' ], dup ['subreddit' ])
40- message = message + '\n \n ---- \n \n ^^I^^am^^a^^bot^^[FAQ](https://www.reddit.com/r/DuplicatesBot/wiki/index)-[Code](https://github.com/PokestarFan/DuplicateBot-[Bugs](https://www.reddit.com/r/DuplicatesBot/comments/6ypgmx/bugs_and_problems/)-[Suggestions](https://www.reddit.com/r/DuplicatesBot/comments/6ypg85/suggestion_for_duplicatesbot/)-[How to block](https://www.reddit.com/r/DuplicatesBot/wiki/index#wiki_block_bot_from_tagging_on_your_posts'
28+ if 'ImagesOf' not in str (dup_sub .subreddit ) and 'auto' not in str (dup_sub .subreddit ):
29+ time = dup_sub .created
30+ time = str (datetime .datetime .fromtimestamp (time ))
31+ author = str (dup_sub .author )
32+ if str (submission .author ) == author :
33+ author = author + '[author of both threads]'
34+ 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 })
35+ if len (duplicates ) > 0 :
36+ message = 'Here is a list of threads in other subreddits about the same content:\n '
37+ for dup in duplicates :
38+ message = str (message + '\n * [{}]({}) on /r/{} (created at {} by {})' ).format (dup ['title' ], dup ['link' ], dup ['subreddit' ], dup ['time' ], dup ['author' ])
39+ message = message + '\n \n ---- \n \n ^^I ^^am ^^a ^^bot ^^[FAQ](https://www.reddit.com/r/DuplicatesBot/wiki/index)-[Code](https://github.com/PokestarFan/DuplicateBot-[Bugs](https://www.reddit.com/r/DuplicatesBot/comments/6ypgmx/bugs_and_problems/)-[Suggestions](https://www.reddit.com/r/DuplicatesBot/comments/6ypg85/suggestion_for_duplicatesbot/)-[Block](https://www.reddit.com/r/DuplicatesBot/wiki/index#wiki_block_bot_from_tagging_on_your_posts)'
4140 try :
4241 submission .reply (message )
43- with open ('writes.log' , 'a' ) as logfile :
44- logfile .write ('Replied to submission id {}' .format (sub_id ))
45- print ('Replied to submission id {}' .format (sub_id ))
46- with open ('posts_written_to.txt' ,'a' ) as writefile2 :
47- writefile2 .write (sub_id )
48- except (UnboundLocalError ):
42+ message = ''
43+ except :
4944 pass
50-
45+ message = ''
46+
5147
5248if __name__ == '__main__' :
53- action ()
49+ while True :
50+ try :
51+ action ()
52+ except (KeyboardInterrupt ):
53+ raise KeyboardInterrupt
54+ except :
55+ pass
0 commit comments