• English
  • Deutsch
  • Español
  • Русский
Professional Radio Automation at a price you can afford.Professional Radio Automation at a price you can afford.
RadioBOSS box
RadioBOSS makes automating your broadcast to the world, your business or shop professional, simple, affordable, reliable.

Author Topic: [Web site] Song request demo script  (Read 23889 times)

0 Members and 2 Guests are viewing this topic.

djsoft

  • Administrator
  • Posts: 10337
    • http://www.djsoft.net
[Web site] Song request demo script
« on: March 19, 2018, 08:36:42 AM »
RadioBOSS song request script. An example script showing how to implement song requests in RadioBOSS.

User manual page about song requests: https://manual.djsoft.net/radioboss/en/songrequests.htm

How it works
It shows a form with the three fields: artist, title and message. At least one of the fields (artist or title) have to be filled.

The requested song is searched in the music library. If it's found, the request is added to the song request list in RadioBOSS along with the message entered.

It is strongly recommended to have some web programming knowledge in order to install and use this script.

If a script does't work, you can set the $show_errors to 1 to see the detailed error message. Please do not forget to set it back to 0 once the script is deployed.

Update 2019-11-25
Script uses cURL instead of file_get_contents to communicate with RadioBOSS. This allows the script to work on hosts where file_get_contents is not permitted to access URLs.

Code: [Select]
<?php
/*
    RadioBOSS Song Request demo script

    To play the requested songs in RadioBOSS, schedule an event with "playrequestedsong" command:
    http://manual.djsoft.net/radioboss/en/scheduler_commands.htm#songrequest

    Please make sure the RadioBOSS API is enabled and a password is set:
    http://manual.djsoft.net/radioboss/en/remote_controlapi.htm

If RadioBOSS is installed on a server, please make sure the API port (9000 by default) is allowed in firewall.

    Home or studio PC:
If RadioBOSS is installed on a home or studio PC, please make sure it has a static IP address.
    If a static IP address is not available, a Dynamic DNS address has to be used instead
    The IP address (or dynamic DNS address) is entered into the $rb_server variable (please do not include http://)

If a computer is behing a NAT (this is usually the case when a router is used), then API port (9000 by default) has
to be forwarded in router settings - please see the port forwarding documentation for your router.
*/

//---------------//
// CONFIGURATION //
//---------------//
//RadioBOSS API connection details
$rb_server '127.0.0.1'//RadioBOSS hostname or IP
$rb_port '9000'//RadioBOSS port
$rb_password '7bNR5UK'//API password
//music library name, omitting the .xml extension, the library is loaded from "Music library folder" as set in RadioBOSS settings
$rb_library 'music';

//show detailed error messages (1 - show error details, 0 - show only general error messages)
//IMPORTANT! Make sure this is set to 0 once everything is configured and working to avoid revealing too many details to users!
//Error messages may contain passwords and other sensitive information
//Set this to 1 only if something's not working to get more details
$show_errors 0;

//-------------------//
// SONG REQUEST FORM //
//-------------- ----//
//API URL base
$rb_api "http://$rb_server:$rb_port?pass=$rb_password";
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>RadioBOSS Song Request demo</title>
    <style>
        body {
            font-family: Tahoma, sans-serif;
            font-size: 0.8em;
        }
        label {
            display: block;
            margin-bottom: 5pt;
        }
    </style>
</head>
<body>
<?php

$last_err 
''//last HTTPGet error message

function HTTPGet($url) {
    global 
$last_err;
    
$curl curl_init($url);
    
curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
    
curl_setopt($curlCURLOPT_HEADERfalse);
    
$res curl_exec($curl);
    if (
$res === false) {
        
$last_err curl_error($curl);
    }
    
curl_close($curl);
    return 
$res;
}

function 
result($msg) {
    
$back_link '<a href="javascript:history.back();">Back</a>';
    exit(
"$msg $back_link");
}

$type = isset($_POST['type']) ? $_POST['type'] : '';
if (
$type === '') {
    echo 
'<form method="post">
            <input type="hidden" name="type" value="request">
            <label>Artist<br><input size="30" name="artist"></label>
            <label>Title<br><input size="30" name="title"></label>
            <label>Message<br><textarea cols="30" rows="3" name="message"></textarea></label>
            <button>Request a song</button>
        </form>'
;
} elseif (
$type === 'request') {
    
//requested artist
    
$artist mb_strtolower(trim($_POST['artist']));
    if (
$artist === '') {
        
$artist false;
    }
    
//requested title
    
$title mb_strtolower(trim($_POST['title']));
    if (
$title === '') {
        
$title false;
    }
    if ((
$artist === false) && ($title === false)) {
        
result('No artist or title entered.');
    }
    
//load library
    
$library_raw HTTPGet("$rb_api&action=library&filename=" urlencode($rb_library));
    if (
$library_raw === false) {
        
$err 'Song request failed: unable to load music library.';
        if (
$show_errors) {
            
$err .= ' Error: ' $last_err;
        }
        
result($err);
    }
    
//parse XML data
    
$xml simplexml_load_string($library_raw);
    if (
$xml === false) {
        
result('Song request failed: unable to parse music library XML data.');
    }
    
$fn false;
    
//search requested song in a music library
    
foreach ($xml as $x) {
        if (
$x->getName() !== 'Track') {
            continue;
        }
        
$found = (($artist === false) || (mb_strtolower((string)$x['artist']) === $artist)) &&
            ((
$title === false) || (mb_strtolower((string)$x['title']) === $title));
        if (
$found) {
            
$fn = (string)$x['filename'];
            break;
        }
    }
    
//song found, add to requested songs list in RadioBOSS
    
if ($fn !== false) {
        
$msg = isset($_POST['message']) ? $_POST['message'] : '';
        
$res HTTPGet("$rb_api&action=songrequest&filename=" urlencode($fn) . '&message=' urlencode($msg));
        if (
$res === 'OK') {
            
result('Song requested successfully!');
        } else {
            
$err 'An error occurred while adding song request.';
            if (
$show_errors) {
                
$err .= ' Error: ' $last_err;
            }
            
result($err);
        }
    } else {
        
result('Requested song not found in the music library.');
    }
}
?>

</body>
</html>
« Last Edit: November 25, 2019, 06:58:16 AM by djsoft »
Dmitry  •  DJSoft.Net Team  •  Email: support@djsoft.net

viadeskp .

  • Posts: 1
Re: Song request demo script
« Reply #1 on: March 30, 2018, 01:24:31 PM »
thanks for the contribution, I have a doubt, the code is pasted as is in the example? clear changing the ip the port and the key

djsoft

  • Administrator
  • Posts: 10337
    • http://www.djsoft.net
Re: Song request demo script
« Reply #2 on: April 02, 2018, 02:21:11 AM »
Yes, this is an example. The password is an example too - it won't work anywhere.
Dmitry  •  DJSoft.Net Team  •  Email: support@djsoft.net

Sasa Grbic

  • Posts: 53
    • Radio Grmljavina
Re: Song request demo script
« Reply #3 on: June 02, 2018, 08:57:49 AM »
This script will not work for me. I enabled an API in the RadioBOSS settings, set a password, and all the data entered in the script. I've made a database with a complete music called music.xml. In the script under the database is already written by music. The dynamic dns is working properly, the ip address of the computer on which RadioBOSS is located is located in the DMZ zone on the router. When I open a script on a server and enter the artist name, I get a note that I can not load the database. In the settings at one place, NVDA read \ database \. I guess it's a path to the base. My databases are in appdata \ roaming \ djsoftnet \ radiobossxxx \ database.

stokemusic

  • Posts: 22
Re: Song request demo script
« Reply #4 on: June 03, 2018, 03:00:40 AM »
Yes, I had the same problem, in the end I gave up

djsoft

  • Administrator
  • Posts: 10337
    • http://www.djsoft.net
Re: Song request demo script
« Reply #5 on: June 04, 2018, 08:10:42 AM »
The dynamic dns is working properly, the ip address of the computer on which RadioBOSS is located is located in the DMZ zone on the router. When I open a script on a server and enter the artist name, I get a note that I can not load the database.
Can you please post the exact error message?
Dmitry  •  DJSoft.Net Team  •  Email: support@djsoft.net

Sasa Grbic

  • Posts: 53
    • Radio Grmljavina
Re: Song request demo script
« Reply #6 on: June 04, 2018, 08:40:06 AM »
Code: [Select]
Song request failed: unable to load music library.

djsoft

  • Administrator
  • Posts: 10337
    • http://www.djsoft.net
Re: Song request demo script
« Reply #7 on: June 04, 2018, 02:20:33 PM »
Code: [Select]
Song request failed: unable to load music library.
It appears that either dynamic IP does not work, or song request script is not configured properly. Please try accessing from your browser (change address and port to actual values).
http://address:port?pass=1

It should show you the password is incorrect - that means, the request reached RadioBOSS and was processed. If you see some other error message, then RadioBOSS is inaccessible from internet.
Dmitry  •  DJSoft.Net Team  •  Email: support@djsoft.net

Sasa Grbic

  • Posts: 53
    • Radio Grmljavina
Re: Song request demo script
« Reply #8 on: June 04, 2018, 04:25:18 PM »
It still makes the same mistake.

djsoft

  • Administrator
  • Posts: 10337
    • http://www.djsoft.net
Re: Song request demo script
« Reply #9 on: June 05, 2018, 05:08:04 AM »
What happens when you access http://address:port?pass=1 via web browser?
Dmitry  •  DJSoft.Net Team  •  Email: support@djsoft.net

Sasa Grbic

  • Posts: 53
    • Radio Grmljavina
Re: Song request demo script
« Reply #10 on: June 05, 2018, 05:34:03 AM »
When I entered the web browser http://name.server.com:port/?pass=xxxxxxxxxxxxxxxxx I got the following code:
Code: [Select]
E003: Nothing to do

djsoft

  • Administrator
  • Posts: 10337
    • http://www.djsoft.net
Re: Song request demo script
« Reply #11 on: June 05, 2018, 10:58:04 AM »
Please make sure that you did not include "http" in the server address when configuring a script.

Correct:
$rb_server = 'name.server.com';

Wrong:
$rb_server = 'http://name.server.com';
Dmitry  •  DJSoft.Net Team  •  Email: support@djsoft.net

Sasa Grbic

  • Posts: 53
    • Radio Grmljavina
Re: Song request demo script
« Reply #12 on: June 05, 2018, 12:31:17 PM »
That's how it is set. Http is only in api url.

djsoft

  • Administrator
  • Posts: 10337
    • http://www.djsoft.net
Re: Song request demo script
« Reply #13 on: June 07, 2018, 02:15:25 PM »
It's hard to tell what's wrong. You can send your script to support@djsoft.net, we'll check what could be wrong there. Alternatively, if you are familiar with PHP, you may add some additional error checking to the script to see why it fails.
Dmitry  •  DJSoft.Net Team  •  Email: support@djsoft.net

stokemusic

  • Posts: 22
Re: Song request demo script
« Reply #14 on: June 10, 2018, 03:03:39 AM »
Could a script be incorporated into the next release of RB which could do this automatically if desired?