-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment_reposter.py
More file actions
139 lines (103 loc) · 4.09 KB
/
comment_reposter.py
File metadata and controls
139 lines (103 loc) · 4.09 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
import praw
import configparser
import time
config = configparser.ConfigParser()
config.read('config.ini')
reddit = praw.Reddit(**config['Auth'])
def repost(comment):
# Build comment
user = f'/u/{comment.author.name}'
parent = comment.parent()
if isinstance(parent, praw.models.Comment):
parent_link_template = config['Options']['parent_link_template']
parent = parent_link_template.format(link=parent.permalink)
else:
parent = config['Options']['parent_none_template']
body = comment.body
repost_template = config['Options']['repost_template']
message = repost_template.format(user=user, parent=parent, body=body)
# Submit
source_corner = select_source_corner(comment)
if source_corner is None:
print('ERROR: Could not find Source Corner for comment')
return None
print(f'Reposting comment {comment.id}:')
print(f' as a reply to Source Corner ({source_corner.id})')
print('### Message Start ###')
print(message)
print('### Message end ###')
return source_corner.reply(message)
def select_source_corner(comment):
# Get the discussion post and check is episode discussion
submission = comment.submission
print(f'Selecting the Source Corner for submission {submission.id}')
if submission.author.name != config['Options']['episode_bot_account']:
print(' ! Unexpected submission author')
return None
# Get the top comment and check it is the Source Corner
top_comment = submission.comments[0]
if not top_comment.stickied:
print(' ! Top comment is not stickied')
return None
if top_comment.author.name != config['Options']['sc_bot_account']:
print(' ! Unexpected top comment author')
return None
return top_comment
def get_all_sc_removals(modlog, last_timestamp):
sc_removals = list()
for action in modlog:
if action.created_utc < last_timestamp:
break
if action.action != 'distinguish':
continue
if is_sc_removal(action):
url = 'https://www.reddit.com' + action.target_permalink
action_comment = reddit.comment(url=url)
sc_removals.append(action_comment.parent())
print(f'Collected {len(sc_removals)} source corner removals')
return sc_removals
def is_sc_removal(action):
# SC removals are detected via the removal reason
if action.action != 'distinguish':
return False
# Do not pick the source corner itself
if action.mod.name == config['Options']['sc_bot_account']:
return False
body = action.target_body.lower()
# Removal message must mention the source corner
if not ('source corner' in body or 'source material' in body):
return False
# If the comment was removed for spoilers, don't repost it
if 'spoiler' in body:
return False
# Make sure the post is an episode discussion post
url = 'https://www.reddit.com' + action.target_permalink
action_comment = reddit.comment(url=url)
submission = action_comment.submission
if submission.author.name != config['Options']['episode_bot_account']:
return False
return True
def scan_modlog_once(subreddit, last_timestamp):
sc_removals = get_all_sc_removals(subreddit.mod.log(), last_timestamp)
for removal in sc_removals:
try:
repost(removal)
except Exception as e:
print(f'ERROR: Could not repost {removal.id}')
print(f' {type(e).__name__}: {str(e)}')
print(f' Link: {removal.permalink}')
return len(sc_removals) > 0
def scan_modlog_loop(subreddit):
last_timestamp = time.time()
sleep_time = int(config['Options']['sleep_time'])
active = True
while True:
#print(f'Sleeping for {sleep_time} seconds')
time.sleep(sleep_time)
print('Scanning modlog...')
active = scan_modlog_once(subreddit, last_timestamp)
last_timestamp = time.time()
#print('Done')
if __name__ == '__main__':
subreddit = reddit.subreddit(config['Options']['subreddit'])
scan_modlog_loop(subreddit)