#!/bin/bash # # LinuxMCE script # This script shows/adds/deletes local radio stations that it finds from the website http://www.radiotime.com # to/from the local media database. # #Var Declaraions DownloadURL="http://opml.radiotime.com/Browse.ashx?c=local&filter=s:popular" NameOfTempFile=LocalRadio.Temp case $1 in show) #Download All Local Radio Stations Index wget -q --output-document=$NameOfTempFile $DownloadURL #Process Index cat $NameOfTempFile | grep audio | grep station | while read LINE ; do #Get Station Name Station=$(echo $LINE | gawk -F'"' '{ print $4}' | gawk -F'(' '{print $1}' | gawk -F"|" '{print $1}') echo -e "Radio Station : \e[01;33m$Station\e[00m" #Get Url echo $LINE | gawk -F'&f' '{print $1}' | gawk -F'"' '{system("curl --silent "$6"")}' | while read Url ; do echo -e "URL : $Url" done #Get Genre Genre=$(echo $LINE | gawk -F'"' '{ print $4}' | gawk -F'(' '{print $2}' | gawk -F')' '{print $1}') echo -e "Genre : $Genre" echo done #Remove temp file rm $NameOfTempFile ;; add) #Download All Local Radio Stations Index wget -q --output-document=$NameOfTempFile $DownloadURL #Process Index cat $NameOfTempFile | grep audio | grep station | while read LINE ; do #Get Station Name Station=$(echo $LINE | gawk -F'"' '{ print $4}' | gawk -F'(' '{print $1}' | gawk -F"|" '{print $1}') #Display what we are doing echo -e "Adding Radio Station : \e[01;33m$Station\e[00m To LinuxMCE" #Get Url Url=$(echo $LINE | gawk -F'&f' '{print $1}' | gawk -F'"' '{system("curl --silent "$6" | head -n1")}') #Get Genre Genre=$(echo $LINE | gawk -F'"' '{ print $4}' | gawk -F'(' '{print $2}' | gawk -F')' '{print $1}') #Check if Url is already in Database if ! [ $(mysql pluto_media -ss -e "select PK_File from File where Filename='$Url';") ]; then #Add Url to Database PK_File=$(mysql pluto_media -ss -e "insert into File (EK_MediaType,DateAdded,Filename,Missing,IsDirectory,IsNew) VALUES(43,NOW(),'$Url',0,0,1);select LAST_INSERT_ID() from File;" | tail -n1) #Add Station Name to Database FK_Attribute=$(mysql pluto_media -ss -e "insert into Attribute (FK_AttributeType,Name) VALUES(10,'$Station');select LAST_INSERT_ID() from Attribute;" | tail -n1) #Hook Station Name to Url mysql pluto_media -e "insert into File_Attribute (FK_File,FK_Attribute,Track,Section) VALUES('$PK_File','$FK_Attribute',0,0);" #Check If Genre Exists in Database GenreID=$(mysql pluto_media -ss -e "select PK_Attribute from Attribute where FK_AttributeType=8 and Name='$Genre';"| tail -n1) if ! [ $(echo $GenreID) ]; then #Add Genre to Database GenreID=$(mysql pluto_media -ss -e "insert into Attribute (FK_AttributeType,Name) VALUES(8,'$Genre');select LAST_INSERT_ID() from Attribute;" | tail -n1) fi #Hook Genre to Url mysql pluto_media -e "insert into File_Attribute (FK_File,FK_Attribute,Track,Section) VALUES('$PK_File','$GenreID',0,0);" else echo -e "\e[00;31mError\e[00m : URL Already in Database" fi done #Remove temp file rm $NameOfTempFile ;; delete) #Download All Local Radio Stations Index wget -q --output-document=$NameOfTempFile $DownloadURL #Process Index cat $NameOfTempFile | grep audio | grep station | while read LINE ; do #Get Station Name Station=$(echo $LINE | gawk -F'"' '{ print $4}' | gawk -F'(' '{print $1}' | gawk -F"|" '{print $1}') #Display What we are doing echo -e "Deleting Radio Station : \e[01;33m$Station\e[00m From LinuxMCE" #Get Genre Genre=$(echo $LINE | gawk -F'"' '{ print $4}' | gawk -F'(' '{print $2}' | gawk -F')' '{print $1}') #Get Url echo "$LINE" | gawk -F'&f' '{print $1}' | gawk -F'"' '{system("curl --silent "$6"")}' | while read Url ; do #Check if Url is in Database if [ $(mysql pluto_media -ss -e "select PK_File from File where Filename='$Url';" | tail -n1) ]; then #Delete Url from Database PK_File=$(mysql pluto_media -ss -e "select PK_File from File where Filename='$Url';" | tail -n1) mysql pluto_media -e "delete from File where PK_File=$PK_File;" #Delete Station Name PK_Attribute=$(mysql pluto_media -ss -e "select PK_Attribute from Attribute where Name='$Station';") mysql pluto_media -e "delete from Attribute where PK_Attribute=$PK_Attribute;" #Delete Hook for Station Name mysql pluto_media -e "delete from File_Attribute where FK_File="$PK_File" and FK_Attribute="$PK_Attribute";" #Delete Hook for Genre PK_Attribute=$(mysql pluto_media -ss -e "select PK_Attribute from Attribute where Name='$Genre';") mysql pluto_media -e "delete from File_Attribute where FK_File=$PK_File and FK_Attribute=$PK_Attribute;" else echo -e "\e[00;31mError\e[00m : URL not in Database" fi done done #Remove temp file # rm $NameOfTempFile ;; *) echo "usage: $0 {show|add|delete}" exit 1 ;; esac exit 0