-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathdatabase_engine.py
More file actions
185 lines (142 loc) 路 5.54 KB
/
Copy pathdatabase_engine.py
File metadata and controls
185 lines (142 loc) 路 5.54 KB
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
import os
import sqlite3
from .settings import Settings
SELECT_FROM_PROFILE_WHERE_NAME = "SELECT * FROM profiles WHERE name = :name"
INSERT_INTO_PROFILE = "INSERT INTO profiles (name) VALUES (?)"
SQL_CREATE_PROFILE_TABLE = """
CREATE TABLE IF NOT EXISTS `profiles` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL);"""
SQL_CREATE_RECORD_ACTIVITY_TABLE = """
CREATE TABLE IF NOT EXISTS `recordActivity` (
`profile_id` INTEGER REFERENCES `profiles` (id),
`likes` SMALLINT UNSIGNED NOT NULL,
`comments` SMALLINT UNSIGNED NOT NULL,
`follows` SMALLINT UNSIGNED NOT NULL,
`unfollows` SMALLINT UNSIGNED NOT NULL,
`server_calls` INT UNSIGNED NOT NULL,
`created` DATETIME NOT NULL);"""
SQL_CREATE_FOLLOW_RESTRICTION_TABLE = """
CREATE TABLE IF NOT EXISTS `followRestriction` (
`profile_id` INTEGER REFERENCES `profiles` (id),
`username` TEXT NOT NULL,
`times` TINYINT UNSIGNED NOT NULL);"""
SQL_CREATE_SHARE_WITH_PODS_RESTRICTION_TABLE = """
CREATE TABLE IF NOT EXISTS `shareWithPodsRestriction` (
`profile_id` INTEGER REFERENCES `profiles` (id),
`postid` TEXT NOT NULL,
`times` TINYINT UNSIGNED NOT NULL);"""
SQL_CREATE_COMMENT_RESTRICTION_TABLE = """
CREATE TABLE IF NOT EXISTS `commentRestriction` (
`profile_id` INTEGER REFERENCES `profiles` (id),
`postid` TEXT NOT NULL,
`times` TINYINT UNSIGNED NOT NULL);"""
SQL_CREATE_ACCOUNTS_PROGRESS_TABLE = """
CREATE TABLE IF NOT EXISTS `accountsProgress` (
`profile_id` INTEGER NOT NULL,
`followers` INTEGER NOT NULL,
`following` INTEGER NOT NULL,
`total_posts` INTEGER NOT NULL,
`created` DATETIME NOT NULL,
`modified` DATETIME NOT NULL,
CONSTRAINT `fk_accountsProgress_profiles1`
FOREIGN KEY(`profile_id`) REFERENCES `profiles`(`id`));"""
def get_database(make=False):
logger = Settings.logger
credentials = Settings.profile
profile_id, name = credentials["id"], credentials["name"]
address = validate_database_address()
if not os.path.isfile(address) or make:
create_database(address, logger, name)
profile_id = (
get_profile(name, address, logger) if profile_id is None or make else profile_id
)
return address, profile_id
def create_database(address, logger, name):
try:
connection = sqlite3.connect(address)
with connection:
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
create_tables(
cursor,
[
"profiles",
"recordActivity",
"followRestriction",
"shareWithPodsRestriction",
"commentRestriction",
"accountsProgress",
],
)
connection.commit()
except Exception as exc:
logger.warning(
"Wah! Error occurred while getting a DB for '{}':\n\t{}".format(
name, str(exc).encode("utf-8")
)
)
finally:
if connection:
# close the open connection
connection.close()
def create_tables(cursor, tables):
if "profiles" in tables:
cursor.execute(SQL_CREATE_PROFILE_TABLE)
if "recordActivity" in tables:
cursor.execute(SQL_CREATE_RECORD_ACTIVITY_TABLE)
if "followRestriction" in tables:
cursor.execute(SQL_CREATE_FOLLOW_RESTRICTION_TABLE)
if "shareWithPodsRestriction" in tables:
cursor.execute(SQL_CREATE_SHARE_WITH_PODS_RESTRICTION_TABLE)
if "commentRestriction" in tables:
cursor.execute(SQL_CREATE_COMMENT_RESTRICTION_TABLE)
if "accountsProgress" in tables:
cursor.execute(SQL_CREATE_ACCOUNTS_PROGRESS_TABLE)
def verify_database_directories(address):
db_dir = os.path.dirname(address)
if not os.path.exists(db_dir):
os.makedirs(db_dir)
def validate_database_address():
address = Settings.database_location
if not address.endswith(".db"):
slash = "\\" if "\\" in address else "/"
address = address if address.endswith(slash) else address + slash
address += "instapy.db"
Settings.database_location = address
verify_database_directories(address)
return address
def get_profile(name, address, logger):
try:
conn = sqlite3.connect(address)
with conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
profile = select_profile_by_username(cursor, name)
if profile is None:
add_profile(conn, cursor, name)
# reselect the table after adding data to get the proper `id`
profile = select_profile_by_username(cursor, name)
except Exception as exc:
logger.warning(
"Heeh! Error occurred while getting a DB profile for '{}':\n\t{}".format(
name, str(exc).encode("utf-8")
)
)
finally:
if conn:
# close the open connection
conn.close()
profile = dict(profile)
profile_id = profile["id"]
# assign the id to its child in `Settings` class
Settings.profile["id"] = profile_id
return profile_id
def add_profile(conn, cursor, name):
cursor.execute(INSERT_INTO_PROFILE, (name,))
# commit the latest changes
conn.commit()
def select_profile_by_username(cursor, name):
cursor.execute(SELECT_FROM_PROFILE_WHERE_NAME, {"name": name})
profile = cursor.fetchone()
return profile