HPI/my/hackernews/materialistic.py
Dima Gerasimov 5c82d0faa9 switch from using dataset to raw sqlite3 module
dataset is kinda unmaintaned and currently broken due to sqlalchemy 2.0 changes

resolves https://github.com/karlicoss/HPI/issues/264
2023-02-07 01:57:00 +00:00

55 lines
1.4 KiB
Python

"""
[[https://play.google.com/store/apps/details?id=io.github.hidroh.materialistic][Materialistic]] app for Hackernews
"""
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, Iterator, NamedTuple, Sequence
from my.core import get_files
from my.core.sqlite import sqlite_connection
from my.config import materialistic as config
# todo migrate config to my.hackernews.materialistic
def inputs() -> Sequence[Path]:
return get_files(config.export_path)
Row = Dict[str, Any]
from .common import hackernews_link
class Saved(NamedTuple):
row: Row
@property
def when(self) -> datetime:
ts = int(self.row['time']) / 1000
return datetime.fromtimestamp(ts, tz=timezone.utc)
@property
def uid(self) -> str:
return self.row['itemid']
@property
def url(self) -> str:
return self.row['url']
@property
def title(self) -> str:
return self.row['title']
@property
def hackernews_link(self) -> str:
return hackernews_link(self.uid)
def raw() -> Iterator[Row]:
last = max(inputs())
with sqlite_connection(last, immutable=True, row_factory='dict') as conn:
yield from conn.execute('SELECT * FROM saved ORDER BY time')
# TODO wonder if it's 'save time' or creation time?
def saves() -> Iterator[Saved]:
yield from map(Saved, raw())