-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
219 lines (198 loc) · 7.47 KB
/
database.py
File metadata and controls
219 lines (198 loc) · 7.47 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
SQLite Database module for YTDL Telegram Bot.
Handles channel subscriptions persistence.
"""
import sqlite3
import logging
from datetime import datetime
from typing import List, Optional, Tuple
logger = logging.getLogger(__name__)
DB_PATH = './data/subscriptions.db'
def init_db():
"""Initialize the database and create tables."""
import os
os.makedirs('./data', exist_ok=True)
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Subscriptions table
cursor.execute('''
CREATE TABLE IF NOT EXISTS subscriptions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
channel_id TEXT NOT NULL,
channel_name TEXT,
chat_id INTEGER NOT NULL,
max_quality INTEGER DEFAULT 1080,
sub_type TEXT DEFAULT 'video', -- 'video' or 'live'
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(channel_id, chat_id, sub_type)
)
''')
# Processed videos table (to avoid duplicates)
cursor.execute('''
CREATE TABLE IF NOT EXISTS processed_videos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
video_id TEXT NOT NULL UNIQUE,
channel_id TEXT NOT NULL,
title TEXT,
processed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# User settings table
cursor.execute('''
CREATE TABLE IF NOT EXISTS user_settings (
chat_id INTEGER PRIMARY KEY,
download_mode TEXT DEFAULT 'video',
resolution INTEGER DEFAULT 1080
)
''')
conn.commit()
conn.close()
logger.info("Database initialized successfully")
def add_subscription(channel_id: str, channel_name: str, chat_id: int, max_quality: int = 1080, sub_type: str = 'video') -> bool:
"""Add a new channel subscription."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO subscriptions (channel_id, channel_name, chat_id, max_quality, sub_type)
VALUES (?, ?, ?, ?, ?)
''', (channel_id, channel_name, chat_id, max_quality, sub_type))
conn.commit()
conn.close()
logger.info(f"Subscription added: {channel_name} ({channel_id}) [{sub_type}] -> chat {chat_id}")
return True
except Exception as e:
logger.error(f"Failed to add subscription: {e}")
return False
def remove_subscription(channel_id: str, chat_id: int, sub_type: str = 'video') -> bool:
"""Remove a channel subscription."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
DELETE FROM subscriptions WHERE channel_id = ? AND chat_id = ? AND sub_type = ?
''', (channel_id, chat_id, sub_type))
deleted = cursor.rowcount > 0
conn.commit()
conn.close()
return deleted
except Exception as e:
logger.error(f"Failed to remove subscription: {e}")
return False
def get_all_subscriptions() -> List[Tuple]:
"""Get all active subscriptions."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('SELECT channel_id, channel_name, chat_id, max_quality, sub_type FROM subscriptions')
results = cursor.fetchall()
conn.close()
return results
except Exception as e:
logger.error(f"Failed to get subscriptions: {e}")
return []
def get_user_subscriptions(chat_id: int) -> List[Tuple]:
"""Get subscriptions for a specific user/chat."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
SELECT channel_id, channel_name, max_quality, sub_type, created_at
FROM subscriptions WHERE chat_id = ?
''', (chat_id,))
results = cursor.fetchall()
conn.close()
return results
except Exception as e:
logger.error(f"Failed to get user subscriptions: {e}")
return []
def is_video_processed(video_id: str) -> bool:
"""Check if a video has already been processed."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('SELECT 1 FROM processed_videos WHERE video_id = ?', (video_id,))
result = cursor.fetchone() is not None
conn.close()
return result
except Exception as e:
logger.error(f"Failed to check processed video: {e}")
return False
def mark_video_processed(video_id: str, channel_id: str, title: str) -> bool:
"""Mark a video as processed."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
INSERT OR IGNORE INTO processed_videos (video_id, channel_id, title)
VALUES (?, ?, ?)
''', (video_id, channel_id, title))
conn.commit()
conn.close()
return True
except Exception as e:
logger.error(f"Failed to mark video processed: {e}")
return False
def cleanup_old_processed(days: int = 30):
"""Remove processed video records older than specified days."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
DELETE FROM processed_videos
WHERE processed_at < datetime('now', ?)
''', (f'-{days} days',))
deleted = cursor.rowcount
conn.commit()
conn.close()
if deleted > 0:
logger.info(f"Cleaned up {deleted} old processed video records")
except Exception as e:
logger.error(f"Failed to cleanup old records: {e}")
def get_user_settings(chat_id: int) -> dict:
"""Get settings for a specific user/chat."""
try:
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('''
SELECT download_mode, resolution
FROM user_settings WHERE chat_id = ?
''', (chat_id,))
row = cursor.fetchone()
conn.close()
if row:
return dict(row)
else:
# Default settings
return {'download_mode': 'video', 'resolution': 1080}
except Exception as e:
logger.error(f"Failed to get user settings: {e}")
return {'download_mode': 'video', 'resolution': 1080}
def update_user_settings(chat_id: int, download_mode: str = None, resolution: int = None) -> bool:
"""Update user settings."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Check if settings exist
cursor.execute('SELECT 1 FROM user_settings WHERE chat_id = ?', (chat_id,))
exists = cursor.fetchone() is not None
if not exists:
# Insert default first then update or just insert
mode = download_mode or 'video'
res = resolution or 1080
cursor.execute('''
INSERT INTO user_settings (chat_id, download_mode, resolution)
VALUES (?, ?, ?)
''', (chat_id, mode, res))
else:
if download_mode:
cursor.execute('UPDATE user_settings SET download_mode = ? WHERE chat_id = ?', (download_mode, chat_id))
if resolution:
cursor.execute('UPDATE user_settings SET resolution = ? WHERE chat_id = ?', (resolution, chat_id))
conn.commit()
conn.close()
return True
except Exception as e:
logger.error(f"Failed to update user settings: {e}")
return False