Spaces:
Sleeping
Sleeping
Download core/db.py from Brainw/UdemyScout: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/spaces/Brainw/UdemyScout/resolve/main/core/db.py
- Command line
-
hf download hf://spaces/Brainw/UdemyScout/core/db.py
-
curl -L -o db.py https://huggingface.co/spaces/Brainw/UdemyScout/resolve/main/core/db.py
3 kB
| import sqlite3 | |
| from pathlib import Path | |
| from datetime import datetime | |
| DB_PATH = Path("data/courses.db") | |
| DB_PATH.parent.mkdir(exist_ok=True) | |
| def get_db_connection(): | |
| conn = sqlite3.connect(DB_PATH) | |
| conn.row_factory = sqlite3.Row | |
| return conn | |
| def init_db(): | |
| conn = get_db_connection() | |
| c = conn.cursor() | |
| c.execute(''' | |
| CREATE TABLE IF NOT EXISTS courses ( | |
| url TEXT PRIMARY KEY, | |
| title TEXT, | |
| category TEXT, | |
| source TEXT, | |
| status TEXT, | |
| seen_at TEXT, | |
| last_checked TEXT, | |
| is_new INTEGER DEFAULT 1 | |
| ) | |
| ''') | |
| conn.commit() | |
| conn.close() | |
| def add_course(course_data): | |
| """ | |
| Upsert course data. | |
| course_data should have: url, title, category, source, status | |
| """ | |
| conn = get_db_connection() | |
| c = conn.cursor() | |
| now = datetime.utcnow().isoformat() | |
| try: | |
| # Check if exists | |
| c.execute("SELECT seen_at FROM courses WHERE url = ?", (course_data['url'],)) | |
| row = c.fetchone() | |
| if row: | |
| # Update last_checked and status | |
| c.execute(''' | |
| UPDATE courses | |
| SET last_checked = ?, status = ?, is_new = 0 | |
| WHERE url = ? | |
| ''', (now, course_data.get('status'), course_data['url'])) | |
| is_new = False | |
| else: | |
| # Insert new | |
| c.execute(''' | |
| INSERT INTO courses (url, title, category, source, status, seen_at, last_checked, is_new) | |
| VALUES (?, ?, ?, ?, ?, ?, ?, 1) | |
| ''', ( | |
| course_data['url'], | |
| course_data['title'], | |
| course_data.get('category', 'General'), | |
| course_data.get('source', 'Unknown'), | |
| course_data.get('status', ''), | |
| now, | |
| now | |
| )) | |
| is_new = True | |
| conn.commit() | |
| except Exception as e: | |
| print(f"DB Error: {e}") | |
| is_new = False | |
| finally: | |
| conn.close() | |
| return is_new | |
| def get_active_courses(limit=100): | |
| conn = get_db_connection() | |
| c = conn.cursor() | |
| # Get most recently checked courses first | |
| c.execute("SELECT * FROM courses ORDER BY last_checked DESC, seen_at DESC LIMIT ?", (limit,)) | |
| rows = c.fetchall() | |
| conn.close() | |
| return [dict(row) for row in rows] | |
| def get_stats(): | |
| conn = get_db_connection() | |
| c = conn.cursor() | |
| # Total courses | |
| c.execute("SELECT COUNT(*) FROM courses") | |
| total = c.fetchone()[0] | |
| # New today (seen_at starts with today's date) | |
| today = datetime.utcnow().strftime("%Y-%m-%d") | |
| c.execute("SELECT COUNT(*) FROM courses WHERE seen_at LIKE ?", (f"{today}%",)) | |
| new_today = c.fetchone()[0] | |
| conn.close() | |
| return { | |
| "total": total, | |
| "new_today": new_today, | |
| "expiring_soon": 0 # Placeholder for now | |
| } | |
| # Initialize on module load | |
| init_db() | |