|
@@ -1,11 +1,12 @@
|
|
|
from jellyfin_api_client.models import *
|
|
|
-from jellyfin_api_client.api.items import get_items
|
|
|
+from jellyfin_api_client.api.items import get_items, get_item_user_data
|
|
|
from jellyfin_api_client.api.user import get_users
|
|
|
from jellyfin_api_client.api.playlists import create_playlist, update_playlist
|
|
|
from jellyfin_api_client.types import Response
|
|
|
from jellyfin_api_client import AuthenticatedClient
|
|
|
|
|
|
import os
|
|
|
+import random
|
|
|
from dotenv import load_dotenv
|
|
|
from pprint import pp
|
|
|
|
|
@@ -13,8 +14,8 @@ load_dotenv()
|
|
|
|
|
|
def log_response(response):
|
|
|
request = response.request
|
|
|
- print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}")
|
|
|
if response.status_code >= 400:
|
|
|
+ print(f"{request.method} {request.url} - Status {response.status_code}")
|
|
|
raise Exception(response.read())
|
|
|
|
|
|
server_url = os.getenv('SERVER_URL')
|
|
@@ -22,6 +23,7 @@ api_token = os.getenv('API_TOKEN')
|
|
|
device = os.getenv('HOSTNAME')
|
|
|
user_names = os.getenv('USER_NAMES').split()
|
|
|
playlist_name = os.getenv('PLAYLIST')
|
|
|
+item_count = int(os.getenv('ITEM_COUNT'))
|
|
|
|
|
|
client = AuthenticatedClient(
|
|
|
base_url=server_url,
|
|
@@ -32,7 +34,7 @@ client = AuthenticatedClient(
|
|
|
|
|
|
with client as client:
|
|
|
users = get_users.sync(client=client)
|
|
|
- user_ids = [user.id for user in users if user.name in user_names]
|
|
|
+ users = [user for user in users if user.name in user_names]
|
|
|
|
|
|
playlists = get_items.sync(
|
|
|
client=client,
|
|
@@ -42,21 +44,45 @@ with client as client:
|
|
|
)
|
|
|
|
|
|
songs: list[BaseItemDto] = []
|
|
|
- for user_id in user_ids:
|
|
|
+ for user in users:
|
|
|
result = get_items.sync(
|
|
|
client=client,
|
|
|
- user_id=user_ids,
|
|
|
+ user_id=user.id,
|
|
|
recursive=True,
|
|
|
+ enable_images=False,
|
|
|
include_item_types=[BaseItemKind.AUDIO],
|
|
|
- limit=420,
|
|
|
+ limit=item_count,
|
|
|
sort_by=[ItemSortBy.PLAYCOUNT, ItemSortBy.RANDOM])
|
|
|
- songs.extend([item for item in result.items if not any(s.id == item.id for s in songs)])
|
|
|
+
|
|
|
+ play_count_min = min(s.user_data.play_count for s in result.items)
|
|
|
+ play_count_max = max(s.user_data.play_count for s in result.items)
|
|
|
+ print(f"Got {len(result.items)} songs for user {user.name} (played {play_count_min} to {play_count_max} times)")
|
|
|
+ for item in result.items:
|
|
|
+ if any(item.id == s.id for s in songs):
|
|
|
+ continue
|
|
|
+ play_count = item.user_data.play_count
|
|
|
+ for u in users:
|
|
|
+ if u.id == user.id:
|
|
|
+ continue
|
|
|
+ user_data = get_item_user_data.sync(item.id, client=client, user_id=u.id)
|
|
|
+ play_count += user_data.play_count
|
|
|
+ item.user_data.play_count = play_count
|
|
|
+ songs.append(item)
|
|
|
+
|
|
|
+ print(f"Selecting from {len(songs)} unique songs...")
|
|
|
+ random.shuffle(songs)
|
|
|
+ songs.sort(key=lambda s: s.user_data.play_count)
|
|
|
+ songs = songs[:item_count]
|
|
|
+
|
|
|
+ play_count_min = min(s.user_data.play_count for s in songs)
|
|
|
+ play_count_max = max(s.user_data.play_count for s in songs)
|
|
|
+ print(f"Using the {len(songs)} least played songs (played {play_count_min} to {play_count_max} times)")
|
|
|
|
|
|
try:
|
|
|
playlist_id = next(p.id for p in playlists.items if p.name == playlist_name)
|
|
|
body = UpdatePlaylistDto(
|
|
|
name=playlist_name,
|
|
|
- users=[PlaylistUserPermissions(user_id=id, can_edit=True) for id in user_ids],
|
|
|
+ users=[PlaylistUserPermissions(user_id=u.id, can_edit=True) for u in users],
|
|
|
ids=[item.id for item in songs],
|
|
|
is_public=True
|
|
|
)
|
|
@@ -70,12 +96,11 @@ with client as client:
|
|
|
client=client,
|
|
|
body=CreatePlaylistDto(
|
|
|
name=playlist_name,
|
|
|
- user_id=user_ids[0],
|
|
|
- users=[PlaylistUserPermissions(user_id=id, can_edit=True) for id in user_ids],
|
|
|
+ user_id=users[0].id,
|
|
|
+ users=[PlaylistUserPermissions(user_id=u.id, can_edit=True) for u in users],
|
|
|
ids=[item.id for item in songs],
|
|
|
is_public=True,
|
|
|
media_type=CreatePlaylistDtoMediaType.AUDIO
|
|
|
)
|
|
|
)
|
|
|
- print()
|
|
|
print(f"Successfully updated playlist '{playlist_name}' with {len(songs)} songs")
|