more minor tweaks, benefit from get_files
This commit is contained in:
parent
9bd61940b8
commit
0b61dd9e42
6 changed files with 28 additions and 31 deletions
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue