store playcount and duration on the track table

+ allow sorting all items with those two
+ add methods to update scrobble info
This commit is contained in:
cwilvx
2024-06-30 19:33:13 +03:00
parent 4a9f804e70
commit b9ad07441a
15 changed files with 161 additions and 45 deletions
+2 -2
View File
@@ -26,7 +26,7 @@ from app.api import (
settings,
lyrics,
plugins,
logger,
scrobble,
home,
getall,
auth,
@@ -116,7 +116,7 @@ def create_api():
app.register_api(lyrics_plugin.api)
# Logger
app.register_api(logger.api)
app.register_api(scrobble.api)
# Home
app.register_api(home.api)
+25 -1
View File
@@ -18,6 +18,7 @@ from app.utils.dates import (
create_new_date,
date_string_to_time_passed,
seconds_to_time_string,
timestamp_to_time_passed,
)
bp_tag = Tag(name="Get all", description="List all items")
@@ -57,6 +58,13 @@ def get_all_items(path: GetAllItemsPath, query: GetAllItemsQuery):
Get all items
Used to show all albums or artists in the library
Sort keys:
-
Both albums and artists: `duration`, `created_date`, `playcount`, `playduration`, `lastplayed`, `trackcount`
Albums only: `title`, `albumartists`, `date`
Artists only: `name`, `albumcount`
"""
is_albums = path.itemtype == "albums"
is_artists = path.itemtype == "artists"
@@ -76,6 +84,9 @@ def get_all_items(path: GetAllItemsPath, query: GetAllItemsQuery):
sort_is_count = sort == "trackcount"
sort_is_duration = sort == "duration"
sort_is_create_date = sort == "created_date"
sort_is_playcount = sort == "playcount"
sort_is_playduration = sort == "playduration"
sort_is_lastplayed = sort == "lastplayed"
sort_is_date = is_albums and sort == "date"
sort_is_artist = is_albums and sort == "albumartists"
@@ -94,7 +105,6 @@ def get_all_items(path: GetAllItemsPath, query: GetAllItemsQuery):
for item in items:
item_dict = serialize_album(item) if is_albums else serialize_artist(item)
print(item_dict)
if sort_is_date:
item_dict["help_text"] = item.date
@@ -122,6 +132,20 @@ def get_all_items(path: GetAllItemsPath, query: GetAllItemsQuery):
f"{format_number(item.albumcount)} album{'' if item.albumcount == 1 else 's'}"
)
if sort_is_playcount:
item_dict["help_text"] = (
f"{format_number(item.playcount)} play{'' if item.playcount == 1 else 's'}"
)
if sort_is_lastplayed:
if item.playduration == 0:
item_dict["help_text"] = "Never played"
else:
item_dict["help_text"] = timestamp_to_time_passed(item.lastplayed)
if sort_is_playduration:
item_dict["help_text"] = seconds_to_time_string(item.playduration)
album_list.append(item_dict)
return {"items": album_list, "total": total}
@@ -3,7 +3,8 @@ from flask_openapi3 import APIBlueprint
from pydantic import Field
from app.api.apischemas import TrackHashSchema
from app.db.sqlite.logger.tracks import SQLiteTrackLogger as db
from app.db.libdata import AlbumTable, ArtistTable, TrackTable
from app.db.userdata import ScrobbleTable
from app.settings import Defaults
bp_tag = Tag(name="Logger", description="Log item plays")
@@ -26,19 +27,20 @@ def log_track(body: LogTrackBody):
"""
Log a track play to the database.
"""
trackhash = body.trackhash
timestamp = body.timestamp
duration = body.duration
source = body.source
if not timestamp or duration < 5:
return {"msg": "Invalid entry."}, 400
last_row = db.insert_track(
trackhash=trackhash,
timestamp=timestamp,
duration=duration,
source=source,
)
track = TrackTable.get_track_by_trackhash(body.trackhash)
return {"total entries": last_row}
if track is None:
return {"msg": "Track not found."}, 404
ScrobbleTable.add(dict(body))
TrackTable.increment_playcount(body.trackhash, duration, timestamp)
AlbumTable.increment_playcount(track.albumhash, duration, timestamp)
ArtistTable.increment_playcount(track.artisthashes, duration, timestamp)
return {"msg": "recorded"}, 201