set up track logging

+ install flask restful
This commit is contained in:
mungai-njoroge 2023-12-01 10:55:07 +03:00
parent 8b40792ba0
commit 7749b4fc3c
8 changed files with 128 additions and 5 deletions

View File

@ -20,6 +20,7 @@ from app.api import (
settings,
lyrics,
plugins,
logger,
)
@ -52,4 +53,7 @@ def create_api():
app.register_blueprint(plugins.api)
app.register_blueprint(lyrics_plugin.api)
# Logger
app.register_blueprint(logger.api_bp)
return app

View File

@ -0,0 +1,11 @@
from flask import Blueprint, request
from flask_restful import Api
from app.api.logger.tracks import LogTrack
api_bp = Blueprint("logger", __name__, url_prefix="/logger")
api = Api(api_bp)
api.add_resource(LogTrack, "/track/log")

20
app/api/logger/tracks.py Normal file
View File

@ -0,0 +1,20 @@
from flask_restful import Resource, reqparse
from app.db.sqlite.logger.tracks import SQLiteTrackLogger as db
parser = reqparse.RequestParser()
parser.add_argument("trackhash", type=str, required=True)
parser.add_argument("duration", type=int, required=True)
parser.add_argument("source", type=str, required=True)
class LogTrack(Resource):
def post(self):
args = parser.parse_args(strict=True)
last_row = db.insert_track(
args["trackhash"],
args["duration"],
args["source"],
)
return {"last_row": last_row}

View File

@ -0,0 +1,26 @@
from app.db.sqlite.utils import SQLiteManager
import time
class SQLiteTrackLogger:
@classmethod
def insert_track(cls, trackhash: str, duration: int, source: str):
"""
Inserts a track into the database
"""
with SQLiteManager(userdata_db=True) as cur:
sql = """INSERT OR REPLACE INTO track_logger(
trackhash,
duration,
timestamp,
source,
userid
) VALUES(?,?,?,?,?)
"""
timestamp = int(time.time())
cur.execute(sql, (trackhash, duration, timestamp, source, 0))
lastrowid = cur.lastrowid
return lastrowid

View File

@ -2,10 +2,6 @@
This file contains the SQL queries to create the database tables.
"""
# banner_pos integer NOT NULL,
# has_gif integer,
CREATE_USERDATA_TABLES = """
CREATE TABLE IF NOT EXISTS playlists (
id integer PRIMARY KEY,
@ -48,6 +44,15 @@ CREATE TABLE IF NOT EXISTS plugins (
description text NOT NULL,
active integer NOT NULL DEFAULT 0,
settings text
);
CREATE TABLE IF NOT EXISTS track_logger (
id integer PRIMARY KEY,
trackhash text NOT NULL,
duration integer NOT NULL,
timestamp integer NOT NULL,
source text,
userid integer NOT NULL DEFAULT 0
)
"""

11
app/models/logger.py Normal file
View File

@ -0,0 +1,11 @@
from attr import dataclass
@dataclass
class Track:
"""
Track play logger model
"""
trackhash: str
duration: int
timestamp: int

47
poetry.lock generated
View File

@ -11,6 +11,20 @@ files = [
{file = "altgraph-0.17.4.tar.gz", hash = "sha256:1b5afbb98f6c4dcadb2e2ae6ab9fa994bbb8c1d75f4fa96d340f9437ae454406"},
]
[[package]]
name = "aniso8601"
version = "9.0.1"
description = "A library for parsing ISO 8601 strings."
optional = false
python-versions = "*"
files = [
{file = "aniso8601-9.0.1-py2.py3-none-any.whl", hash = "sha256:1d2b7ef82963909e93c4f24ce48d4de9e66009a21bf1c1e1c85bdd0812fe412f"},
{file = "aniso8601-9.0.1.tar.gz", hash = "sha256:72e3117667eedf66951bb2d93f4296a56b94b078a8a95905a052611fb3f1b973"},
]
[package.extras]
dev = ["black", "coverage", "isort", "pre-commit", "pyenchant", "pylint"]
[[package]]
name = "astroid"
version = "2.15.8"
@ -518,6 +532,26 @@ files = [
Flask = ">=0.9"
Six = "*"
[[package]]
name = "flask-restful"
version = "0.3.10"
description = "Simple framework for creating REST APIs"
optional = false
python-versions = "*"
files = [
{file = "Flask-RESTful-0.3.10.tar.gz", hash = "sha256:fe4af2ef0027df8f9b4f797aba20c5566801b6ade995ac63b588abf1a59cec37"},
{file = "Flask_RESTful-0.3.10-py2.py3-none-any.whl", hash = "sha256:1cf93c535172f112e080b0d4503a8d15f93a48c88bdd36dd87269bdaf405051b"},
]
[package.dependencies]
aniso8601 = ">=0.82"
Flask = ">=0.8"
pytz = "*"
six = ">=1.3.0"
[package.extras]
docs = ["sphinx"]
[[package]]
name = "gunicorn"
version = "20.1.0"
@ -1098,6 +1132,17 @@ files = [
[package.dependencies]
six = ">=1.5"
[[package]]
name = "pytz"
version = "2023.3.post1"
description = "World timezone definitions, modern and historical"
optional = false
python-versions = "*"
files = [
{file = "pytz-2023.3.post1-py2.py3-none-any.whl", hash = "sha256:ce42d816b81b68506614c11e8937d3aa9e41007ceb50bfdcb0749b921bf646c7"},
{file = "pytz-2023.3.post1.tar.gz", hash = "sha256:7b4fddbeb94a1eba4b557da24f19fdf9db575192544270a9101d8509f9f43d7b"},
]
[[package]]
name = "pytzdata"
version = "2020.1"
@ -1700,4 +1745,4 @@ files = [
[metadata]
lock-version = "2.0"
python-versions = ">=3.10,<3.12"
content-hash = "6b0eebfb7c29b88c87c31f6efc13229d17148c9643b6d9e37576e5a23e6c967c"
content-hash = "2d6fc44b7cc1c0daac215059d3fbd2dc0a2986df3242628b6125af283d9468c5"

View File

@ -23,6 +23,7 @@ pendulum = "^2.1.2"
flask-compress = "^1.13"
tabulate = "^0.9.0"
setproctitle = "^1.3.2"
flask-restful = "^0.3.10"
[tool.poetry.dev-dependencies]
pylint = "^2.15.5"