-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmongodb_local.py
287 lines (230 loc) · 8.65 KB
/
mongodb_local.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from pymongo import MongoClient
from pandas import DataFrame
from config import (
CONNECTION_STRING,
DB_NAME,
DEFAULT_SUBREDDIT,
SUBREDDITS,
IGNORE_SENT_TO_NOTION,
)
def get_database_client(connection_string=CONNECTION_STRING, db_name=DB_NAME):
client = MongoClient(connection_string)
return client[db_name]
def post_in_db(
post_title,
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None, # I think passing the database makes it go a little faster?
):
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# Check to ensure post is unique in DB, using title as key
query = {"title": post_title}
if DataFrame(database.find(query)).empty:
return False
else:
return True
def post_id_in_db(
post_id,
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None, # I think passing the database makes it go a little faster?
):
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# Check to ensure post is unique in DB, using post_id as key
query = {"post_id": post_id}
if DataFrame(database.find(query)).empty:
return False
else:
return True
def add_post_to_db(
post,
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None,
):
# If DB connection wasn't passed, make a new one
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# Add post to DB if title isn't found in there
if not post_in_db(post["title"], subreddit, connection_string, db_name):
print(f"Adding {post['title']} to MongoDB...")
database.insert_one(post)
else:
print(f"{post['title']} is already in MongoDB, skipping...")
def add_tags_to_post(
post,
tags,
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None,
):
# If DB connection wasn't passed, make a new one
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# Update any matching items in the query (should only be 1)
query = {"title": post["title"]}
post_df = DataFrame(database.find(query))
for tag in tags:
# Add a tag if it is not already in the tags array, else keep it the same (to prevent duplicate tags)
post_df["tags"] = post_df["tags"].apply(
lambda tags: tags + [tag] if tag not in tags else tags
)
# Update any matching items in the query (should only be 1)
for index, row in post_df.iterrows():
query = {"_id": row["_id"]}
new = {"$set": {"tags": row["tags"]}}
database.update_many(query, new)
# print(f"Tag {tag} added to {post_df['title']}")
def reset_post_tags(
post,
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None,
confirm_all=False,
):
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# confirm_all is if you seriously wanna nuke the tags from the whole database
# Don't do this, it'll be the most expensive mistake you've made all week
# Because tagging costs $$$
if confirm_all == False:
confirm = input(
f"You are about to reset tags for {post['title']}. This process cannot be undone.\n \
Type RESET TAGS to continue: "
)
while confirm != "RESET TAGS":
confirm = input(
"Incorrect input.\n \
Type RESET TAGS to continue: "
)
# Update any matching items in the query (should only be 1)
query = {"title": post["title"]}
post_df = DataFrame(database.find(query))
for index, row in post_df.iterrows():
query = {"_id": row["_id"]}
new = {"$set": {"tags": []}}
database.update_many(query, new)
# print(f"Tags removed from {post_df['title']}")
def reset_sent_to_notion(
subreddits=SUBREDDITS,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None,
):
# Resetting the sent_to_notion flags to send to a clean Notion database
# The rebuild takes a very long time (full LegendLore takes ~2 days to rebuild)
confirm = input(
"You are about to set sent_to_notion to False for all MongoDB entries. This will cause the database to lose state with Notion, and require a full Notion rebuild (3+ days).\n \
Type RESET NOTION to continue: "
)
while confirm != "RESET NOTION":
confirm = input(
"Incorrect input.\n \
Type RESET NOTION to continue: "
)
# Reset the flag in Mongo for all posts
for subreddit in subreddits:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
query = {}
new = {"$set": {"sent_to_notion": False}}
database_client[subreddit].update_many(query, new)
def set_sent_to_notion(
post,
sent=True,
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None,
):
if not IGNORE_SENT_TO_NOTION:
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# Get post from DB
query = {"title": post["title"]}
post_df = DataFrame(database.find(query))
# Update any matching items in the query (should only be 1)
for index, row in post_df.iterrows():
query = {"_id": row["_id"]}
new = {"$set": {"sent_to_notion": sent}}
database.update_many(query, new)
database_client["all"].update_many(query, new)
# print(f"sent_to_notion {sent} added to {post_df['title']}")
def update_post_score(
post,
sent=True,
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None,
):
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# Get post from DB
query = {"title": post["title"]}
post_df = DataFrame(database.find(query))
# Update anything matching those keys in the query
for index, row in post_df.iterrows():
query = {"_id": row["_id"]}
new = {"$set": {"score": post["score"]}}
database.update_many(query, new)
print(f"{post['title']} updated score from {row['score']} to {post['score']}")
# If score updated, return so we know to send it to Notion later
if row["score"] != post["score"]:
return post["title"]
def get_post_from_db(
post_title,
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None,
):
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# Get post from DB
query = {"title": post_title}
return DataFrame(database.find(query))
def get_all_posts_from_db(
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None,
):
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# Adding this filter to remove things with a score of 0 - they are usually spam or low-effort, downvoted posts (not maps)
# Necessary to filter out dirty images, even if we miss a couple maps
return DataFrame(database.find({"score": {"$gte": 1}}))
def get_untagged_posts_from_db(
subreddit=DEFAULT_SUBREDDIT,
connection_string=CONNECTION_STRING,
db_name=DB_NAME,
database=None,
):
if database == None:
database_client = get_database_client(connection_string, db_name)
database = database_client[subreddit]
# Adding this gte 1 filter to remove things with a score of 0 - they are usually spam or low-effort, downvoted posts (not maps)
# Necessary to filter out dirty images, even if we miss a couple maps
# Also get all things tagged Untagged
return DataFrame(
database.find(
{"score": {"$gte": 1}, "tags": {"$elemMatch": {"name": "Untagged"}}}
)
)