mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
move recently added to routines
This commit is contained in:
@@ -3,25 +3,13 @@ Recipes are a way to create mixes.
|
||||
"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from app.db.userdata import UserTable
|
||||
from app.models.mix import Mix
|
||||
from app.plugins.mixes import MixesPlugin
|
||||
from app.store.homepage import HomepageStore
|
||||
|
||||
from typing import Any, List
|
||||
|
||||
class HomepageRoutine(ABC):
|
||||
"""
|
||||
A routine creates a row of homepage items.
|
||||
"""
|
||||
|
||||
title: str
|
||||
description: str
|
||||
|
||||
items: List[Mix]
|
||||
extra: Dict[str, Any]
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def is_valid(self) -> bool: ...
|
||||
@@ -30,37 +18,12 @@ class HomepageRoutine(ABC):
|
||||
if not self.is_valid:
|
||||
return
|
||||
|
||||
self.items = self.run()
|
||||
self.run()
|
||||
|
||||
@abstractmethod
|
||||
def run(self) -> List[Mix]:
|
||||
def run(self) -> List[Any]:
|
||||
"""
|
||||
Creates the homepage items and saves them to the
|
||||
homepage store if self.is_valid is true.
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class ArtistMixes(HomepageRoutine):
|
||||
items: List[Mix] = []
|
||||
extra: Dict[str, Any] = {}
|
||||
store_key = "artist_mixes"
|
||||
|
||||
@property
|
||||
def is_valid(self):
|
||||
return MixesPlugin().enabled
|
||||
|
||||
def run(self):
|
||||
users = UserTable.get_all()
|
||||
|
||||
for user in users:
|
||||
mix = MixesPlugin()
|
||||
mixes = mix.create_artist_mixes(user.id)
|
||||
|
||||
if not mixes:
|
||||
continue
|
||||
|
||||
HomepageStore.set_mixes(mixes, mixkey=self.store_key, userid=user.id)
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from app.db.userdata import UserTable
|
||||
from app.lib.recipes import HomepageRoutine
|
||||
from app.plugins.mixes import MixesPlugin
|
||||
from app.store.homepage import HomepageStore
|
||||
|
||||
|
||||
class ArtistMixes(HomepageRoutine):
|
||||
store_key = "artist_mixes"
|
||||
|
||||
@property
|
||||
def is_valid(self):
|
||||
return MixesPlugin().enabled
|
||||
|
||||
def run(self):
|
||||
users = UserTable.get_all()
|
||||
|
||||
for user in users:
|
||||
mix = MixesPlugin()
|
||||
mixes = mix.create_artist_mixes(user.id)
|
||||
|
||||
if not mixes:
|
||||
continue
|
||||
|
||||
HomepageStore.set_mixes(mixes, entrykey=self.store_key, userid=user.id)
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
@@ -0,0 +1,46 @@
|
||||
import pprint
|
||||
from app.db.userdata import UserTable
|
||||
from app.lib.home.recentlyadded import get_recently_added_items
|
||||
from app.lib.home.recentlyplayed import get_recently_played
|
||||
from app.lib.recipes import HomepageRoutine
|
||||
from app.store.homepage import HomepageStore
|
||||
|
||||
|
||||
class RecentlyPlayed(HomepageRoutine):
|
||||
store_key = "recently_played"
|
||||
|
||||
def __init__(self, userid: int | None = None) -> None:
|
||||
"""
|
||||
The userid is provided when we are running this routine
|
||||
outside a cron job. ie. when a user records a new scrobble.
|
||||
"""
|
||||
self.userids = [userid] if userid else [user.id for user in UserTable.get_all()]
|
||||
super().__init__()
|
||||
|
||||
@property
|
||||
def is_valid(self):
|
||||
return True
|
||||
|
||||
def run(self):
|
||||
for userid in self.userids:
|
||||
items = get_recently_played(limit=15, userid=userid)
|
||||
HomepageStore.entries[self.store_key].items[userid] = items
|
||||
|
||||
|
||||
class RecentlyAdded(HomepageRoutine):
|
||||
ITEM_LIMIT = 15
|
||||
store_key = "recently_added"
|
||||
|
||||
@property
|
||||
def is_valid(self):
|
||||
return True
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def run(self):
|
||||
items = get_recently_added_items(limit=self.ITEM_LIMIT)
|
||||
|
||||
# NOTE: Recently added is a global entry
|
||||
# So we don't need a userid
|
||||
HomepageStore.entries[self.store_key].items[0] = items
|
||||
@@ -0,0 +1,84 @@
|
||||
from gettext import ngettext
|
||||
from os import name
|
||||
import pendulum
|
||||
|
||||
from app.crons.cron import CronJob
|
||||
from app.db.userdata import UserTable
|
||||
from app.lib.recipes import HomepageRoutine
|
||||
from app.store.homepage import HomepageStore
|
||||
from app.utils.dates import get_date_range, seconds_to_time_string
|
||||
from app.utils.stats import get_artists_in_period
|
||||
|
||||
|
||||
class TopArtists(CronJob, HomepageRoutine):
|
||||
"""
|
||||
A routine to populate the top streamed artists/albums in the last week or month
|
||||
"""
|
||||
|
||||
hours = 1
|
||||
ITEM_LIMIT = 15
|
||||
|
||||
@property
|
||||
def is_valid(self):
|
||||
"""
|
||||
Only valid if it's the middle or last 2 days of this month.
|
||||
|
||||
When the duration is "week", it's valid on Saturday and Sunday.
|
||||
"""
|
||||
if self.duration == "month":
|
||||
now = pendulum.now()
|
||||
middle_day = now.days_in_month // 2
|
||||
|
||||
return (
|
||||
now.day in range(middle_day, middle_day + 2)
|
||||
or now.day > now.days_in_month - 2
|
||||
)
|
||||
if self.duration == "week":
|
||||
return pendulum.now().isoweekday() in (6, 7)
|
||||
|
||||
return False
|
||||
|
||||
def __init__(self, duration: str = "month") -> None:
|
||||
super().__init__()
|
||||
self.duration = duration
|
||||
|
||||
if not self.is_valid:
|
||||
return
|
||||
|
||||
def run(self):
|
||||
if not self.is_valid:
|
||||
self.destroy()
|
||||
return
|
||||
|
||||
self.userids = [user.id for user in UserTable.get_all()]
|
||||
|
||||
for userid in self.userids:
|
||||
date_range = get_date_range(self.duration)
|
||||
artists = get_artists_in_period(date_range[0], date_range[1], userid)[
|
||||
: self.ITEM_LIMIT
|
||||
]
|
||||
|
||||
artists = [
|
||||
{
|
||||
"type": "artist",
|
||||
"hash": artist["artisthash"],
|
||||
"help_text": seconds_to_time_string(artist["playduration"]),
|
||||
"secondary_text": str(artist["playcount"])
|
||||
+ " "
|
||||
+ ngettext("play", "plays", artist["playcount"]),
|
||||
}
|
||||
for artist in artists
|
||||
]
|
||||
|
||||
HomepageStore.entries[f"top_streamed_{self.duration}ly_artists"].items[
|
||||
userid
|
||||
] = artists
|
||||
|
||||
def destroy(self):
|
||||
"""
|
||||
Clear the top streamed entry from the homepage store.
|
||||
"""
|
||||
keys = [f"top_streamed_{self.duration}ly_artists"]
|
||||
|
||||
for key in keys:
|
||||
HomepageStore.entries[key].items = {}
|
||||
Reference in New Issue
Block a user