-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
76 lines (66 loc) · 1.94 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
# /////////////////////////////////////////////////////////////////////////////////////////////
# -- Libraries --
import psycopg2
from psycopg2 import sql
from dotenv import load_dotenv
import os
# /////////////////////////////////////////////////////////////////////////////////////////////
load_dotenv() # Load environment variables
# -- Database Configurations --
conn = psycopg2.connect(
dbname= os.environ.get('DB_NAME'),
user= os.environ.get('DB_USER'),
password= os.environ.get('DB_PASSWORD'),
host= os.environ.get('DB_HOST'),
port= os.environ.get('DB_PORT')
)
cursor = conn.cursor()
# -- Database Tables --
# Comments Table
def comments_table():
cursor.execute("""
CREATE TABLE IF NOT EXISTS comments_data (
id SERIAL PRIMARY KEY,
video_id VARCHAR(255) NOT NULL,
author VARCHAR(255),
published_at TIMESTAMP,
updated_at TIMESTAMP,
like_count INTEGER,
text TEXT,
UNIQUE (video_id, author, published_at)
)
""")
conn.commit()
print('Comments table created or updated')
# Channels Table
def channels_table():
cursor.execute("""
CREATE TABLE IF NOT EXISTS channels_data (
id SERIAL PRIMARY KEY,
channel_id VARCHAR(255) UNIQUE NOT NULL,
title TEXT,
description TEXT,
view_count INTEGER,
subscriber_count INTEGER,
video_count INTEGER,
published_at TIMESTAMP WITH TIME ZONE
)
""")
conn.commit()
print('Channels table created or updated')
# Video Duration Table
def video_duration_table():
cursor.execute("""
CREATE TABLE IF NOT EXISTS video_duration_data (
id SERIAL PRIMARY KEY,
video_id VARCHAR(255) UNIQUE NOT NULL,
duration INTERVAL
)
""")
conn.commit()
print('Video duration table created or updated')
def ensure_tables_exist():
comments_table()
channels_table()
video_duration_table()
ensure_tables_exist()