37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
import sqlite3
|
|
from hashlib import sha256
|
|
|
|
DEFAULT_DB_PATH = 'output/seen_issues.db'
|
|
|
|
|
|
class SeenIssuesDB:
|
|
def __init__(self, db_path=DEFAULT_DB_PATH):
|
|
self.conn = sqlite3.connect(db_path)
|
|
self._create_table()
|
|
|
|
def _create_table(self):
|
|
with self.conn:
|
|
self.conn.execute("""
|
|
CREATE TABLE IF NOT EXISTS seen_issues (
|
|
issue_hash TEXT PRIMARY KEY
|
|
)
|
|
""")
|
|
|
|
def mark_as_seen(self, issue_text: str):
|
|
issue_hash = self._compute_hash(issue_text)
|
|
with self.conn:
|
|
self.conn.execute(
|
|
'INSERT OR IGNORE INTO seen_issues (issue_hash) VALUES (?)',
|
|
(issue_hash,),
|
|
)
|
|
|
|
def has_seen(self, issue_text: str) -> bool:
|
|
issue_hash = self._compute_hash(issue_text)
|
|
cursor = self.conn.execute(
|
|
'SELECT 1 FROM seen_issues WHERE issue_hash = ?', (issue_hash,)
|
|
)
|
|
return cursor.fetchone() is not None
|
|
|
|
def _compute_hash(self, text: str) -> str:
|
|
return sha256(text.encode('utf-8')).hexdigest()
|