From 0b61dd9e42c2c9a74915995a383bd4b952edaff6 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 3 May 2020 17:15:51 +0100 Subject: [PATCH] more minor tweaks, benefit from get_files --- my/instapaper.py | 2 +- my/lastfm/__init__.py | 36 ++++++++++++++---------------------- my/lastfm/fill_influxdb.py | 6 +++--- my/reading/polar.py | 1 - tests/instapaper.py | 7 +++---- tests/lastfm.py | 7 +++++++ 6 files changed, 28 insertions(+), 31 deletions(-) create mode 100644 tests/lastfm.py diff --git a/my/instapaper.py b/my/instapaper.py index 1564be7..364c402 100644 --- a/my/instapaper.py +++ b/my/instapaper.py @@ -13,7 +13,7 @@ Bookmark = dal.Bookmark def inputs(): - return get_files(config.export_path, glob='*.json') + return get_files(config.export_path) def _dal() -> dal.DAL: diff --git a/my/lastfm/__init__.py b/my/lastfm/__init__.py index 12239b0..d55fef4 100755 --- a/my/lastfm/__init__.py +++ b/my/lastfm/__init__.py @@ -2,27 +2,31 @@ Last.fm scrobbles ''' -from .. import init -from functools import lru_cache -from typing import NamedTuple, Dict, Any +from ..common import get_files, mcachew, Json + from datetime import datetime -from pathlib import Path import json +from pathlib import Path +from typing import NamedTuple, Any, Sequence, Iterable import pytz from my.config import lastfm as config -# TODO Json type? # TODO memoised properties? # TODO lazy mode and eager mode? # lazy is a bit nicer in terms of more flexibility and less processing? # eager is a bit more explicit for error handling -class Scrobble(NamedTuple): - raw: Dict[str, Any] +def inputs() -> Sequence[Path]: + return get_files(config.export_path) + +class Scrobble(NamedTuple): + raw: Json + + # TODO mm, no timezone? hopefuly it's UTC @property def dt(self) -> datetime: ts = int(self.raw['date']) @@ -45,22 +49,10 @@ class Scrobble(NamedTuple): # TODO could also be nice to make generic? maybe even depending on eagerness -# TODO memoise...? -# TODO watch out, if we keep the app running it might expire -def _iter_scrobbles(): - # TODO use get_files - last = max(Path(config.export_path).glob('*.json')) - # TODO mm, no timezone? hopefuly it's UTC +@mcachew(hashf=lambda: inputs()) +def scrobbles() -> Iterable[Scrobble]: + last = max(inputs()) j = json.loads(last.read_text()) for raw in j: yield Scrobble(raw=raw) - - -@lru_cache(1) -def get_scrobbles(): - return list(sorted(_iter_scrobbles(), key=lambda s: s.dt)) - - -def test(): - assert len(get_scrobbles()) > 1000 diff --git a/my/lastfm/fill_influxdb.py b/my/lastfm/fill_influxdb.py index c20e39f..7754760 100755 --- a/my/lastfm/fill_influxdb.py +++ b/my/lastfm/fill_influxdb.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 # pip install influxdb from influxdb import InfluxDBClient # type: ignore -from my.lastfm import get_scrobbles +from my.lastfm import scrobbles -def main(): - scrobbles = get_scrobbles() +def main() -> None: + scrobbles = scrobbles() client = InfluxDBClient() # TODO client.create_database('lastfm') diff --git a/my/reading/polar.py b/my/reading/polar.py index 9eb4783..d2b2d60 100755 --- a/my/reading/polar.py +++ b/my/reading/polar.py @@ -8,7 +8,6 @@ from typing import List, Dict, Iterator, NamedTuple, Sequence, Optional import json import pytz -# TODO declare DEPENDS = [pytz??] from ..common import LazyLogger, get_files diff --git a/tests/instapaper.py b/tests/instapaper.py index 2c492e8..ae685e6 100644 --- a/tests/instapaper.py +++ b/tests/instapaper.py @@ -1,6 +1,5 @@ -from my.instapaper import get_todos +from my.instapaper import pages -def test_get_todos(): - for t in get_todos(): - print(t) +def test_pages(): + assert len(list(pages())) > 3 diff --git a/tests/lastfm.py b/tests/lastfm.py new file mode 100644 index 0000000..e94c3c5 --- /dev/null +++ b/tests/lastfm.py @@ -0,0 +1,7 @@ +from more_itertools import ilen + +from my.lastfm import scrobbles + + +def test(): + assert ilen(scrobbles()) > 1000