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():
return get_files(config.export_path, glob='*.json')
return get_files(config.export_path)
def _dal() -> dal.DAL:

View file

@ -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

View file

@ -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')

View file

@ -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

View file

@ -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

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