handle favorites on recently played

+ use client side timestamp on track logger
This commit is contained in:
mungai-njoroge
2023-12-10 13:27:54 +03:00
parent 312df06fc3
commit 4412902312
6 changed files with 44 additions and 22 deletions
+2 -3
View File
@@ -3,6 +3,7 @@ from app.db.sqlite.logger.tracks import SQLiteTrackLogger as db
parser = reqparse.RequestParser() parser = reqparse.RequestParser()
parser.add_argument("trackhash", type=str, required=True) parser.add_argument("trackhash", type=str, required=True)
parser.add_argument("timestamp", type=int, required=True)
parser.add_argument("duration", type=int, required=True) parser.add_argument("duration", type=int, required=True)
parser.add_argument("source", type=str, required=True) parser.add_argument("source", type=str, required=True)
@@ -12,9 +13,7 @@ class LogTrack(Resource):
args = parser.parse_args(strict=True) args = parser.parse_args(strict=True)
last_row = db.insert_track( last_row = db.insert_track(
args["trackhash"], args["trackhash"], args["duration"], args["source"], args["timestamp"]
args["duration"],
args["source"],
) )
return {"last_row": last_row} return {"last_row": last_row}
+13
View File
@@ -86,3 +86,16 @@ class SQLiteFavoriteMethods:
with SQLiteManager(userdata_db=True) as cur: with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql, (fav_hash, fav_type)) cur.execute(sql, (fav_hash, fav_type))
cur.close() cur.close()
@classmethod
def get_track_count(cls) -> int:
"""
Returns the number of favorite tracks.
"""
sql = """SELECT COUNT(*) FROM favorites WHERE type = ?"""
with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql, (FavType.track,))
count = cur.fetchone()[0]
cur.close()
return count
+1 -3
View File
@@ -1,10 +1,9 @@
from app.db.sqlite.utils import SQLiteManager from app.db.sqlite.utils import SQLiteManager
import time
class SQLiteTrackLogger: class SQLiteTrackLogger:
@classmethod @classmethod
def insert_track(cls, trackhash: str, duration: int, source: str): def insert_track(cls, trackhash: str, duration: int, source: str, timestamp: int):
""" """
Inserts a track into the database Inserts a track into the database
""" """
@@ -18,7 +17,6 @@ class SQLiteTrackLogger:
userid userid
) VALUES(?,?,?,?,?) ) VALUES(?,?,?,?,?)
""" """
timestamp = int(time.time())
cur.execute(sql, (trackhash, duration, timestamp, source, 0)) cur.execute(sql, (trackhash, duration, timestamp, source, 0))
lastrowid = cur.lastrowid lastrowid = cur.lastrowid
+23 -13
View File
@@ -3,6 +3,7 @@ from app.models.logger import Track as TrackLog
from app.db.sqlite.logger.tracks import SQLiteTrackLogger as db from app.db.sqlite.logger.tracks import SQLiteTrackLogger as db
from app.db.sqlite.playlists import SQLitePlaylistMethods as pdb from app.db.sqlite.playlists import SQLitePlaylistMethods as pdb
from app.db.sqlite.favorite import SQLiteFavoriteMethods as fdb
from app.serializers.track import serialize_track from app.serializers.track import serialize_track
from app.serializers.album import album_serializer from app.serializers.album import album_serializer
@@ -66,19 +67,6 @@ def get_recently_played(limit=7):
continue continue
if entry.type == "track":
try:
track = TrackStore.get_tracks_by_trackhashes([entry.trackhash])[0]
except IndexError:
continue
track = serialize_track(track)
track["help_text"] = "track"
items.append({"type": "track", "item": track})
continue
if entry.type == "folder": if entry.type == "folder":
folder = entry.type_src folder = entry.type_src
if not folder: if not folder:
@@ -141,4 +129,26 @@ def get_recently_played(limit=7):
} }
) )
if entry.type == "favorite":
items.append(
{
"type": "favorite_tracks",
"item": {
"help_text": "playlist",
"count": fdb.get_track_count(),
},
}
)
continue
try:
track = TrackStore.get_tracks_by_trackhashes([entry.trackhash])[0]
except IndexError:
continue
track = serialize_track(track)
track["help_text"] = "track"
items.append({"type": "track", "item": track})
return items return items
+2 -2
View File
@@ -15,7 +15,7 @@ from app.models.playlist import Playlist
from app.models.track import Track from app.models.track import Track
from app.store.albums import AlbumStore from app.store.albums import AlbumStore
from app.store.tracks import TrackStore from app.store.tracks import TrackStore
from app.utils.dates import create_new_date from app.utils.dates import create_new_date, date_string_to_time_passed
def create_thumbnail(image: Any, img_path: str) -> str: def create_thumbnail(image: Any, img_path: str) -> str:
@@ -146,7 +146,7 @@ def get_recently_added_playlist(cutoff: int = 14):
tracks = get_recent_tracks(cutoff) tracks = get_recent_tracks(cutoff)
date = datetime.fromtimestamp(tracks[0].created_date) date = datetime.fromtimestamp(tracks[0].created_date)
playlist.last_updated = create_new_date(date) playlist.last_updated = date_string_to_time_passed(create_new_date(date))
images = get_first_4_images(tracks=tracks) images = get_first_4_images(tracks=tracks)
playlist.images = images playlist.images = images
+3 -1
View File
@@ -24,6 +24,7 @@ class Track:
"ar:": "artist", "ar:": "artist",
"pl:": "playlist", "pl:": "playlist",
"fo:": "folder", "fo:": "folder",
"favorite": "favorite",
} }
for prefix, srctype in prefix_map.items(): for prefix, srctype in prefix_map.items():
@@ -31,6 +32,7 @@ class Track:
try: try:
self.type_src = self.source.split(":", 1)[1] self.type_src = self.source.split(":", 1)[1]
except IndexError: except IndexError:
pass self.type_src = None
self.type = srctype self.type = srctype
break break