From b852e5a3a33cb889b3ddd5cfed97dda30d49723f Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Mon, 27 Jan 2020 23:33:48 +0000 Subject: [PATCH] module for Materialistic app --- my/materialistic.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 my/materialistic.py diff --git a/my/materialistic.py b/my/materialistic.py new file mode 100644 index 0000000..3eac318 --- /dev/null +++ b/my/materialistic.py @@ -0,0 +1,51 @@ +""" +Module for Materialistic app for Hackernews +https://play.google.com/store/apps/details?id=io.github.hidroh.materialistic +""" + +from datetime import datetime +from typing import Any, Dict, Iterator, NamedTuple + +import pytz +import dataset # type: ignore + +from mycfg import paths + + +Row = Dict[str, Any] + +class Saved(NamedTuple): + row: Row + + @property + def when(self) -> datetime: + ts = int(self.row['time']) / 1000 + return datetime.fromtimestamp(ts, tz=pytz.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 f'https://news.ycombinator.com/item?id={self.uid}' + + +def raw() -> Iterator[Row]: + db = dataset.connect('sqlite:///' + str(max(paths.materialistic.export_path.glob('*.db')))) + st = db['saved'] + # TODO wonder if it's 'save time'? + yield from st.all(order_by='time') + + +def saves() -> Iterator[Saved]: + yield from map(Saved, raw())