mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-03 20:13:02 +00:00
set up track logging
+ install flask restful
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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")
|
||||
@@ -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}
|
||||
@@ -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
|
||||
@@ -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
|
||||
)
|
||||
"""
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
from attr import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Track:
|
||||
"""
|
||||
Track play logger model
|
||||
"""
|
||||
trackhash: str
|
||||
duration: int
|
||||
timestamp: int
|
||||
Reference in New Issue
Block a user