-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdb.py
144 lines (122 loc) · 3.77 KB
/
db.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
import os
import sys
import sqlite3
from urllib.parse import urlparse
from settings import IGNORED_BOTH, IGNORED_LINKS, IGNORED_SOURCES, IGNORED_USERS
db = sqlite3.connect('totes.sqlite3')
cur = db.cursor()
def create_tables():
"""
Create tables.
"""
cur.execute("""
CREATE TABLE subreddits (
name TEXT PRIMARY KEY,
skip_source BOOLEAN DEFAULT FALSE,
skip_link BOOLEAN DEFAULT FALSE,
language TEXT DEFAULT 'en',
t TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cur.execute("""
CREATE TABLE users (
name TEXT PRIMARY KEY,
skip_source BOOLEAN DEFAULT FALSE,
skip_link BOOLEAN DEFAULT FALSE,
t TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cur.execute("""
CREATE TABLE sources (
id TEXT PRIMARY KEY,
reply TEXT UNIQUE,
subreddit TEXT,
author TEXT,
title TEXT,
skip BOOLEAN DEFAULT FALSE,
t TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cur.execute("""
CREATE TABLE links (
id TEXT PRIMARY KEY,
source TEXT,
subreddit TEXT,
author TEXT,
title TEXT,
permalink TEXT,
skip BOOLEAN DEFAULT FALSE,
t TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cur.execute("""
CREATE INDEX ON links (source)
""")
db.commit()
print("Tables ready.")
def sub_exists(sub):
cur.execute("SELECT 1 FROM subreddits WHERE name=? LIMIT 1", (sub,))
return True if cur.fetchone() else False
def user_exists(user):
cur.execute("SELECT 1 FROM users WHERE name=? LIMIT 1", (user,))
return True if cur.fetchone() else False
def populate_db():
for sub in IGNORED_SOURCES:
if sub_exists(sub):
print("Updating {}".format(sub))
cur.execute("""
UPDATE subreddits SET skip_source=%s
WHERE name=%s
""", (True, sub))
else:
print("Inserting {}".format(sub))
cur.execute("""
INSERT INTO subreddits (name, skip_source)
VALUES (%s, %s)
""", (sub, True))
for sub in IGNORED_BOTH:
if sub_exists(sub):
print("Updating {}".format(sub))
cur.execute("""
UPDATE subreddits SET skip_source=%s, skip_link=%s
WHERE name=%s
""", (True, True, sub))
else:
print("Inserting {}".format(sub))
cur.execute("""
INSERT INTO subreddits (name, skip_source, skip_link)
VALUES (%s, %s, %s)
""", (sub, True, True))
for sub in IGNORED_LINKS:
if sub_exists(sub):
print("Updating {}".format(sub))
cur.execute("""
UPDATE subreddits SET skip_link=%s
WHERE name=%s
""", (True, sub))
else:
print("Inserting {}".format(sub))
cur.execute("""
INSERT INTO subreddits (name, skip_link)
VALUES (%s, %s)
""", (sub, True))
for user in IGNORED_USERS:
if user_exists(user):
print("Updating {}".format(user))
cur.execute("""
UPDATE users SET skip_link=%s
WHERE name=%s
""", (True, user))
else:
print("Inserting {}".format(user))
cur.execute("""
INSERT INTO users (name, skip_link) VALUES (%s, %s)
""", (user, True))
db.commit()
print("Default settings setup.")
if __name__ == '__main__':
if 'create' in sys.argv:
create_tables()
if 'populate' in sys.argv:
populate_db()
db.close()