-
Notifications
You must be signed in to change notification settings - Fork 1
/
SnowflakeDbModel.py
198 lines (186 loc) · 8.79 KB
/
SnowflakeDbModel.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
from snowflake import connector
from AppConfig import AppConfig
from DbModel import DbModel
from YTLogger import YTLogger
import re
def get_table_creation_scripts():
conf = AppConfig()
yield f"""CREATE TABLE IF NOT EXISTS {conf.tbl_name_channel_info} ( channel_id varchar(255) PRIMARY KEY,
channel_title varchar(255),
channel_url varchar(255),
channel_thumbnail varchar(255),
channel_videos_count varchar(20),
channel_subs varchar(20))"""
yield f"""CREATE TABLE IF NOT EXISTS {conf.tbl_name_channel_videos} ( video_id varchar(255) PRIMARY KEY,
video_title varchar(255),
thumbnail_url varchar(255),
views_count varchar(20),
published_time varchar(50),
channel_id varchar(255))"""
yield f"""CREATE TABLE IF NOT EXISTS {conf.tbl_name_video_stats} ( video_id varchar(255) PRIMARY KEY,
video_title varchar(255),
comments_count varchar(20),
likes_count varchar(20),
channel_id varchar(255),
comments_turned_off varchar(10))"""
yield f"""CREATE TABLE IF NOT EXISTS {conf.tbl_name_video_comments} ( video_id varchar(255),
comment_text text,
author_name varchar(255),
author_thumbnail varchar(255),
published_at varchar(50),
updated_at varchar(50))"""
class SnowflakeDbModel(DbModel):
def __init__(self):
self.logger = YTLogger()
self.conf = AppConfig()
try:
with connector.connect(**self.conf.sql_db_creds) as conn:
cur = conn.cursor()
cur.execute(f"CREATE DATABASE IF NOT EXISTS {self.conf.database_name}")
cur.execute(f"USE {self.conf.database_name}")
for create_table in get_table_creation_scripts():
cur.execute(create_table)
conn.close()
except Exception as e:
print(e)
def test_snowflake_connectivity(self):
is_connection_valid = False
with connector.connect(**self.conf.sql_db_creds) as con:
res = con.cursor().execute("SELECT CURRENT_VERSION()")
v, = res.fetchone()
con.close()
if v is not None:
is_connection_valid = True
return is_connection_valid
def db_write_comments(self, records):
columns = ""
values = ""
video_id = ""
for record in records:
video_id = record.get('video_id')
columns, value = SnowflakeDbModel.extract_column_values(record)
value = re.sub("\"", "'", f"{value}")
if values == "":
values = value
else:
values = values + ", " + value
insert_statement = f"INSERT INTO {self.conf.tbl_name_video_comments} ({columns}) values {values};"
with connector.connect(**self.conf.sql_db_creds) as conn:
try:
cur = conn.cursor()
cur.execute(f"USE {self.conf.database_name}")
if video_id != "":
cur.execute(f"DELETE FROM {self.conf.tbl_name_video_comments} WHERE video_id='{video_id}';")
cur.execute(insert_statement)
if cur.rowcount > 0:
self.logger.log(
f"Successfully inserted, {cur.rowcount} records to {self.conf.tbl_name_video_comments}")
else:
self.logger.log(f"Failed to insert at {self.conf.tbl_name_video_comments}",
self.logger.log_level_error)
cur.close()
except Exception as e:
self.logger.log(f"SF_Exception while writing to **{self.db_write_comments.__name__}** **{str(e)}**",
self.logger.log_level_error)
finally:
if cur is not None:
cur.close()
def db_write_channel_info(self, record):
columns, values = SnowflakeDbModel.extract_column_values(record)
insert_statement = f"INSERT INTO {self.conf.tbl_name_channel_info} ({columns}) values {values};"
try:
with connector.connect(**self.conf.sql_db_creds) as conn:
try:
cur = conn.cursor()
cur.execute(f"USE {self.conf.database_name}")
cur.execute(
f"DELETE FROM {self.conf.tbl_name_channel_info} WHERE CHANNEL_ID='{record.get('channel_id')}'")
cur.execute(insert_statement)
if cur.rowcount > 0:
self.logger.log(
f"Successfully inserted, {cur.rowcount} records to {self.conf.tbl_name_channel_info}")
else:
self.logger.log(f"Failed to insert at {self.conf.tbl_name_channel_info}",
self.logger.log_level_error)
cur.close()
except Exception as e:
self.logger.log(f"SF_Exception while writing to **{self.db_write_channel_info.__name__}** **{str(e)}**",
self.logger.log_level_error)
finally:
if cur is not None:
cur.close()
except Exception as e:
print(e)
def db_write_video_stats(self, record):
columns, values = SnowflakeDbModel.extract_column_values(record)
insert_statement = f"INSERT INTO {self.conf.tbl_name_video_stats} ({columns}) values {values};"
try:
with connector.connect(**self.conf.sql_db_creds) as conn:
try:
cur = conn.cursor()
cur.execute(f"USE {self.conf.database_name}")
cur.execute(
f"DELETE FROM {self.conf.tbl_name_video_stats} WHERE VIDEO_ID='{record.get('video_id')}'")
cur.execute(insert_statement)
if cur.rowcount > 0:
self.logger.log(
f"Successfully inserted, {cur.rowcount} records to {self.conf.tbl_name_video_stats}")
else:
self.logger.log(f"Failed to insert at {self.conf.tbl_name_video_stats}",
self.logger.log_level_error)
cur.close()
except Exception as e:
self.logger.log(
f"SF_Exception while writing to **{self.db_write_video_stats.__name__}** **{str(e)}**",
self.logger.log_level_error)
finally:
if cur is not None:
cur.close()
except Exception as e:
print(e)
def db_write_videos(self, records):
columns = ""
values = ""
channel_id = ""
for record in records:
channel_id = record.get('channel_id')
columns, value = SnowflakeDbModel.extract_column_values(record)
value = re.sub("\"", "'", f"{value}")
if values == "":
values = value
else:
values = values + ", " + value
insert_statement = f"INSERT INTO {self.conf.tbl_name_channel_videos} ({columns}) values {values};"
try:
with connector.connect(**self.conf.sql_db_creds) as conn:
try:
cur = conn.cursor()
cur.execute(f"USE {self.conf.database_name}")
if channel_id != "":
cur.execute(f"DELETE FROM {self.conf.tbl_name_channel_videos} where channel_id = '{channel_id}'")
cur.execute(insert_statement)
if cur.rowcount > 0:
self.logger.log(
f"Successfully inserted, {cur.rowcount} records to {self.conf.tbl_name_channel_videos}")
else:
self.logger.log(f"Failed to insert at {self.conf.tbl_name_video_stats}",
self.logger.log_level_error)
cur.close()
except Exception as e:
self.logger.log(f"SF_Exception while writing to **{self.db_write_videos.__name__}** **{str(e)}**",
self.logger.log_level_error)
except Exception as e:
print(e)
@staticmethod
def extract_column_values(record: dict):
columns, values = "", []
for key, value in record.items():
if columns == "":
columns = key
else:
columns = columns + ", " + key.capitalize()
if type(value) == str:
values.append(re.sub('\'', '', value))
else:
values.append(str(value))
return columns, tuple(values)