-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
38 lines (33 loc) · 1.22 KB
/
storage.py
File metadata and controls
38 lines (33 loc) · 1.22 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
\
import sqlite3, time, os
SCHEMA = """
CREATE TABLE IF NOT EXISTS items(
id INTEGER PRIMARY KEY,
fingerprint TEXT UNIQUE,
source TEXT,
title TEXT,
url TEXT,
published_at INTEGER,
symbols TEXT,
tags TEXT
);
CREATE INDEX IF NOT EXISTS idx_published ON items(published_at);
"""
class Storage:
def __init__(self, path: str):
self.conn = sqlite3.connect(path)
self.conn.execute("PRAGMA journal_mode=WAL;")
self.conn.executescript(SCHEMA)
def seen(self, fingerprint: str) -> bool:
cur = self.conn.execute("SELECT 1 FROM items WHERE fingerprint=?", (fingerprint,))
return cur.fetchone() is not None
def add(self, fingerprint: str, source: str, title: str, url: str, published_at: int, symbols: str, tags: str):
self.conn.execute(
"INSERT OR IGNORE INTO items(fingerprint, source, title, url, published_at, symbols, tags) VALUES(?,?,?,?,?,?,?)",
(fingerprint, source, title, url, published_at, symbols, tags),
)
self.conn.commit()
def purge_old(self, keep_seconds: int):
cutoff = int(time.time()) - keep_seconds
self.conn.execute("DELETE FROM items WHERE published_at < ?", (cutoff,))
self.conn.commit()