Another approach to TuneIn (Icecast playlist)
  • Hi,

    In first sorry for my english, google translate is my friend.. ;)

    Let me present you my approach for updating our playlist radio on TuneIn.
    1. Firstly, activate playlist log in icecast. Edit /etc/icecast2/icecast.xml and uncomment this line "<playlistlog>playlist.log</playlistlog>"
    2. Restart icecast service : sudo service icecast2 restart
    3. Now you can see new entries in playlist log : tail -f /var/log/icecast2/playlist.log for each new song played on the air.

    Now you must create two files.

    One bash script for parsing new entries in playlist log : 


    #!/bin/bash
    export LANG=fr_CA.UTF-8

    log=/var/log/icecast2/playlist.log  ## Playlist log
    script=/home/XXX/update_tunein.php  ## PHP script for call TuneIn API
    LASTFILE=""

    ## Browse the log file of playlist.log
    tail -Fn0 $log | \
    while read line ; do
    ## Get the last line of the file and encode it
    INFOCOMPLETE=$(echo "$line"|base64)
    NEWFILE="${line:45}"
    ## If the last line is different from the penultimate call a PHP script
    if [ "$NEWFILE" != "$LASTFILE"  ]
    then
    php $script $INFOCOMPLETE
    LASTFILE=$NEWFILE
    fi
    done

    For testing, just call this script in terminal, but in production, be sure to run this script as a service and start it (example : sudo service tunein start)

    Now, create a php script (update_tunein.php) for updating each song to your TuneIn page : 


    header('Content-Type: text/html; charset=utf-8');

    // Collect information from BASH script
    $args = $_SERVER['argv']; 

    // Decode the parameter passed to the script
    $arguments = $args[1]; 
    if (isset($args[2])) $arguments = $args[1].$args[2]; // Sometimes, encoding base 64 does not generate second argument
    $infos = base64_decode($arguments); 
    //$infos = convert_ascii($infos); // optional
    $meta = substr(utf8_encode($infos), 45); // Data from playlist file are encoded in iso8859 (why ??)
    preg_match('/(.*)[ ] ?- [ ]?(.*)/', $meta, $infos_artist); // Extract artist and title

    $partnerId = "XXX";
    $partnerKey = "XXX";
    $stationId = "sXXXXX";
    $artist_tunein = str_replace(' ','+', $infos_artist[1]);
    $title_tunein = str_replace(' ','+', $infos_artist[2]);
    $commercial = false;

    // You can detect commercial, here is my approach with a specific artist name
    if ($infos_artist[2] == "XXXPublicityXXX") $commercial = true; 

    $url_tunein = "http://air.radiotime.com/Playing.ashx?partnerId=".$partnerId."&partnerKey=".$partnerKey."&id=".$stationId."&title=".$title_tunein."&artist=".$artist_tunein."&commercial=".$commercial;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url_tunein);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    $server_output = simplexml_load_string($data);
    curl_close($ch); 
    // Here a example for get the status code and error message in a log file
    if ($server_output->head->status != '200') $message = $server_output->head->status." : ".$server_output->head->fault." - ".$url_tunein;
      else $message = $server_output->head->status;
    fputs($log, ' - resultat ('.$message.')'.PHP_EOL); 


    What do you think?

    I use also this script to update the database of our Drupal site. You can visiting our historical online as example here http://www.ckiafm.org/chansons
    The above script is lightened because the full script makes also a request to the API Last.fm, Youtube and youtubeinmp3.com.

    I hope this script will inspire you.

    It's hard for me to promise support because of my English.

    Bye !
  • 10 Comments sorted by
  • Excellent to see new approaches.
    The only disadvantages of this method it assumes you are running your own Streaming Icecast server.
    • If you are sending your stream to a Shoutcast hosting or
    • you do not have root access to the Stream server

    you cannot implement this method.

    VOISSES
    Post edited by Voisses Tech at 2014-10-15 05:29:56
    Anyone reading this a find it funny about my grammar , I make no apology ,Go get a translator.
    "The Problem with education today is that it takes a university degree to switch on a light bulb"
    "You learn from your mistakes but wise people learn from others mistakes avoid Making mistakes there is not sufficient rooms to make them"
    "Innuendo","If's","Assumptions" and "Fear" are for politician.Who,What,where,When and How are for those seeking knowledge and care about Humanity.
    "I might be in Mud but that does not Make me a Wild Hog(pig)"
    “Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage to move in the opposite direction.”
    "The only thing that remains constant is change itself"
    May the force be with you,until our path or destiny bring us in tandem.
  • Thank you, Yannick for your script, but i was in need to edit it. Now I share it.
    tunein.sh:
    #!/bin/bash
    export LANG=ru_RU.UTF-8

    log=/var/log/icecast/playlist.log  ## Playlist log
    script=/home/modi/tuneinapi/update_tunein.php  ## PHP script for call TuneIn API
    LASTFILE=""

    ## Browse the log file of playlist.log
    tail -Fn0 $log | \
    while read line ; do
    ## Get the last line of the file and encode it
    INFOCOMPLETE=$(echo $line | sed -e 's/ /_/g' -e 's/|/ /g' | awk '{print $4}' | sed -e 's/_/ /g'|base64)
    NEWFILE=$INFOCOMPLETE
    ## If the last line is different from the penultimate call a PHP script
    if [ "$NEWFILE" != "$LASTFILE" ]
    then
    php $script $INFOCOMPLETE
    LASTFILE=$NEWFILE
    fi
    done
    update_tunein.php:
    <?php
    header('Content-Type: text/html; charset=utf-8');

    // Collect information from BASH script
    $args = $_SERVER['argv']; 

    // Decode the parameter passed to the script
    $arguments = $args[1]; 
    if (isset($args[2])) $arguments = $args[1].$args[2]; // Sometimes, encoding base 64 does not generate second argument
    $infos = base64_decode($arguments); 
    //$infos = convert_ascii($infos); // optional
    $meta = substr(utf8_encode($infos), 0); // Data from playlist file are encoded in iso8859 (why ??)
    $meta2 = str_replace( '&', '%26', $meta);
    preg_match('/(.*)[ ] ?- [ ]?(.*)/', $meta2, $infos_artist); // Extract artist and title

    $partnerId = "XXXXX";
    $partnerKey = "XXXXXX";
    $stationId = "sXXXXX";
    $artist_tunein = str_replace(' ','+', $infos_artist[1]);
    $title_tunein = str_replace(' ','+', $infos_artist[2]);
    $commercial = false;

    // You can detect commercial, here is my approach with a specific artist name
    //if ($infos_artist[2] == "XXXPublicityXXX") $commercial = true; 

    $url_tunein = "http://air.radiotime.com/Playing.ashx?partnerId=".$partnerId."&partnerKey=".$partnerKey."&id=".$stationId."&title=".$title_tunein."&artist=".$artist_tunein."&commercial=".$commercial;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url_tunein);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    $server_output = simplexml_load_string($data);
    curl_close($ch); 
    // Here a example for get the status code and error message in a log file
    if ($server_output->head->status != '200') $message = $server_output->head->status." : ".$server_output->head->fault." - ".$url_tunein;
      else $message = $server_output->head->status." - ".$url_tunein;
    $log = fopen("tuneinlog.txt", "a");
    fputs($log, 'result: "'.$message.'"'.PHP_EOL); 
    fclose ($log);
    ?>
    Post edited by Ярослав Гребнев at 2015-02-01 08:37:41
  • I found, that russian track names are wrong-encoded. Anybody knows how to fix that?
  • Thanks for this suggestion on how to scrape the logs.( i always wanted something like this than my convoluted way

    The disadvantage with this method ( and REGEX) is as you see is how to deal with "non latin" languages

    There use to be in I think Artime 2.3 manual (if you could get a hold on one of them )a way to get the mount encode to utf8

    So search for it Also search the current manual for ICECAST LOCALISATION is will be of assistance.

    http://sourcefabric.booktype.pro/airtime-25-for-broadcasters/interface-localization/

    VOISSES

    Anyone reading this a find it funny about my grammar , I make no apology ,Go get a translator.
    "The Problem with education today is that it takes a university degree to switch on a light bulb"
    "You learn from your mistakes but wise people learn from others mistakes avoid Making mistakes there is not sufficient rooms to make them"
    "Innuendo","If's","Assumptions" and "Fear" are for politician.Who,What,where,When and How are for those seeking knowledge and care about Humanity.
    "I might be in Mud but that does not Make me a Wild Hog(pig)"
    “Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage to move in the opposite direction.”
    "The only thing that remains constant is change itself"
    May the force be with you,until our path or destiny bring us in tandem.
  • on log file russian tags are OK and file have utf-8 encoding
  • Hope some else can help you
    This part is "over my head"

    VOISSES
    Anyone reading this a find it funny about my grammar , I make no apology ,Go get a translator.
    "The Problem with education today is that it takes a university degree to switch on a light bulb"
    "You learn from your mistakes but wise people learn from others mistakes avoid Making mistakes there is not sufficient rooms to make them"
    "Innuendo","If's","Assumptions" and "Fear" are for politician.Who,What,where,When and How are for those seeking knowledge and care about Humanity.
    "I might be in Mud but that does not Make me a Wild Hog(pig)"
    “Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage to move in the opposite direction.”
    "The only thing that remains constant is change itself"
    May the force be with you,until our path or destiny bring us in tandem.
  • fixed that by editing
     $meta = substr(utf8_encode($infos), 0);
    to
     $meta = substr($infos, 0);
  • What about duplicating the yp pinging mechanism in the icecast source, only hitting air.radiotime.com with some http get love instead?
    Post edited by Roger Wilco at 2015-04-25 20:08:58
  • @Roger_Wilco

    I though by now you handle this.!
    In fact I use http $_POST  and Airtime recommends $_GET
    Anyone reading this a find it funny about my grammar , I make no apology ,Go get a translator.
    "The Problem with education today is that it takes a university degree to switch on a light bulb"
    "You learn from your mistakes but wise people learn from others mistakes avoid Making mistakes there is not sufficient rooms to make them"
    "Innuendo","If's","Assumptions" and "Fear" are for politician.Who,What,where,When and How are for those seeking knowledge and care about Humanity.
    "I might be in Mud but that does not Make me a Wild Hog(pig)"
    “Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage to move in the opposite direction.”
    "The only thing that remains constant is change itself"
    May the force be with you,until our path or destiny bring us in tandem.
  • Yeah, after taking a look at the yp stuff myself, this is the best route to take rather than adding it to airtime/liquidsoap.