Have Trouble Adapting LibreTime Best Practices for Developers?

Hey everyone,

I am learning more about LibreTime development and I want some advice on how to tailor and enhance it for particular use cases. I have got a basic setup going but I want to take it a step further. I am interested in learning more about best practices for customizing the UI, automating certain tasks & improving overall performance.

Has anyone here worked on custom modules or integrations with LibreTime?? What were the biggest challenges you faced & how did you overcome them?? Also any suggestion on documentation or resources that helped you during your development journey would be awesome.

I want to know how to manage large scale deployments and what tools or frameworks you have found most effective for streamlining the process. I have been considering integrating a splunk tool for monitoring & analysis has anyone used that with LibreTime??

Also i have check this ; Request: LibreTime demo - sandbox practice if anyone have any resources, tutorials or personal experiences please share with me, It would be greatly appreciated!!

Thank you……. :slight_smile:

We’ve done quite a bit to get LibreTime to work for our use cases. The new API is still a WIP so it’s a bit difficult trying to figure out how to use it. There used to be a watchfolder feature which we used to “sync” our database, and we would write metadata to the mp3 files, and LibreTime would pick up that metadata when the file was imported. We used that to build our playlists and smartblocks. But in this new version (>3.0) the watch folder was removed so I’ve managed to more or less “reimplmement” that using the API. But I’ve had to use the legacy API and the new API to get it to work.

Here’s some of the new code. Sorry this is a very “lazy” reply but hopefully the code might point you in the right direction. LibreTime needs a lot more community work and I’m sure they would appreciate more contributors. A new UI is on the todo list but idk where that’s at.


def login_playwright():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context()
        page = context.new_page()
        page.goto(f'{LIBRETIME_URL}/login')
        # Perform login
        page.fill('input[name="username"]', LIBRETIME_USER)
        page.fill('input[name="password"]', LIBRETIME_PASSWORD)
        page.click('input[name="submit"]')
        expect(page).to_have_url(re.compile('.*showbuilder.*'))
        context.storage_state(path='session.json')
        SESSION_ID = page.context.cookies()[0]['value']
        # Closing browser
        browser.close()
    return SESSION_ID

def load_radio_db():
    print("Loading Libretime DB")
    API_URL = f"{LIBRETIME_URL}/api/v2/files"
    response = requests.get(
        API_URL, auth=LIBRETIME_BASIC_AUTH
    )
    files = response.json()
    return files

def update_file(file_id, kwargs):
    API_URL = f"{LIBRETIME_URL}/api/v2/files/{file_id}"
    response = requests.put(
        API_URL, auth=LIBRETIME_BASIC_AUTH,
        json={**kwargs},
    )
    try:
        response.raise_for_status()
    except Exception:
        logging.error(response.text)
    finally:
        return response.status_code


def delete_file(file_id, SESSION_ID):
    API_URL = f"{LIBRETIME_URL}/library/delete"
    raw_data = \
        f"format=json&media%5B0%5D%5Bid%5D={file_id}&media%5B0%5D%5Btype%5D=audioclip"
    session = requests.Session()
    session.headers.update({
        "Accept": "*/*",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "X-Requested-With": "XMLHttpRequest",
        "Referrer": '{LIBRETIME_URL}/showbuilder',
        "Accept": "*/*",
        'Origin': '{LIBRETIME_URL}',
        'Cookie': f"PHPSESSID={SESSION_ID}"
    })

    response = session.post(
        API_URL,
        data=raw_data,
    )
    try:
        response.raise_for_status()
        response.json()
    except Exception as e:
        logging.error(e)
        logging.error("Failed to delete.")
        return response.status_code

    API_URL = f"{LIBRETIME_URL}/api/v2/files/{file_id}"
    response = requests.delete(
        API_URL, auth=LIBRETIME_BASIC_AUTH
    )
    try:
        response.raise_for_status()
    except Exception:
        return response.status_code
    if response.status_code != 204:
        logging.error("Could not delete")
    return response.status_code


def upload_file(file_path):
    API_URL = f"{LIBRETIME_URL}/rest/media"
    filename = file_path.split('/')[-1]
    try:
        with open(file_path, 'rb') as file:
            response = requests.post(
                API_URL, auth=(API_TOKEN, ''),
                files=[
                    ('file', (filename, file))
                ],
                timeout=30,
            )
    except Exception as e:
        logging.error(e)
        return 500
    try:
        response.raise_for_status()
    except Exception:
        logging.error(response.text)
    finally:
        return response.status_code