Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
274 lines (233 loc) · 8.42 KB
/
Copy pathdatabase.py
File metadata and controls
274 lines (233 loc) · 8.42 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
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
"""
database.py — SQLite database layer for the Substack Finance Scraper.
Provides schema initialization and all query helpers needed by the scraper.
"""
import sqlite3
from typing import Optional
# ---------------------------------------------------------------------------
# Schema
# ---------------------------------------------------------------------------
_DDL = """
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
substack_slug TEXT NOT NULL,
title TEXT,
url TEXT UNIQUE NOT NULL,
published_date TEXT,
body_text TEXT,
is_paywalled INTEGER DEFAULT 0,
scraped_at TEXT DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS ticker_mentions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id INTEGER REFERENCES posts(id),
ticker TEXT NOT NULL,
mention_context TEXT,
match_type TEXT -- 'dollar_sign' or 'context_word'
);
CREATE TABLE IF NOT EXISTS volume_snapshots (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ticker TEXT NOT NULL,
snapshot_date TEXT NOT NULL,
open REAL,
high REAL,
low REAL,
close REAL,
volume INTEGER,
UNIQUE(ticker, snapshot_date)
);
"""
# ---------------------------------------------------------------------------
# Initialization
# ---------------------------------------------------------------------------
def _migrate(conn: sqlite3.Connection) -> None:
"""Add any columns introduced after the initial schema."""
existing = {row[1] for row in conn.execute("PRAGMA table_info(posts)")}
if "is_paywalled" not in existing:
conn.execute("ALTER TABLE posts ADD COLUMN is_paywalled INTEGER DEFAULT 0")
conn.commit()
def init_db(db_path: str = "./substack.db") -> sqlite3.Connection:
"""
Open (or create) the SQLite database at *db_path*, apply the schema, and
return the connection.
The connection is configured with:
- row_factory = sqlite3.Row (so callers can access columns by name)
- WAL journal mode (better concurrent read performance)
- Foreign-key enforcement enabled
"""
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA journal_mode=WAL;")
conn.execute("PRAGMA foreign_keys=ON;")
conn.executescript(_DDL)
_migrate(conn)
conn.commit()
return conn
# ---------------------------------------------------------------------------
# posts helpers
# ---------------------------------------------------------------------------
def insert_post(
conn: sqlite3.Connection,
slug: str,
title: Optional[str],
url: str,
published_date: Optional[str],
body_text: Optional[str],
is_paywalled: bool = False,
) -> int:
"""
Insert a post row and return its *id*.
Uses INSERT OR IGNORE so duplicate URLs are silently skipped. In either
case the existing row's id is returned, making the function idempotent.
"""
conn.execute(
"""
INSERT OR IGNORE INTO posts (substack_slug, title, url, published_date, body_text, is_paywalled)
VALUES (?, ?, ?, ?, ?, ?)
""",
(slug, title, url, published_date, body_text, int(is_paywalled)),
)
conn.commit()
row = conn.execute("SELECT id FROM posts WHERE url = ?", (url,)).fetchone()
return row["id"]
# ---------------------------------------------------------------------------
# ticker_mentions helpers
# ---------------------------------------------------------------------------
def insert_ticker_mention(
conn: sqlite3.Connection,
post_id: int,
ticker: str,
context: Optional[str],
match_type: Optional[str],
) -> None:
"""
Insert a ticker mention linked to *post_id*.
Duplicate mentions (same post + ticker + match_type) are allowed by design
because the same ticker can be mentioned multiple times in one post.
"""
conn.execute(
"""
INSERT INTO ticker_mentions (post_id, ticker, mention_context, match_type)
VALUES (?, ?, ?, ?)
""",
(post_id, ticker, context, match_type),
)
conn.commit()
# ---------------------------------------------------------------------------
# volume_snapshots helpers
# ---------------------------------------------------------------------------
def insert_volume_snapshot(
conn: sqlite3.Connection,
ticker: str,
date: str,
open: Optional[float],
high: Optional[float],
low: Optional[float],
close: Optional[float],
volume: Optional[int],
) -> None:
"""
Insert an OHLCV snapshot for *ticker* on *date*.
The UNIQUE(ticker, snapshot_date) constraint means re-inserting an
existing (ticker, date) pair is silently ignored.
"""
conn.execute(
"""
INSERT OR IGNORE INTO volume_snapshots
(ticker, snapshot_date, open, high, low, close, volume)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(ticker, date, open, high, low, close, volume),
)
conn.commit()
# ---------------------------------------------------------------------------
# Query helpers
# ---------------------------------------------------------------------------
def get_all_tickers(conn: sqlite3.Connection) -> list[str]:
"""Return a sorted list of unique ticker symbols found in ticker_mentions."""
rows = conn.execute(
"SELECT DISTINCT ticker FROM ticker_mentions ORDER BY ticker"
).fetchall()
return [row["ticker"] for row in rows]
def get_ticker_mentions(conn: sqlite3.Connection, ticker: str) -> list[dict]:
"""
Return every mention of *ticker* enriched with post metadata.
Each dict contains:
mention_id, ticker, mention_context, match_type,
post_id, substack_slug, title, url, published_date, scraped_at
"""
rows = conn.execute(
"""
SELECT
tm.id AS mention_id,
tm.ticker,
tm.mention_context,
tm.match_type,
p.id AS post_id,
p.substack_slug,
p.title,
p.url,
p.published_date,
p.scraped_at
FROM ticker_mentions tm
JOIN posts p ON p.id = tm.post_id
WHERE tm.ticker = ?
ORDER BY p.published_date DESC, tm.id
""",
(ticker,),
).fetchall()
return [dict(row) for row in rows]
def get_volume_snapshots(conn: sqlite3.Connection, ticker: str) -> list[dict]:
"""
Return all OHLCV snapshots for *ticker*, ordered by date ascending.
Each dict contains: id, ticker, snapshot_date, open, high, low, close, volume
"""
rows = conn.execute(
"""
SELECT id, ticker, snapshot_date, open, high, low, close, volume
FROM volume_snapshots
WHERE ticker = ?
ORDER BY snapshot_date
""",
(ticker,),
).fetchall()
return [dict(row) for row in rows]
def get_cross_reference_tickers(
conn: sqlite3.Connection,
min_slugs: int = 2,
validated_only: bool = True,
) -> list[dict]:
"""
Return tickers mentioned by at least *min_slugs* different Substack publications.
Args:
min_slugs: Minimum number of distinct publications required.
validated_only: When True (default), restrict to tickers that have at least
one row in volume_snapshots — i.e. yfinance confirmed them as
real, tradeable symbols. This filters out tech-jargon acronyms
that happen to look like tickers (CPO, AWS, OSAT, etc.).
Each dict contains:
ticker, slug_count, slugs (comma-separated), total_mentions
Results are ordered by slug_count DESC, then total_mentions DESC.
"""
market_filter = (
"AND tm.ticker IN (SELECT DISTINCT ticker FROM volume_snapshots)"
if validated_only
else ""
)
rows = conn.execute(
f"""
SELECT
tm.ticker,
COUNT(DISTINCT p.substack_slug) AS slug_count,
GROUP_CONCAT(DISTINCT p.substack_slug) AS slugs,
COUNT(*) AS total_mentions
FROM ticker_mentions tm
JOIN posts p ON p.id = tm.post_id
{market_filter}
GROUP BY tm.ticker
HAVING COUNT(DISTINCT p.substack_slug) >= ?
ORDER BY slug_count DESC, total_mentions DESC
""",
(min_slugs,),
).fetchall()
return [dict(row) for row in rows]