From 16e096c494f0c7e4d51785674400a7dc5ed75095 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 24 Jun 2018 14:46:13 +0100 Subject: [PATCH 1/4] initial --- .gitignore | 170 +++++++++++++++++++++++++++++++++++++++++++++ ci.sh | 9 +++ lastfm/__init__.py | 0 3 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100755 ci.sh create mode 100644 lastfm/__init__.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..07d89cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,170 @@ + +# Created by https://www.gitignore.io/api/python,emacs + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile +projectile-bookmarks.eld + +# directory configuration +.dir-locals.el + +# saveplace +places + +# url cache +url/cache/ + +# cedet +ede-projects.el + +# smex +smex-items + +# company-statistics +company-statistics-cache.el + +# anaconda-mode +anaconda-mode/ + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +.pytest_cache/ +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule.* + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + + +# End of https://www.gitignore.io/api/python,emacs diff --git a/ci.sh b/ci.sh new file mode 100755 index 0000000..c186243 --- /dev/null +++ b/ci.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +cd "$(this_dir)" || exit + +. ~/bash_ci + +ci_run python3.6 test.py + +ci_report_errors diff --git a/lastfm/__init__.py b/lastfm/__init__.py new file mode 100644 index 0000000..e69de29 From 76814f7f0649515dbc260a8772f75ad0c4e65c0b Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 24 Jun 2018 15:11:26 +0100 Subject: [PATCH 2/4] initial.. --- lastfm/__init__.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lastfm/__init__.py b/lastfm/__init__.py index e69de29..0aed269 100644 --- a/lastfm/__init__.py +++ b/lastfm/__init__.py @@ -0,0 +1,34 @@ +from functools import lru_cache +from kython import listdir_abs, json_load, JSONType +from typing import Dict, List, NamedTuple +from pytz import UTC +from datetime import datetime +import os + + +_PATH = "/L/backups/lastfm" + +class Scrobble(NamedTuple): + dt: datetime + track: str + + +# TODO memoise...? + +# TODO watch out, if we keep the app running it might expire +def _iter_scrobbles(): + last = max(listdir_abs(_PATH)) + # TODO mm, no timezone? wonder if it's UTC... + j: List[Dict[str, str]] + with open(last, 'r') as fo: + j = json_load(fo) + for d in j: + ts = int(d['date']) + dt = datetime.fromtimestamp(ts, tz=UTC) + track = f"{d['artist']} — {d['name']}" + yield Scrobble(dt, track) + + +@lru_cache() +def get_scrobbles(): + return list(_iter_scrobbles()) From 351528bb64e126802a5f16183569e86d95c3ec03 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Wed, 8 May 2019 20:54:37 +0100 Subject: [PATCH 3/4] simplify provider --- ci.sh | 9 --------- fill_influxdb.py | 16 ++++++++++++++++ lastfm/__init__.py | 23 ++++++++++++++--------- 3 files changed, 30 insertions(+), 18 deletions(-) delete mode 100755 ci.sh create mode 100755 fill_influxdb.py mode change 100644 => 100755 lastfm/__init__.py diff --git a/ci.sh b/ci.sh deleted file mode 100755 index c186243..0000000 --- a/ci.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -cd "$(this_dir)" || exit - -. ~/bash_ci - -ci_run python3.6 test.py - -ci_report_errors diff --git a/fill_influxdb.py b/fill_influxdb.py new file mode 100755 index 0000000..a27399b --- /dev/null +++ b/fill_influxdb.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +from influxdb import InfluxDBClient # type: ignore +from my.lastfm import get_scrobbles + + +def main(): + scrobbles = get_scrobbles() + client = InfluxDBClient() + # TODO client.create_database('lastfm') + + jsons = [{"measurement": 'scrobble', "tags": {}, "time": str(sc.dt), "fields": {"name": sc.track}} for sc in scrobbles] + client.write_points(jsons, database='lastfm') + + +if __name__ == '__main__': + main() diff --git a/lastfm/__init__.py b/lastfm/__init__.py old mode 100644 new mode 100755 index 0aed269..dc0a27f --- a/lastfm/__init__.py +++ b/lastfm/__init__.py @@ -1,12 +1,14 @@ +#!/usr/bin/env python3 from functools import lru_cache -from kython import listdir_abs, json_load, JSONType from typing import Dict, List, NamedTuple -from pytz import UTC from datetime import datetime +from pathlib import Path import os +import json +import pytz -_PATH = "/L/backups/lastfm" +_PATH = Path("/L/backups/lastfm") class Scrobble(NamedTuple): dt: datetime @@ -17,18 +19,21 @@ class Scrobble(NamedTuple): # TODO watch out, if we keep the app running it might expire def _iter_scrobbles(): - last = max(listdir_abs(_PATH)) + last = max(_PATH.glob('*.json')) # TODO mm, no timezone? wonder if it's UTC... - j: List[Dict[str, str]] - with open(last, 'r') as fo: - j = json_load(fo) + j = json.loads(last.read_text()) + for d in j: ts = int(d['date']) - dt = datetime.fromtimestamp(ts, tz=UTC) + dt = datetime.fromtimestamp(ts, tz=pytz.utc) track = f"{d['artist']} — {d['name']}" yield Scrobble(dt, track) -@lru_cache() +@lru_cache(1) def get_scrobbles(): return list(_iter_scrobbles()) + + +def test(): + assert len(get_scrobbles()) > 1000 From 1ce6a9d8d1a256ce9fbe98900dce5bfa7b227cb1 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Mon, 10 Jun 2019 08:28:07 +0100 Subject: [PATCH 4/4] add pip install --- fill_influxdb.py | 1 + 1 file changed, 1 insertion(+) diff --git a/fill_influxdb.py b/fill_influxdb.py index a27399b..c20e39f 100755 --- a/fill_influxdb.py +++ b/fill_influxdb.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# pip install influxdb from influxdb import InfluxDBClient # type: ignore from my.lastfm import get_scrobbles