File size: 19,646 Bytes
d043a72 | 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | import time
import psycopg2
from psycopg2 import pool
from config import DB_URL, logger
# =========================
# CONNECTION POOL
# =========================
if not DB_URL:
raise RuntimeError("DATABASE_URL is not set. Please configure environment variables.")
db_pool = psycopg2.pool.SimpleConnectionPool(1, 20, DB_URL)
def init_db():
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
CREATE TABLE IF NOT EXISTS users (
user_id BIGINT PRIMARY KEY,
username TEXT,
balance INTEGER DEFAULT 1000,
vip_until BIGINT DEFAULT 0,
ref_by BIGINT DEFAULT NULL,
last_daily BIGINT DEFAULT 0,
last_url TEXT DEFAULT NULL,
created_at BIGINT DEFAULT 0
)""")
c.execute("""
CREATE TABLE IF NOT EXISTS opened_chapters (
user_id BIGINT,
chapter_id TEXT,
opened_at BIGINT DEFAULT 0,
manga_slug TEXT DEFAULT NULL,
PRIMARY KEY (user_id, chapter_id)
)""")
c.execute("""
CREATE TABLE IF NOT EXISTS vouchers (
code TEXT PRIMARY KEY,
coins INTEGER DEFAULT 0,
vip_days INTEGER DEFAULT 0,
used_by BIGINT DEFAULT NULL
)""")
c.execute("""
CREATE TABLE IF NOT EXISTS manga_index (
slug TEXT PRIMARY KEY,
title TEXT,
url TEXT,
thumbnail TEXT
)""")
c.execute("""
CREATE TABLE IF NOT EXISTS manga_genres (
manga_slug TEXT,
genre_id TEXT,
genre_name TEXT,
PRIMARY KEY (manga_slug, genre_id)
)""")
c.execute("""
CREATE TABLE IF NOT EXISTS bookmarks (
user_id BIGINT,
manga_slug TEXT,
manga_url TEXT,
manga_title TEXT,
last_chapter_id TEXT DEFAULT NULL,
added_at BIGINT DEFAULT 0,
PRIMARY KEY (user_id, manga_slug)
)""")
c.execute("""
CREATE TABLE IF NOT EXISTS chapter_cache (
manga_slug TEXT,
chapter_id TEXT,
file_format TEXT DEFAULT 'pdf',
file_id TEXT,
PRIMARY KEY (manga_slug, chapter_id, file_format)
)""")
c.execute("""
CREATE TABLE IF NOT EXISTS manga_queue (
url TEXT PRIMARY KEY,
added_at BIGINT DEFAULT 0
)""")
# Migrations
c.execute("ALTER TABLE users ADD COLUMN IF NOT EXISTS username TEXT DEFAULT NULL")
c.execute("ALTER TABLE users ADD COLUMN IF NOT EXISTS created_at BIGINT DEFAULT 0")
c.execute("ALTER TABLE opened_chapters ADD COLUMN IF NOT EXISTS manga_slug TEXT DEFAULT NULL")
c.execute("ALTER TABLE chapter_cache ADD COLUMN IF NOT EXISTS file_format TEXT DEFAULT 'pdf'")
c.execute("ALTER TABLE bookmarks ADD COLUMN IF NOT EXISTS manga_slug TEXT DEFAULT NULL")
c.execute(
"UPDATE bookmarks "
"SET manga_slug = split_part(trim(trailing '/' from manga_url), '/manga/', 2) "
"WHERE (manga_slug IS NULL OR manga_slug = '') AND manga_url IS NOT NULL"
)
c.execute(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_bookmarks_user_slug "
"ON bookmarks (user_id, manga_slug)"
)
c.execute("UPDATE users SET created_at = %s WHERE created_at = 0 OR created_at IS NULL", (int(time.time()),))
c.execute("ALTER TABLE users ADD COLUMN IF NOT EXISTS preferred_format TEXT DEFAULT 'pdf'")
conn.commit()
finally:
db_pool.putconn(conn)
# =========================
# USER HELPERS
# =========================
def get_user_preferred_format(user_id) -> str:
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT preferred_format FROM users WHERE user_id=%s", (user_id,))
row = c.fetchone()
return row[0] if row else 'pdf'
finally:
db_pool.putconn(conn)
def set_user_preferred_format(user_id, format_type: str):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("UPDATE users SET preferred_format=%s WHERE user_id=%s", (format_type, user_id))
conn.commit()
finally:
db_pool.putconn(conn)
def get_user(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
SELECT user_id, username, balance, vip_until, ref_by, last_daily, last_url
FROM users WHERE user_id=%s
""", (user_id,))
return c.fetchone()
finally:
db_pool.putconn(conn)
def create_user(user_id, username=None):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"INSERT INTO users (user_id, username, balance, vip_until, created_at) "
"VALUES (%s,%s,%s,%s,%s) ON CONFLICT (user_id) DO UPDATE SET username = EXCLUDED.username",
(user_id, username, 1000, 0, int(time.time()))
)
conn.commit()
finally:
db_pool.putconn(conn)
def ensure_user(user_id, username=None):
user = get_user(user_id)
if not user:
create_user(user_id, username)
user = get_user(user_id)
elif username and user[1] != username:
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("UPDATE users SET username=%s WHERE user_id=%s", (username, user_id))
conn.commit()
finally:
db_pool.putconn(conn)
return user
def update_user_status(user_id, balance, vip_until=None):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
if vip_until is not None:
c.execute("UPDATE users SET balance=%s, vip_until=%s WHERE user_id=%s",
(balance, vip_until, user_id))
else:
c.execute("UPDATE users SET balance=%s WHERE user_id=%s", (balance, user_id))
conn.commit()
finally:
db_pool.putconn(conn)
def update_last_daily(user_id, ts):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("UPDATE users SET last_daily=%s WHERE user_id=%s", (ts, user_id))
conn.commit()
finally:
db_pool.putconn(conn)
def save_last_url(user_id, url):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("UPDATE users SET last_url=%s WHERE user_id=%s", (url, user_id))
conn.commit()
finally:
db_pool.putconn(conn)
def is_vip(user):
if not user or len(user) < 4: return False
return user[3] is not None and user[3] > int(time.time())
def vip_days_left(user):
if not is_vip(user): return 0
return max(0, (user[3] - int(time.time())) // 86400)
# =========================
# CHAPTERS / HISTORY
# =========================
def is_opened(user_id: int, chapter_id: str) -> bool:
conn = db_pool.getconn()
try:
with conn.cursor() as c:
one_week_ago = int(time.time()) - 7 * 86400
c.execute(
"SELECT 1 FROM opened_chapters WHERE user_id=%s AND chapter_id=%s AND opened_at >= %s",
(user_id, str(chapter_id), one_week_ago),
)
return c.fetchone() is not None
finally:
db_pool.putconn(conn)
def save_opened(user_id, ch_id, manga_slug=None):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"INSERT INTO opened_chapters (user_id, chapter_id, opened_at, manga_slug) "
"VALUES (%s,%s,%s,%s) ON CONFLICT (user_id, chapter_id) "
"DO UPDATE SET opened_at=EXCLUDED.opened_at, manga_slug=EXCLUDED.manga_slug",
(user_id, str(ch_id), int(time.time()), manga_slug)
)
conn.commit()
finally:
db_pool.putconn(conn)
def get_user_history(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"SELECT chapter_id, opened_at FROM opened_chapters "
"WHERE user_id=%s ORDER BY opened_at DESC LIMIT 10",
(user_id,)
)
return c.fetchall()
finally:
db_pool.putconn(conn)
def get_chapters_bought_count(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT COUNT(*) FROM opened_chapters WHERE user_id=%s", (user_id,))
return c.fetchone()[0]
finally:
db_pool.putconn(conn)
def get_referral_count(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT COUNT(*) FROM users WHERE ref_by=%s", (user_id,))
return c.fetchone()[0]
finally:
db_pool.putconn(conn)
def get_top_buyers(limit=5):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
SELECT u.user_id, u.username, COUNT(oc.chapter_id) AS cnt
FROM users u
JOIN opened_chapters oc ON u.user_id = oc.user_id
GROUP BY u.user_id, u.username ORDER BY cnt DESC LIMIT %s
""", (limit,))
return c.fetchall()
finally:
db_pool.putconn(conn)
# =========================
# MANGA INDEX / GENRES
# =========================
def upsert_manga_index(slug, title, url, thumbnail=""):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
INSERT INTO manga_index (slug, title, url, thumbnail)
VALUES (%s, %s, %s, %s)
ON CONFLICT (slug) DO UPDATE
SET title=EXCLUDED.title, url=EXCLUDED.url, thumbnail=EXCLUDED.thumbnail
""", (slug, title, url, thumbnail))
conn.commit()
finally:
db_pool.putconn(conn)
def search_manga_index(query: str, limit: int = 10):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
SELECT slug, title, url FROM manga_index
WHERE LOWER(title) LIKE LOWER(%s) LIMIT %s
""", (f"%{query}%", limit))
return c.fetchall()
finally:
db_pool.putconn(conn)
def save_manga_genres(manga_slug: str, genres: list):
if not genres:
logger.warning(f"β οΈ save_manga_genres called with empty list for {manga_slug}")
return
conn = db_pool.getconn()
try:
with conn.cursor() as c:
for g in genres:
gid = str(g.get("term_id", g.get("id", "")))
gname = g.get("name", "")
if gid and gname:
c.execute("""
INSERT INTO manga_genres (manga_slug, genre_id, genre_name)
VALUES (%s, %s, %s) ON CONFLICT DO NOTHING
""", (manga_slug, gid, gname))
conn.commit()
logger.info(f"β
Saved {len(genres)} genres for {manga_slug}")
except Exception as e:
logger.error(f"β save_manga_genres failed for {manga_slug}: {e}")
finally:
db_pool.putconn(conn)
def get_manga_by_genre(genre_id: str, limit: int = 30):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
SELECT mi.slug, mi.title, mi.url
FROM manga_genres mg
JOIN manga_index mi ON mg.manga_slug = mi.slug
WHERE mg.genre_id = %s LIMIT %s
""", (genre_id, limit))
return c.fetchall()
finally:
db_pool.putconn(conn)
# =========================
# BOOKMARKS / FAVORITES
# =========================
def get_manga_slug(manga_url: str) -> str:
return manga_url.rstrip("/").split("/manga/")[-1].rstrip("/")
def add_bookmark(user_id, manga_url, manga_title, last_chapter_id=None):
slug = get_manga_slug(manga_url)
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
INSERT INTO bookmarks (user_id, manga_slug, manga_title, manga_url, last_chapter_id, added_at)
VALUES (%s, %s, %s, %s, %s, %s)
ON CONFLICT (user_id, manga_slug) DO UPDATE
SET manga_title=EXCLUDED.manga_title, manga_url=EXCLUDED.manga_url
""", (user_id, slug, manga_title, manga_url, last_chapter_id, int(time.time())))
conn.commit()
finally:
db_pool.putconn(conn)
def remove_bookmark(user_id, manga_url):
slug = get_manga_slug(manga_url)
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("DELETE FROM bookmarks WHERE user_id=%s AND manga_slug=%s", (user_id, slug))
conn.commit()
finally:
db_pool.putconn(conn)
def get_bookmarks(user_id):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("""
SELECT manga_url, manga_title, last_chapter_id, added_at, manga_slug
FROM bookmarks WHERE user_id=%s ORDER BY added_at DESC
""", (user_id,))
return c.fetchall()
finally:
db_pool.putconn(conn)
def is_bookmarked(user_id, manga_url) -> bool:
slug = get_manga_slug(manga_url)
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT 1 FROM bookmarks WHERE user_id=%s AND manga_slug=%s", (user_id, slug))
return c.fetchone() is not None
finally:
db_pool.putconn(conn)
def update_bookmark_chapter(user_id, manga_url, chapter_id):
slug = get_manga_slug(manga_url)
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"UPDATE bookmarks SET last_chapter_id=%s WHERE user_id=%s AND manga_slug=%s",
(chapter_id, user_id, slug)
)
conn.commit()
finally:
db_pool.putconn(conn)
def get_all_bookmarks_for_notification():
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT user_id, manga_url, manga_title, last_chapter_id FROM bookmarks WHERE manga_url IS NOT NULL")
return c.fetchall()
finally:
db_pool.putconn(conn)
# =========================
# MANGA QUEUE
# =========================
def save_manga_queue(urls: list):
if not urls: return
conn = db_pool.getconn()
try:
with conn.cursor() as c:
for url in urls:
c.execute(
"INSERT INTO manga_queue (url, added_at) VALUES (%s, %s) "
"ON CONFLICT (url) DO UPDATE SET added_at = EXCLUDED.added_at",
(url, int(time.time()))
)
conn.commit()
finally:
db_pool.putconn(conn)
def get_manga_queue() -> list:
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT url FROM manga_queue ORDER BY added_at")
return [row[0] for row in c.fetchall()]
finally:
db_pool.putconn(conn)
def get_manga_queue_count() -> int:
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT COUNT(*) FROM manga_queue")
return c.fetchone()[0]
finally:
db_pool.putconn(conn)
def get_cached_file_id(manga_slug: str, chapter_id: str, file_format: str):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"SELECT file_id FROM chapter_cache WHERE manga_slug=%s AND chapter_id=%s AND file_format=%s",
(manga_slug, str(chapter_id), file_format),
)
row = c.fetchone()
return row[0] if row else None
finally:
db_pool.putconn(conn)
def save_cached_file_id(manga_slug: str, chapter_id: str, file_format: str, file_id: str):
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute(
"INSERT INTO chapter_cache (manga_slug, chapter_id, file_format, file_id) "
"VALUES (%s, %s, %s, %s) "
"ON CONFLICT (manga_slug, chapter_id, file_format) DO UPDATE SET file_id=EXCLUDED.file_id",
(manga_slug, str(chapter_id), file_format, file_id),
)
conn.commit()
finally:
db_pool.putconn(conn)
# =========================
# WEEKLY STATS
# =========================
def get_weekly_stats():
one_week_ago = int(time.time()) - 7 * 86400
conn = db_pool.getconn()
try:
with conn.cursor() as c:
c.execute("SELECT COUNT(*) FROM users")
total_users = c.fetchone()[0]
c.execute("SELECT COUNT(*) FROM users WHERE created_at >= %s", (one_week_ago,))
new_users = c.fetchone()[0]
c.execute("SELECT COUNT(*) FROM opened_chapters WHERE opened_at >= %s", (one_week_ago,))
weekly_chapters = c.fetchone()[0]
c.execute("""
SELECT u.user_id, u.username, COUNT(oc.chapter_id) AS cnt
FROM users u JOIN opened_chapters oc ON u.user_id = oc.user_id
WHERE oc.opened_at >= %s
GROUP BY u.user_id, u.username ORDER BY cnt DESC LIMIT 5
""", (one_week_ago,))
top_users = c.fetchall()
c.execute("""
SELECT manga_slug, COUNT(*) AS cnt FROM opened_chapters
WHERE opened_at >= %s AND manga_slug IS NOT NULL
GROUP BY manga_slug ORDER BY cnt DESC LIMIT 5
""", (one_week_ago,))
top_manga = c.fetchall()
c.execute("SELECT COUNT(*) FROM chapter_cache")
cached_chapters = c.fetchone()[0]
return {
"total_users": total_users, "new_users": new_users,
"weekly_chapters": weekly_chapters, "top_users": top_users,
"top_manga": top_manga, "cached_chapters": cached_chapters,
}
finally:
db_pool.putconn(conn)
def build_weekly_report_text(stats: dict) -> str:
medals = ["π₯","π₯","π₯","4οΈβ£","5οΈβ£"]
top_users_lines = "\n".join(
f"{medals[i]} {('@'+uname) if uname else str(uid)} β *{cnt}* chapters"
for i, (uid, uname, cnt) in enumerate(stats["top_users"])
) or "_No activity this week_"
top_manga_lines = "\n".join(
f"{medals[i]} `{slug}` β *{cnt}* downloads"
for i, (slug, cnt) in enumerate(stats["top_manga"])
) or "_No activity this week_"
now_str = time.strftime("%Y-%m-%d %H:%M UTC", time.gmtime())
return (
f"π *Weekly Bot Report*\n_Generated: {now_str}_\n\n"
f"π₯ Total Registered Users: *{stats['total_users']}*\n"
f"π New Users This Week: *{stats['new_users']}*\n"
f"π Chapters Unlocked This Week: *{stats['weekly_chapters']}*\n"
f"πΎ Cached Chapters: *{stats['cached_chapters']}*\n\n"
f"π *Top 5 Users This Week:*\n{top_users_lines}\n\n"
f"π *Top 5 Manga This Week:*\n{top_manga_lines}"
) |