mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 12:33:03 +00:00
add methods to get recently played items
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
from dataclasses import asdict
|
||||
|
||||
from tomlkit import item
|
||||
|
||||
from app.db.sqlite.logger.tracks import SQLiteTrackLogger as db
|
||||
from app.db.sqlite.playlists import SQLitePlaylistMethods as pdb
|
||||
from app.lib.playlistlib import get_first_4_images, get_recently_added_playlist
|
||||
from app.models.logger import Track as TrackLog
|
||||
from app.serializers.album import album_serializer
|
||||
from app.serializers.artist import serialize_for_card
|
||||
from app.serializers.playlist import serialize_for_card as serialize_playlist
|
||||
from app.serializers.track import serialize_track
|
||||
from app.store.albums import AlbumStore
|
||||
from app.store.artists import ArtistStore
|
||||
from app.store.tracks import TrackStore
|
||||
|
||||
|
||||
def get_recently_played(limit=7):
|
||||
entries = db.get_all()
|
||||
items = []
|
||||
added = set()
|
||||
|
||||
for entry in entries:
|
||||
if len(items) >= limit:
|
||||
break
|
||||
|
||||
entry = TrackLog(*entry)
|
||||
|
||||
if entry.source in added:
|
||||
continue
|
||||
|
||||
added.add(entry.source)
|
||||
|
||||
if entry.type == "album":
|
||||
album = AlbumStore.get_album_by_hash(entry.type_src)
|
||||
|
||||
if album is None:
|
||||
continue
|
||||
|
||||
album = album_serializer(
|
||||
album,
|
||||
{
|
||||
"genres",
|
||||
"date",
|
||||
"count",
|
||||
"duration",
|
||||
"albumartists_hashes",
|
||||
"og_title",
|
||||
},
|
||||
)
|
||||
|
||||
items.append({"type": "album", "item": album})
|
||||
continue
|
||||
|
||||
if entry.type == "artist":
|
||||
artist = ArtistStore.get_artist_by_hash(entry.type_src)
|
||||
|
||||
if artist is None:
|
||||
continue
|
||||
|
||||
artist = serialize_for_card(artist)
|
||||
|
||||
items.append({"type": "artist", "item": artist})
|
||||
|
||||
continue
|
||||
|
||||
if entry.type == "track":
|
||||
try:
|
||||
track = TrackStore.get_tracks_by_trackhashes([entry.trackhash])[0]
|
||||
except IndexError:
|
||||
continue
|
||||
|
||||
track = serialize_track(track)
|
||||
|
||||
items.append({"type": "track", "item": track})
|
||||
|
||||
continue
|
||||
|
||||
if entry.type == "folder":
|
||||
count = len([t for t in TrackStore.tracks if t.folder == entry.type_src])
|
||||
items.append(
|
||||
{
|
||||
"type": "folder",
|
||||
"item": {
|
||||
"path": entry.type_src,
|
||||
"count": count,
|
||||
},
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
if entry.type == "playlist":
|
||||
is_recently_added = entry.type_src == "recentlyadded"
|
||||
|
||||
if is_recently_added:
|
||||
playlist, _ = get_recently_added_playlist()
|
||||
playlist.images = [i["image"] for i in playlist.images]
|
||||
items.append(
|
||||
{
|
||||
"type": "playlist",
|
||||
"item": serialize_playlist(
|
||||
playlist, to_remove={"settings", "duration"}
|
||||
),
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
playlist = pdb.get_playlist_by_id(entry.type_src)
|
||||
if playlist is None:
|
||||
continue
|
||||
|
||||
tracks = TrackStore.get_tracks_by_trackhashes(playlist.trackhashes)
|
||||
playlist.clear_lists()
|
||||
|
||||
if not playlist.has_image:
|
||||
images = get_first_4_images(tracks)
|
||||
images = [i["image"] for i in images]
|
||||
playlist.images = images
|
||||
|
||||
items.append({"type": "playlist", "item": serialize_playlist(playlist)})
|
||||
|
||||
return items
|
||||
+65
-28
@@ -4,11 +4,18 @@ This library contains all the functions related to playlists.
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from PIL import Image, ImageSequence
|
||||
|
||||
from app import settings
|
||||
from app.lib.home.recentlyadded import get_recent_tracks
|
||||
from app.models.playlist import Playlist
|
||||
from app.models.track import Track
|
||||
from app.store.albums import AlbumStore
|
||||
from app.store.tracks import TrackStore
|
||||
from app.utils.dates import create_new_date
|
||||
|
||||
|
||||
def create_thumbnail(image: Any, img_path: str) -> str:
|
||||
@@ -86,33 +93,63 @@ def save_p_image(
|
||||
return filename
|
||||
|
||||
|
||||
#
|
||||
# class ValidatePlaylistThumbs:
|
||||
# """
|
||||
# Removes all unused images in the images/playlists folder.
|
||||
# """
|
||||
#
|
||||
# def __init__(self) -> None:
|
||||
# images = []
|
||||
# playlists = Get.get_all_playlists()
|
||||
#
|
||||
# log.info("Validating playlist thumbnails")
|
||||
# for playlist in playlists:
|
||||
# if playlist.image:
|
||||
# img_path = playlist.image.split("/")[-1]
|
||||
# thumb_path = playlist.thumb.split("/")[-1]
|
||||
#
|
||||
# images.append(img_path)
|
||||
# images.append(thumb_path)
|
||||
#
|
||||
# p_path = os.path.join(settings.APP_DIR, "images", "playlists")
|
||||
#
|
||||
# for image in os.listdir(p_path):
|
||||
# if image not in images:
|
||||
# os.remove(os.path.join(p_path, image))
|
||||
#
|
||||
# log.info("Validating playlist thumbnails ... ✅")
|
||||
#
|
||||
def duplicate_images(images: list):
|
||||
if len(images) == 1:
|
||||
images *= 4
|
||||
elif len(images) == 2:
|
||||
images += list(reversed(images))
|
||||
elif len(images) == 3:
|
||||
images = images + images[:1]
|
||||
|
||||
return images
|
||||
|
||||
|
||||
# TODO: Fix ValidatePlaylistThumbs
|
||||
def get_first_4_images(
|
||||
tracks: list[Track] = [], trackhashes: list[str] = []
|
||||
) -> list[dict["str", str]]:
|
||||
if len(trackhashes) > 0:
|
||||
tracks = TrackStore.get_tracks_by_trackhashes(trackhashes)
|
||||
|
||||
albums = []
|
||||
|
||||
for track in tracks:
|
||||
if track.albumhash not in albums:
|
||||
albums.append(track.albumhash)
|
||||
|
||||
if len(albums) == 4:
|
||||
break
|
||||
|
||||
albums = AlbumStore.get_albums_by_hashes(albums)
|
||||
images = [
|
||||
{
|
||||
"image": album.image,
|
||||
"color": "".join(album.colors),
|
||||
}
|
||||
for album in albums
|
||||
]
|
||||
|
||||
if len(images) == 4:
|
||||
return images
|
||||
|
||||
return duplicate_images(images)
|
||||
|
||||
|
||||
def get_recently_added_playlist(cutoff: int = 14):
|
||||
playlist = Playlist(
|
||||
id="recentlyplayed",
|
||||
name="Recently Added",
|
||||
image=None,
|
||||
last_updated="Now",
|
||||
settings={},
|
||||
trackhashes=[],
|
||||
)
|
||||
|
||||
tracks = get_recent_tracks(cutoff)
|
||||
date = datetime.fromtimestamp(tracks[0].created_date)
|
||||
playlist.last_updated = create_new_date(date)
|
||||
|
||||
images = get_first_4_images(tracks=tracks)
|
||||
playlist.images = images
|
||||
playlist.set_count(len(tracks))
|
||||
|
||||
return playlist, tracks
|
||||
|
||||
Reference in New Issue
Block a user