-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
183 lines (142 loc) · 5.23 KB
/
run.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import json
import pickle
import csv
import praw
import sys
import argparse
from os.path import exists
from os import remove
from anytree import NodeMixin, RenderTree, PostOrderIter
from anytree.search import findall_by_attr
parser = argparse.ArgumentParser()
parser.add_argument(
"-d", "--delete", help="Delete all posts from pickle object", action="store_true"
)
if not exists("creds.json"):
print("Cannot find credentials, please run setup.py and try again")
sys.exit(1)
if not exists("info.json"):
print("Cannot find post info, please run setup.py and try again")
sys.exit(1)
with open("creds.json", "r") as f:
creds = json.load(f)
with open("info.json", "r") as f:
info = json.load(f)
reddit = praw.Reddit(
client_id=creds["client_id"],
client_secret=creds["client_secret"],
user_agent=creds["user_agent"],
redirect_uri=creds["redirect_uri"],
refresh_token=creds["refresh_token"],
)
subreddit = reddit.subreddit(info["subreddit"])
class ThreadPost(NodeMixin):
def __init__(self, name, parent=None):
super(ThreadPost, self).__init__()
self.name = name
self.parent = parent
self.link_str = None
self.post_obj = None
def parse_csv() -> ThreadPost:
root = ThreadPost("Root", parent=None)
with open(info["csv_file"], "r") as f:
reader = csv.reader(f)
curr_parent = root
curr_heads = []
next(reader)
for row in next(reader):
if row != "":
post = ThreadPost(row, parent=curr_parent)
curr_parent = post
curr_heads.append(post)
else:
curr_heads.append(None)
for row in reader:
for n, value in enumerate(row):
if n == 0 and value != "":
post = ThreadPost(value, parent=root)
curr_heads[n] = post
elif value != "":
post = ThreadPost(value, parent=curr_heads[n - 1])
curr_heads[n] = post
for pre, _, node in RenderTree(root):
print("%s%s" % (pre, node.name))
return root
def generate_link_str(node: ThreadPost) -> str:
"""Create thread children link, if root node is given, 2 consecutive children are linked for visual appeal"""
link_str = ""
if node.depth == 0:
for child in node.children:
link_str += f"##{child.name}\n"
for child2 in child.children:
link_str += f"[{child2.name}]({child2.post_obj.url})\n"
link_str += "\n"
else:
link_str += f"##{node.name}\n"
for child in node.children:
link_str += f"[{child.name}]({child.post_obj.url})\n"
return link_str
def create_post(node: ThreadPost, link_str=None) -> praw.models.Submission:
"""Create reddit post by accepting ThreadPost object and link_str"""
print(f"Creating {node.name} thread.")
if node.is_leaf:
title = info[str(node.depth)]["title"].replace("$NAME$", node.name)
selftext = info[str(node.depth)]["no_child_text"].replace("$NAME$", node.name)
else:
title = info[str(node.depth)]["title"].replace("$NAME$", node.name)
selftext = info[str(node.depth)]["description"].replace("$NAME$", node.name)
selftext += "\n\n" + link_str
selftext += "\n\n" + info[-1]["footer"]
node.link_str = link_str
submission = subreddit.submit(title, selftext=selftext)
if not (node.is_leaf or node.height == 1):
submission.mod.lock()
elif not info["need_root"] and node.depth == 0:
submission.hide()
return submission
def save_object(root):
"""Save root into pickle"""
print("Saving file..")
with open("reddit_tree.pickle", "wb") as f:
pickle.dump(root, f)
def bottomup_thread_maker(root):
"""Start making threads from height 0 and up"""
for node in root.leaves:
node.post_obj = create_post(node)
for i in range(root.height - 1, -1, -1):
for node in findall_by_attr(root, i, name="depth"):
if i == 1:
continue
elif not node.is_leaf:
node.post_obj = create_post(node, link_str=generate_link_str(node))
# update all posts with link to parent
for node in root.PostOrderIter():
if node.depth == 0:
continue
post_txt = node.post_obj.selftext
post_txt.replace("$PARENT$", node.parent.post_obj.url)
node.post_obj.edit(body=post_txt)
def main():
args = parser.parse_args()
if args.delete:
if not exists("reddit_tree.pickle"):
print(
"No saved threads found. Run this script without the delete argument first"
)
sys.exit(0)
else:
with open("reddit_tree.pickle", "rb") as f:
root = pickle.load(f)
for node in root.PostOrderIter():
node.post_obj.mod.approve()
node.post_obj.delete()
remove("reddit_tree.pickle")
print("All posts have been deleted.")
sys.exit(0)
root = parse_csv()
bottomup_thread_maker(root)
save_object(root)
print("Success. Link to master thread: \n")
print(root.post_obj.url)
if __name__ == "__main__":
main()