-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
478 lines (415 loc) · 18.9 KB
/
main.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
"""
Reddit -> Lemmy Post Converter. Copies Reddit posts over to a Lemmy community.
Copyright (C) 2023 Jivan RamjiSingh
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import requests
import logging
import yaml
import json
import psycopg2
import time
import sqlite3
from pythorhead import Lemmy
from alive_progress import alive_bar
logging.basicConfig(filename='output/out.log', level=logging.ERROR, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def load_yaml(file: str):
"""Load yaml file
file -> string location of the file to load
"""
with open(file, 'r') as f:
data = yaml.safe_load(f)
return data
def lemmy_setup(config: dict):
"""Initialize and login to Lemmy
config -> config.yml dictionary
"""
lemmy = Lemmy(config['lemmy']['url'])
lemmy.log_in(config['credentials']['lemmy_user'], config['credentials']['lemmy_pass'])
return lemmy
def load_db(file: str):
"""Load sqlite3 db connection
file -> string location of the sqlite3 db
"""
# Create the DB connection and open cursor
db = sqlite3.connect(file)
sq_cursor = db.cursor()
# Check if the posts table is created, if not make it
try:
sq_cursor.execute("SELECT * FROM posts")
logging.debug(f"{file} table 'posts' exists")
except sqlite3.OperationalError:
sq_cursor.execute("CREATE TABLE posts(reddit_post_id text, lemmy_post_id text, post_score integer, post_hash text)")
db.commit()
logging.debug(f"{file} table 'posts' created")
# Check if the comments table is created, if not make it
try:
sq_cursor.execute("SELECT * FROM comments")
logging.debug(f"{file} table 'comments' exists")
except sqlite3.OperationalError:
sq_cursor.execute("CREATE TABLE comments(reddit_comment_id text, lemmy_comment_id text, reddit_post_id text, lemmy_post_id text, comment_score integer, comment_hash text)")
db.commit()
logging.debug(f"{file} table 'comments' created")
sq_cursor.close()
return db
def pg_setup(config: dict):
"""Initialize and connect to postgres
config -> config.yml dictionary
"""
pg = psycopg2.connect(
database = config['lemmy']['pg_db'],
host = config['lemmy']['pg_host'],
port = config['lemmy']['pg_port'],
user = config['credentials']['pg_user'],
password = config['credentials']['pg_pass']
)
return pg
def get_json(endpoint: str):
"""Conduct the Reddit API request for a post
endpoint -> the api endpoint to hit
"""
headers = {
'User-agent': 'RLC 0.1'
}
url = f"https://reddit.com{endpoint}.json?limit=10000"
response = requests.get(url, headers=headers)
return response.json()
def get_frontpage(subreddit: str):
"""Get the API response for a subreddit frontpage
subreddit -> subreddit to hit (i.e. /r/tifu/)
"""
headers = {
'User-agent': 'RLC 0.1'
}
url = f"https://reddit.com{subreddit}.json"
print(url)
response = requests.get(url, headers=headers)
return response.json()
def check_dupe(db: sqlite3.Connection, post: dict = {}, comment: dict = {}):
"""Checks sqlite3 db for a duplicate post/comment based on reddit post/comment id
Only post OR comment should be passed, not both at once
db -> sqlite3 db connection
post -> post data dict (defaults empty/false)
comment -> comment data dict (defaults empty/false)
"""
db_cursor = db.cursor()
row_list = []
if (post):
db_data = db_cursor.execute(f"SELECT reddit_post_id, lemmy_post_id, post_score FROM posts WHERE reddit_post_id='{post['id']}'").fetchall()
for row in db_data or []:
db_dict = {
'reddit_post_id': row[0],
'lemmy_post_id': row[1],
'post_score': row[2]
}
row_list.append(db_dict)
elif (comment):
db_data = db_cursor.execute(f"SELECT reddit_post_id, lemmy_post_id, lemmy_comment_id, reddit_comment_id, comment_score FROM comments WHERE reddit_post_id='{comment['reddit_post_id']}' AND reddit_comment_id='{comment['reddit_comment_id']}'").fetchall()
for row in db_data or []:
db_dict = {
'reddit_post_id': row[0],
'lemmy_post_id': row[1],
'lemmy_comment_id': row[2],
'reddit_comment_id': row[3],
'comment_score': row[4]
}
row_list.append(db_dict)
else:
logging.error(f"check_dupe called but no data was passed.")
db_cursor.close()
if (len(row_list) > 1):
logging.error(f"More than one result returned: {row_list}")
return row_list[0]
elif (len(row_list) == 1):
return row_list[0]
else:
return []
def save_entry(db: sqlite3.Connection, comment_data: dict = {}, post_data: dict = {}, comment: dict = {}, post: dict = {}):
"""Save post/comment information to sqlite3
db -> sqlite3 db connection
comment_data -> comment data from Lemmy
post_data -> post data from Lemmy
comment -> comment information parsed from Reddit API
post -> post information parsed from Reddit API
"""
db_cursor = db.cursor()
if (comment_data and comment):
db_cursor.execute(f"""
INSERT INTO comments (reddit_comment_id, lemmy_comment_id, reddit_post_id, lemmy_post_id, comment_score)
VALUES ('{comment['reddit_comment_id']}', '{comment['lemmy_comment_id']}', '{comment['reddit_post_id']}', '{comment['lemmy_post_id']}', {comment['comment_score']})
""")
pass
elif (post_data and post):
db_cursor.execute(f"""
INSERT INTO posts (reddit_post_id, lemmy_post_id, post_score)
VALUES ('{post['id']}', '{post_data['post_view']['post']['id']}', {post['score']})
""")
else:
logging.error('save_entry called incorrectly')
db.commit()
db_cursor.close()
def fix_comment_score(pg: psycopg2.extensions.connection, comment_data: dict, item: dict):
"""Edit comment data in postgres to match score on Reddit
pg -> postgres db connection
comment_data -> comment data returned by pythorhead on creation
item -> comment data from Reddit API
"""
score = item['data']['score']
if score == 1:
# If the score is 1, then it doesn't need to be updated on Lemmy
# By default all comments on Lemmy have a score of 1
logging.info(f"Comment {comment_data['comment_view']['comment']['id']} has a score of 1, skipping")
return
else:
query = f"UPDATE comment_aggregates SET score = {score} WHERE comment_id = {comment_data['comment_view']['comment']['id']};"
pg_cursor = pg.cursor()
pg_cursor.execute(query)
pg.commit()
pg_cursor.close()
logging.info(f"Fixed score for comment {comment_data['comment_view']['comment']['id']} to {score}")
def fix_post_score(pg: psycopg2.extensions.connection, post_data: dict, post: dict):
"""Edit post data in postgres to match score on Reddit
pg -> postgres db connection
post_data -> post data returned by pythorhead on creation
post -> post data from Reddit API
"""
score = post['score']
if score == 1:
# If the score is 1, then it doesn't need to be updated on Lemmy
# By default all comments on Lemmy have a score of 1
logging.info(f"Post {post_data['post_view']['post']['id']} has a score of 1, skipping")
else:
query = f"UPDATE post_aggregates SET score = {score} WHERE post_id = {post_data['post_view']['post']['id']};"
pg_cursor = pg.cursor()
pg_cursor.execute(query)
pg.commit()
pg_cursor.close()
logging.info(f"Fixed score for post {post_data['post_view']['post']['id']} to {score}")
pass
def parse_comments(pg: psycopg2.extensions.connection, lemmy: Lemmy, post_data: dict, data: dict, post: dict, db: sqlite3.Connection, parent_comment: dict = {}):
"""Parse through comments and create them on the Lemmy post
pg -> postgres db connection
lemmy -> Lemmy instance connection
post_data -> post data returned by pythorhead on creation
data -> comment data returned by Reddit API
post -> post information parsed from Reddit API
parent_comment -> parent comment information, if applicable (defaults empty/false)
"""
for item in data['data']['children']:
# Iterate over all the comments
if (item['kind'] == 'more'):
# This comment type is for unloaded comments, should be ignored
pass
else:
comment = {
'reddit_post_id': post['id'],
'reddit_comment_id': item['data']['id'],
'lemmy_post_id': post_data['post_view']['post']['id'],
'comment_score': item['data']['score']
}
comment_data = {}
if (check_dupe(db, comment=comment)):
logging.info(f"Duplicate comment found: {comment['reddit_comment_id']}")
else:
comment_made = False
# Copy over the comment, if it has a parent comment then the context should be preserved
if (parent_comment):
try:
comment_data = lemmy.comment.create(int(post_data['post_view']['post']['id']), item['data']['body'], parent_id=parent_comment['comment_view']['comment']['id'])
comment['lemmy_comment_id'] = comment_data['comment_view']['comment']['id']
comment_made = True
except:
logging.error(f"Could not push child comment {item['data']['id']}")
with open(f"output/errors/{item['data']['id']}.json", 'w') as f:
f.write(json.dumps(item, indent=4))
else:
try:
comment_data = lemmy.comment.create(int(post_data['post_view']['post']['id']), item['data']['body'])
comment['lemmy_comment_id'] = comment_data['comment_view']['comment']['id']
comment_made = True
except:
logging.error(f"Could not push comment {item['data']['id']}")
with open(f"output/errors/{item['data']['id']}.json", 'w') as f:
f.write(json.dumps(item, indent=4))
if (comment_made):
# Copy over the score of the comment
try:
fix_comment_score(pg, comment_data, item)
except:
logging.error(f"Could not fix comment score {item['data']['id']}")
try:
with open(f"output/errors/{item['data']['id']}_score.json", 'w') as f:
f.write(json.dumps(comment_data, indent=4))
with open(f"output/errors/{item['data']['id']}.json", 'w') as f:
f.write(json.dumps(item, indent=4))
except:
logging.critical(f"Comment has no comment_data {item['data']['id']}")
# Save the comment information to sqlite3
try:
save_entry(db, comment_data=comment_data, comment=comment)
except:
logging.error(f"Could not save comment information to sqlite3")
try:
with open(f"output/errors/{comment['lemmy_comment_id']}.json", 'w') as f:
f.write(json.dumps(comment, indent=4))
except:
logging.critical(f"Could not save comment error data")
# Loop through any replies to the comment to follow comment chains
if (item['data']['replies'] != ""):
try:
parse_comments(pg, lemmy, post_data, item['data']['replies'], post, db, comment_data)
except:
logging.error(f"Could not handle comment reply {item['data']['id']}")
try:
with open(f"output/errors/{item['data']['id']}_score.json", 'w') as f:
f.write(json.dumps(comment_data, indent=4))
with open(f"output/errors/{item['data']['id']}.json", 'w') as f:
f.write(json.dumps(item, indent=4))
except:
logging.critical(f"Comment has no comment_data {item['data']['id']}")
return True
def load_example_data():
"""Load example data for testing purposes
"""
with open('output/example.json', 'r') as f:
data = json.load(f)
return data
def copy_post(lemmy: Lemmy, pg: psycopg2.extensions.connection, permalink: str, db: sqlite3.Connection, comments: bool = True):
"""Copy post from Reddit to Lemmy
lemmy -> Lemmy instance connection
pg -> postgres db connection
permalink -> Reddit post permalink (i.e. /r/tifu/comments/xxx/xxx)
comments -> whether to copy comments, defaults True
"""
# Receive Reddit API response and process it
data = get_json(permalink)
post = {
'title': data[0]['data']['children'][0]['data']['title'],
'url': data[0]['data']['children'][0]['data']['url'],
'body': data[0]['data']['children'][0]['data']['selftext'],
'creator_id': data[0]['data']['children'][0]['data']['author'],
'subreddit': data[0]['data']['children'][0]['data']['subreddit'],
'nsfw': data[0]['data']['children'][0]['data']['over_18'],
'score': data[0]['data']['children'][0]['data']['score'],
'id': data[0]['data']['children'][0]['data']['id']
}
# Find the ID of matching community name on Lemmy
post['community_id'] = lemmy.community.get(name=post['subreddit'])['community_view']['community']['id']
# While loop is here to handle rate limits or other reasons for post copy failure
# Will attempt 5 times before skipping
attempts = 0
post_made = False
post_data = check_dupe(db, post=post)
if (post_data):
logging.info(f"Duplicate post found: {post['title']}")
post_data = {
'post_view': {
'post': {
"id": post_data['lemmy_post_id']
}
}
}
else:
while True:
try:
post_data = lemmy.post.create(post['community_id'], post['title'], post['url'], f"{post['body']} \n\n Originally Posted on r/{post['subreddit']} by u/{post['creator_id']}", post['nsfw'])
fix_post_score(pg, post_data, post)
post_made = True
break
except:
if (attempts == 5):
logging.critical(f'Post could not be copied, skipping')
logging.critical(post)
post_made = False
break
logging.warning(f'Something went wrong, retrying {attempts}')
time.sleep(30)
attempts += 1
if (post_made):
logging.info(f"Post Created: {post['title']}")
save_entry(db, post_data=post_data, post=post)
# Copy over comments if requested
if (comments):
comment_data = parse_comments(pg, lemmy, post_data, data[1], post, db)
def main():
"""Main function
Gets the data from Reddit
Sorts through what should and should not go to Lemmy
"""
# Load data and initialize connections
config = load_yaml('config.yml')
lemmy = lemmy_setup(config)
pg = pg_setup(config)
db = load_db("rlc.db")
# Loop through subreddits where we want comments
for sub in config['subreddits']:
posts = get_frontpage(f'/r/{sub}/')
try:
with alive_bar(len(posts['data']['children'])) as bar:
for post in posts['data']['children']:
if (post['data']['stickied'] == True):
# Skip sticky posts
pass
else:
start = time.time()
copy_post(lemmy, pg, post['data']['permalink'], db)
end = time.time()
# Want to make sure we stay under the Reddit rate limit
# 10 per minute
# Lemmy request times make this unnecessary, but is a precaution
if ((end - start) < 10):
time.sleep(10 - (end - start))
bar()
except:
try:
if (posts['reason'] == 'banned'):
# Some subs would fail because they have been banned
# Log the error and keep moving
logging.error(f"{sub} has been banned")
except:
with open(f'output/errors/{sub}.json', 'w') as f:
f.write(json.dumps(posts, indent=4))
time.sleep(120)
# Loop through subreddits where we only want pictures/links
for sub in config['po_subreddits']:
posts = get_frontpage(f'/r/{sub}/')
try:
with alive_bar(len(posts['data']['children'])) as bar:
for post in posts['data']['children']:
if (post['data']['stickied'] == True):
# Skip sticky posts
pass
else:
start = time.time()
copy_post(lemmy, pg, post['data']['permalink'], db, False)
end = time.time()
# Want to make sure we stay under the Reddit rate limit
# 10 per minute
# Lemmy request times make this unnecessary, but is a precaution
if ((end - start) < 10):
time.sleep(10 - (end - start))
bar()
except:
try:
if (posts['reason'] == 'banned'):
# Some subs would fail because they have been banned
# Log the error and keep moving
logging.error(f"{sub} has been banned")
except:
with open(f'output/errors/{sub}.json', 'w') as f:
f.write(json.dumps(posts, indent=4))
time.sleep(120)
db.commit()
db.close()
pg.close()
if __name__ == '__main__':
main()