-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_submission_status.py
75 lines (62 loc) · 2.32 KB
/
check_submission_status.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
# -*- coding: utf-8 -*-
import praw
import psycopg2
import datetime
from psycopg2 import OperationalError
DB=""
DB_USER=""
DB_PASS=""
DB_HOST="127.0.0.1"
DB_PORT="5432"
target_subreddit=""
reddit = praw.Reddit(
client_id="",
client_secret="",
user_agent="subreddit archiver bot",
check_for_async=False
)
## Create connection with postgreSQL
def create_connection(db_name, db_user, db_password, db_host, db_port):
connection = None
try:
connection = psycopg2.connect(
database=db_name,
user=db_user,
password=db_password,
host=db_host,
port=db_port,
)
print("Connection to PostgreSQL DB successful")
except OperationalError as e:
print(f"The error '{e}' occurred")
return connection
connection = create_connection(
DB, DB_USER, DB_PASS, DB_HOST, DB_PORT
)
extract_past_24h = '''SELECT id,selftext,is_self FROM PUBLIC.submissions WHERE datetime >= NOW() - '24 hours'::INTERVAL AND deleted = FALSE;'''
cursor = connection.cursor()
cursor.execute(extract_past_24h)
lot = cursor.fetchall()
print("Number of submissions to check: ", len(lot))
mark_deleted = '''UPDATE public.submissions SET "deleted" = True WHERE id = %s;'''
mark_edited = '''UPDATE public.submissions SET "edited" = True, edited_body = %s WHERE id = %s;'''
cursor = connection.cursor()
def main():
for i in range(0, len(lot)):
lot_length = len(lot)
submission = reddit.submission(lot[i][0])
if submission.author == None:
print("Submission was deleted. (Submission ID: %s)" % lot[i][0])
cursor.execute(mark_deleted, (lot[i][0],))
connection.commit()
print("Progress: %s / %s" % (i + 1, lot_length))
elif (lot[i][2] and submission.selftext != lot[i][1]):
print("Submission was edited. (Submission ID: %s)" % lot[i][0])
cursor.execute(mark_edited, (reddit.submission(lot[i][0]).selftext,
lot[i][0]))
connection.commit()
print("Progress: %s / %s" % (i + 1, lot_length))
else:
print("No changes were identified. (Submission ID: %s) " % lot[i][0])
print("Progress: %s / %s" % (i + 1, lot_length))
main()