-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_cache.py
134 lines (120 loc) · 3.71 KB
/
build_cache.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
import pymysql.cursors
from secrets import HOST, DB_USER, DB_PASSWORD, DB, CHARSET
def db_setup():
"""
Drops and creates the following tables: Subreddit, Submission, and Comment.
Args:
None
Returns:
None
"""
# Connect to the database
try:
connection = pymysql.connect(host = HOST,
user = DB_USER,
password = DB_PASSWORD,
db = DB,
charset = CHARSET,
cursorclass = pymysql.cursors.DictCursor)
cursor = connection.cursor()
except Exception as e:
print(f"Error creating DB: {e}.")
connection.close()
###################
### DROP TABLES ###
# Drop Subreddit table
try:
statement = """
DROP TABLE IF EXISTS Subreddit;
"""
cursor.execute(statement)
connection.commit()
except Exception as e:
print(f"Error dropping Subreddit table: {e}.")
connection.close()
# Drop Submission table
try:
statement = """
DROP TABLE IF EXISTS Submission;
"""
cursor.execute(statement)
connection.commit()
except Exception as e:
print(f"Error dropping Submission table: {e}.")
connection.close()
# Drop Comment table
try:
statement = """
DROP TABLE IF EXISTS Comment;
"""
cursor.execute(statement)
connection.commit()
except Exception as e:
print(f"Error dropping Comment table: {e}.")
connection.close()
#####################
### CREATE TABLES ###
# Create Subreddit table
try:
statement = """
CREATE TABLE Subreddit(
insert_update_ts TIMESTAMP,
id VARCHAR(100) PRIMARY KEY,
name VARCHAR(100) NOT NULL,
display_name VARCHAR(100) NOT NULL,
subscribers INT NOT NULL,
user_is_banned BOOLEAN NOT NULL
);
"""
cursor.execute(statement)
connection.commit()
except Exception as e:
print(f"Error creating Subreddit table: {e}.")
connection.close()
# Create Submission table
try:
statement = """
CREATE TABLE Submission(
insert_update_ts TIMESTAMP,
id VARCHAR(100) PRIMARY KEY,
subreddit_id VARCHAR(100) NOT NUll,
author VARCHAR(100) NOT NULL,
created_utc VARCHAR(100) NOT NULL,
is_original_content BOOLEAN NOT NULL,
locked BOOLEAN NOT NULL,
name VARCHAR(100),
title VARCHAR(100),
num_comments INT NOT NULL,
score INT NOT NULL,
permalink VARCHAR(100) NOT NULL,
retrieved TIMESTAMP NOT NULL
);
"""
cursor.execute(statement)
connection.commit()
except Exception as e:
print(f"Error creating Submission table: {e}.")
connection.close()
# Create Comment table
try:
statement = """
CREATE TABLE Comment(
insert_update_ts TIMESTAMP,
id VARCHAR(100) PRIMARY KEY,
link_id VARCHAR(100) NOT NULL,
subreddit_name VARCHAR(100) NOT NULL,
author VARCHAR(100) NOT NULL,
body BLOB NOT NULL,
score INT NOT NULL,
permalink VARCHAR(100) NOT NULL,
retrieved TIMESTAMP NOT NULL
);
"""
#* link_id = submission_id
cursor.execute(statement)
connection.commit()
except Exception as e:
print(f"Error creating Comment table: {e}.")
connection.close()
if __name__ == "__main__":
db_setup()