-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_feed.py
62 lines (51 loc) · 1.57 KB
/
check_feed.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
import requests
import time
import sys
import pymysql.cursors
sys.path.append('../')
from config import *
def save_to_database(threadID, subreddit, utc):
connection = pymysql.connect(
host=HOST,
user=USER,
password=PASSWORD,
db=DB_NAME,
autocommit=True
)
try:
with connection.cursor() as cursor:
sql = 'INSERT INTO removed_threads (thread_id, subreddit, utc) VALUES (%s, %s, %s)'
cursor.execute(sql, (threadID, subreddit, str(utc)))
finally:
connection.close()
# Throwaway account
username = 'ComprehensiveMeeting'
appName = 'dickbutt'
url = 'https://www.reddit.com/r/undelete/new.json?limit=10'
headers = {'User-Agent': '{}/1.0 by {}'.format(appName, username)}
while True:
# Check every 5 min
time.sleep(60*5)
print('-- CHECKING --')
res = requests.get(url, headers=headers).json()
threads = res['data']['children']
threads = [thread['data'] for thread in threads]
for thread in threads:
if thread['link_flair_text'] != '[META]':
parts = thread['url'].split('/')
if len(parts) < 7:
print('SOMETHING WRONG WITH URL:', parts, flush=True)
continue
subreddit = parts[4].lower()
threadID = parts[6]
utc = int(thread['created_utc'])
print('{:<22} {:<8} {:<5}'.format(subreddit, threadID, utc), end='', flush=True)
try:
save_to_database(threadID, subreddit, utc)
except pymysql.err.IntegrityError as e:
if e.args[0] == 1062:
print(' - ALREADY IN DATABASE', end='')
else:
print()
print(e)
print()