feat: Add script to get Navidrome playlists via Subsonic API
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
62
main.py
62
main.py
@@ -1,8 +1,68 @@
|
||||
import os, logging
|
||||
import hashlib
|
||||
import random
|
||||
import string
|
||||
|
||||
import requests
|
||||
DEBUG = os.environ.get('DEBUG')
|
||||
logging.basicConfig(
|
||||
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
|
||||
level=logging.DEBUG if DEBUG else logging.INFO)
|
||||
|
||||
def main():
|
||||
pass
|
||||
"""Get playlists from a Navidrome server using the Subsonic API."""
|
||||
navidrome_url = os.environ.get('NAVIDROME_URL')
|
||||
username = os.environ.get('NAVIDROME_USER')
|
||||
password = os.environ.get('NAVIDROME_PASSWORD')
|
||||
|
||||
if not all([navidrome_url, username, password]):
|
||||
logging.error("NAVIDROME_URL, NAVIDROME_USER, and NAVIDROME_PASSWORD environment variables must be set.")
|
||||
return
|
||||
|
||||
# Subsonic API requires a salt and a token (md5(password + salt))
|
||||
salt = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
|
||||
token = hashlib.md5((password + salt).encode('utf-8')).hexdigest()
|
||||
|
||||
params = {
|
||||
'u': username,
|
||||
't': token,
|
||||
's': salt,
|
||||
'v': '1.16.1',
|
||||
'c': 'playlist-script',
|
||||
'f': 'json'
|
||||
}
|
||||
|
||||
api_url = f"{navidrome_url.rstrip('/')}/rest/getPlaylists.view"
|
||||
|
||||
try:
|
||||
response = requests.get(api_url, params=params)
|
||||
response.raise_for_status() # Raise an exception for bad status codes
|
||||
except requests.exceptions.RequestException as e:
|
||||
logging.error(f"Error connecting to Navidrome: {e}")
|
||||
return
|
||||
|
||||
data = response.json()
|
||||
|
||||
subsonic_response = data.get('subsonic-response')
|
||||
if not subsonic_response:
|
||||
logging.error("Invalid response format from server.")
|
||||
return
|
||||
|
||||
if subsonic_response.get('status') == 'failed':
|
||||
error = subsonic_response.get('error', {})
|
||||
logging.error(f"API Error: {error.get('message')} (code: {error.get('code')})")
|
||||
return
|
||||
|
||||
playlists = subsonic_response.get('playlists', {}).get('playlist', [])
|
||||
|
||||
if not playlists:
|
||||
logging.info("No playlists found for user '%s'.", username)
|
||||
return
|
||||
|
||||
print(f"Playlists for {username}:")
|
||||
for playlist in playlists:
|
||||
print(f" - {playlist.get('name')} ({playlist.get('songCount')} songs)")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user