77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
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():
|
|
"""Get playlists from a Navidrome server using the Subsonic API."""
|
|
navidrome_url = os.environ.get('NAVIDROME_URL')
|
|
username = os.environ.get('NAVIDROME_USER')
|
|
|
|
if not all([navidrome_url, username]):
|
|
logging.error("NAVIDROME_URL and NAVIDROME_USER environment variables must be set.")
|
|
return
|
|
|
|
salt = os.environ.get('SUBSONIC_SALT')
|
|
token = os.environ.get('SUBSONIC_TOKEN')
|
|
|
|
if not all([salt, token]):
|
|
password = os.environ.get('NAVIDROME_PASSWORD')
|
|
if not password:
|
|
logging.error("Either (SUBSONIC_SALT and SUBSONIC_TOKEN) or NAVIDROME_PASSWORD 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()
|