-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (44 loc) · 1.53 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
import os
import sqlite3
import praw
from dotenv import load_dotenv
from praw.models.reddit import submission, comment
load_dotenv()
sql = open('init.sql', 'r').read()
db = sqlite3.connect('saved.db')
cursor = db.cursor()
cursor.executescript(sql)
reddit = praw.Reddit(client_id=os.getenv("REDDIT_CLIENT_ID"),
client_secret=os.getenv("REDDIT_CLIENT_SECRET"),
password=os.getenv("REDDIT_PASSWORD"),
user_agent="reddit-save-saver",
username=os.getenv("REDDIT_USERNAME"))
for item in reddit.user.me().saved(limit=None):
print(item.id)
if type(item) == submission.Submission:
cursor.execute("INSERT INTO submissions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [
item.id,
item.author.name if item.author is not None else None,
item.title,
item.selftext if item.is_self else None,
item.url if not item.is_self else None,
item.permalink,
item.score,
item.over_18,
item.subreddit.display_name,
item.created_utc
])
elif type(item) == comment.Comment:
cursor.execute("INSERT INTO comments VALUES (?, ?, ?, ?, ?, ?, ?)", [
item.id,
item.author.name if item.author is not None else None,
item.body,
item.permalink,
item.score,
item.subreddit.display_name,
item.created_utc
])
else:
print('unknown', type(item))
db.commit()
db.close()