-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_db_setup.py
More file actions
197 lines (164 loc) · 7.09 KB
/
SQL_db_setup.py
File metadata and controls
197 lines (164 loc) · 7.09 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
186
187
188
189
190
191
192
193
194
195
196
197
import mysql.connector
import os
import json
class VideoDatabase:
def __init__(self, host='localhost', user='your_username', password='your_password', database='your_database'):
self.conn = None
self.config = {
'host': host,
'user': user,
'password': password,
'database': database,
}
self.create_table()
self.analytics_table()
def create_table(self):
try:
self.conn = mysql.connector.connect(**self.config)
cursor = self.conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS stats(
video_id VARCHAR(255) PRIMARY KEY,
commentCount INT,
viewCount INT,
favoriteCount INT,
dislikeCount INT,
likeCount INT
);
''')
except Exception as e:
print(f"Error creating table: {e}")
finally:
if self.conn:
self.conn.close()
def insert_videos(self, folder_path = 'test'):
try:
self.conn = mysql.connector.connect(**self.config)
cursor = self.conn.cursor()
for filename in os.listdir(folder_path):
if filename.endswith('.json'):
file_path = os.path.join(folder_path, filename)
with open(file_path, 'r') as file:
data = json.load(file)
video_id = data['videoInfo']['id']
statistics = data['videoInfo']['statistics']
cursor.execute('''
INSERT IGNORE INTO stats (video_id, commentCount, viewCount, favoriteCount, dislikeCount, likeCount)
VALUES (%s, %s, %s, %s, %s, %s)
''', (video_id, statistics.get('commentCount', 0), statistics.get('viewCount', 0),
statistics.get('favoriteCount', 0), statistics.get('dislikeCount', 0),
statistics.get('likeCount', 0)))
self.conn.commit()
print("Videos inserted successfully!")
except Exception as e:
print(f"Error inserting videos: {e}")
finally:
if self.conn:
self.conn.close()
def query_by_video_id(self, video_id):
try:
self.conn = mysql.connector.connect(**self.config)
cursor = self.conn.cursor()
cursor.execute('''
SELECT * FROM stats WHERE video_id = %s
''', (video_id,))
result = cursor.fetchall()
return result
except Exception as e:
print(f"Error querying by video_id: {e}")
return None
finally:
if self.conn:
self.conn.close()
def query_by_video_id_2(self, video_id):
try:
self.conn = mysql.connector.connect(**self.config)
cursor = self.conn.cursor()
cursor.execute('''
SELECT * FROM engagement WHERE video_id = %s
''', (video_id,))
result = cursor.fetchall()
return result
except Exception as e:
print(f"Error querying by video_id: {e}")
return None
finally:
if self.conn:
self.conn.close()
def performing_search(self, vid):
vidstats_dict={}
sql_result = self.query_by_video_id(vid)
if sql_result:
vidstats_dict = {
'video_id': vid,
'commentCount': sql_result[0][1],
'viewCount': sql_result[0][2],
'favoriteCount': sql_result[0][3],
'dislikeCount': sql_result[0][4],
'likeCount': sql_result[0][5]
}
return vidstats_dict
def performing_search_2(self, vid):
engmnts_dict={}
sql_result = self.query_by_video_id_2(vid)
if sql_result:
engmnts_dict = {
'video_id': vid,
'engagement': sql_result[0][1],
'engagement_ratio': sql_result[0][2],
'clash_of_tastes': sql_result[0][3]
}
return engmnts_dict
def analytics_table(self):
try:
self.conn = mysql.connector.connect(**self.config)
cursor = self.conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS engagement (
video_id VARCHAR(255) PRIMARY KEY,
engagement INT,
engagement_ratio VARCHAR(10),
clash_of_tastes VARCHAR(10),
FOREIGN KEY (video_id) REFERENCES stats(video_id)
);
''')
except Exception as e:
print(f"Error creating table: {e}")
finally:
if self.conn:
self.conn.close()
def insert_engagement_details(self, folder_path='test'):
try:
self.conn = mysql.connector.connect(**self.config)
cursor = self.conn.cursor()
for filename in os.listdir(folder_path):
if filename.endswith('.json'):
file_path = os.path.join(folder_path, filename)
with open(file_path, 'r') as file:
data = json.load(file)
video_id = data['videoInfo']['id']
# No need to fetch statistics here, as you're using a SELECT statement below
cursor.execute('''
INSERT INTO engagement (video_id, engagement, engagement_ratio, clash_of_tastes)
SELECT
s.video_id,
(s.viewCount + s.likeCount + s.dislikeCount) AS engagement,
CASE WHEN s.likeCount >= s.viewCount / 10 THEN 'good' ELSE 'poor' END AS engagement_ratio,
CASE WHEN (s.likeCount - s.dislikeCount) >= 0 THEN 'Liking' ELSE 'disliking' END AS clash_of_tastes
FROM stats s
WHERE s.video_id = %s
AND NOT EXISTS (
SELECT 1 FROM engagement e where e.video_id = s.video_id
)
''', (video_id,))
self.conn.commit()
print("Engagement Table created successfully!")
except Exception as e:
print(f"Error inserting engagement details: {e}")
finally:
if self.conn:
self.conn.close()
if __name__ == "__main__":
video_db = VideoDatabase(user='root', password='Rey@nsh4', database='Course_Project')
video_db.insert_videos()
video_db.insert_engagement_details()