more minor tweaks, benefit from get_files

This commit is contained in:
Dima Gerasimov 2020-05-03 17:15:51 +01:00
parent 9bd61940b8
commit 0b61dd9e42
6 changed files with 28 additions and 31 deletions

View file

@ -13,7 +13,7 @@ Bookmark = dal.Bookmark
def inputs(): def inputs():
return get_files(config.export_path, glob='*.json') return get_files(config.export_path)
def _dal() -> dal.DAL: def _dal() -> dal.DAL:

View file

@ -2,27 +2,31 @@
Last.fm scrobbles Last.fm scrobbles
''' '''
from .. import init
from functools import lru_cache from ..common import get_files, mcachew, Json
from typing import NamedTuple, Dict, Any
from datetime import datetime from datetime import datetime
from pathlib import Path
import json import json
from pathlib import Path
from typing import NamedTuple, Any, Sequence, Iterable
import pytz import pytz
from my.config import lastfm as config from my.config import lastfm as config
# TODO Json type?
# TODO memoised properties? # TODO memoised properties?
# TODO lazy mode and eager mode? # TODO lazy mode and eager mode?
# lazy is a bit nicer in terms of more flexibility and less processing? # lazy is a bit nicer in terms of more flexibility and less processing?
# eager is a bit more explicit for error handling # eager is a bit more explicit for error handling
class Scrobble(NamedTuple): def inputs() -> Sequence[Path]:
raw: Dict[str, Any] return get_files(config.export_path)
class Scrobble(NamedTuple):
raw: Json
# TODO mm, no timezone? hopefuly it's UTC
@property @property
def dt(self) -> datetime: def dt(self) -> datetime:
ts = int(self.raw['date']) 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 could also be nice to make generic? maybe even depending on eagerness
# TODO memoise...? @mcachew(hashf=lambda: inputs())
# TODO watch out, if we keep the app running it might expire def scrobbles() -> Iterable[Scrobble]:
def _iter_scrobbles(): last = max(inputs())
# TODO use get_files
last = max(Path(config.export_path).glob('*.json'))
# TODO mm, no timezone? hopefuly it's UTC
j = json.loads(last.read_text()) j = json.loads(last.read_text())
for raw in j: for raw in j:
yield Scrobble(raw=raw) 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

View file

@ -1,11 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# pip install influxdb # pip install influxdb
from influxdb import InfluxDBClient # type: ignore from influxdb import InfluxDBClient # type: ignore
from my.lastfm import get_scrobbles from my.lastfm import scrobbles
def main(): def main() -> None:
scrobbles = get_scrobbles() scrobbles = scrobbles()
client = InfluxDBClient() client = InfluxDBClient()
# TODO client.create_database('lastfm') # TODO client.create_database('lastfm')

View file

@ -8,7 +8,6 @@ from typing import List, Dict, Iterator, NamedTuple, Sequence, Optional
import json import json
import pytz import pytz
# TODO declare DEPENDS = [pytz??]
from ..common import LazyLogger, get_files from ..common import LazyLogger, get_files

View file

@ -1,6 +1,5 @@
from my.instapaper import get_todos from my.instapaper import pages
def test_get_todos(): def test_pages():
for t in get_todos(): assert len(list(pages())) > 3
print(t)

7
tests/lastfm.py Normal file
View file

@ -0,0 +1,7 @@
from more_itertools import ilen
from my.lastfm import scrobbles
def test():
assert ilen(scrobbles()) > 1000