This repository was archived by the owner on Dec 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyncdatabase.py
More file actions
81 lines (65 loc) · 2.76 KB
/
syncdatabase.py
File metadata and controls
81 lines (65 loc) · 2.76 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
"""
cardStatX - Football Card Data Ingestion System
Author: Samuel Stockstrom
License: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
"""
from typing import Dict
import sqlite3
import logging
import os
logger = logging.getLogger('sync_database')
class SyncCardDatabase:
def __init__(self, db_path: str = "data/cards.db"):
self.db_path = db_path
os.makedirs(os.path.dirname(db_path), exist_ok=True)
def initialize(self):
"""Initialize the database with required tables"""
with sqlite3.connect(self.db_path) as db:
db.execute("""
CREATE TABLE IF NOT EXISTS cards (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
db.execute("""
CREATE TABLE IF NOT EXISTS listings (
id TEXT PRIMARY KEY,
card_id TEXT NOT NULL,
title TEXT NOT NULL,
condition_text TEXT,
price REAL NOT NULL,
currency TEXT DEFAULT 'USD',
listing_date TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (card_id) REFERENCES cards (id)
)
""")
db.execute("""
CREATE INDEX IF NOT EXISTS idx_card_id ON listings(card_id)
""")
db.execute("""
CREATE INDEX IF NOT EXISTS idx_listing_date ON listings(listing_date)
""")
db.commit()
def add_card(self, card_id: str, card_name: str) -> bool:
"""Add a new card or update existing one"""
try:
with sqlite3.connect(self.db_path) as db:
db.execute("""
INSERT OR REPLACE INTO cards (id, name, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
""", (card_id, card_name))
db.commit()
return True
except Exception as e:
logger.error(f"Error adding card {card_id}: {e}")
return False
def get_all_cards(self) -> Dict[str, str]:
"""Get all cards as a dictionary {id: name}"""
with sqlite3.connect(self.db_path) as db:
cursor = db.execute("SELECT id, name FROM cards")
rows = cursor.fetchall()
return {row[0]: row[1] for row in rows}