From DriveCast Wiki
/***************************************************************
** Drivecast API Javascript library
** Author: Luca Restagno
** E-mail: luca.restagno@gmail.com
**
** This library allows to make requests to the Drivecast API
** A Base64 Javascript library is needed
** I used the webtoolkit.base64.js in this library
** It is included at the end of this library
**
** A JSON Javascript library is needed
** I used the JSON.js in this library
** It is included at the end of this library
**
** Simple usage:
var d = new Drivecast();
d.getAuthorizationCode("username","password")
d.setAPIkey("apikey")
d.makeRequest("resource", "action", {
object : "",
onFinish : callbackFunction
onError : errorCallbackFunction
})
**
** Read the official Drivecast API Documentation page at
** http://wiki.drivecast.eu/index.php/Drivecast_API
** and the Drivecast API Javascript Library documentation page at
** http://wiki.drivecast.eu/index.php/Javascript_Library
** for more information
**
** License
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
****************************************************************/
// Global var to permit the concurrency of requests
var DrivecastRequests = new Array();
function Drivecast() {
// API Translator Url
this._proxy = "http://beta.drivecast.eu/lib/1.0/DrivecastAPIGetTranslator.php"
this._AuthCode = ""
this._APIkey = ""
this.result = ""
this.statusCode = ""
this.statusDesc = ""
this.options = ""
this.scriptElId = ""
this.callbackFunction = function() {}
this.errorFunction = function() {}
this.used = false
this.timestamp = ""
this.loopCounter = 0
this.base64check = 0
this.incrementalId = (Date.parse(new Date()))+Math.floor(Math.random()*100);
}
// return and set the AuthCode
Drivecast.prototype.getAuthorizationCode = function( user , password ) {
if(typeof(Base64) !== 'undefined'){
var auth_code = Base64.encode( user + ':' + password )
this._AuthCode = auth_code
return auth_code
}
else{
this.base64check++
if(this.base64check<2)
setTimeout(function( that ) { that.getAuthorizationCode(user , password); },1000,this)
else
return false;
}
}
Drivecast.prototype.setAuthorizationCode = function( code ) {
this._AuthCode = code
}
Drivecast.prototype.setAPIkey = function( apikey ) {
this._APIkey = apikey
}
/*******************************************
* makeRequest function
* Arguments:
*
* resource
* "feed"
* "playlist"
* "library"
* "radio"
* "podcast"
* "recording"
*
* action
* "read"
* "create"
* "modify"
* "delete"
*
* options:
* object: a JSON objet to pass only if you are making a "modify" or "create" action
* onFinish: callback function triggered when the request is completed
* onError: callback function triggered when an error occurs
*********************************************/
Drivecast.prototype.makeRequest = function(resource, action, options){
if(!this.used){
this.used = true
}
else{
setTimeout(function( that ) { that.makeRequest(resource, action, options); },100,this)
return;
}
var obj = ""
if(options){
if(options.object){
obj = JSON.stringify(options.object);
}
}
this.timestamp = ""+Date.parse(new Date())+""
var temp_obj = "";
if(obj != "")
temp_obj = "&obj='"+obj+"'"
this.scriptElId = this.inc(this._proxy+"?action="+action+"&resource="+resource+""+temp_obj+"&authcode="+this._AuthCode+"&apikey="+this._APIkey+"×tamp="+this.timestamp, true)
if(options){
if(options.onError){
this.onError = options.onError
}
else{
this.onError = null
}
if(options.onFinish){
this.onFinish = options.onFinish
}
else{
this.onFinish = null
}
}
this.checkLoad()
}
Drivecast.prototype.onFinish = this.callbackFunction
Drivecast.prototype.onError = this.errorFunction
Drivecast.prototype.checkLoad = function (){
if(this.loopCounter == 50)
return false
this.loopCounter++
if((DrivecastRequests) && DrivecastRequests.length>0){
for(var i=0; i<DrivecastRequests.length; i++){
if(DrivecastRequests[i].timestamp==this.timestamp){
var myDrivecastRequest = DrivecastRequests[i]
// I must do these here, because if the user callback fails,
//the used var is never set to false!!! very dangerous! (infinite loop)
DrivecastRequests.splice(i,1);
this.used = false;
this.loopCounter = 0
// this.removeInc(this.incrementalId)
if(myDrivecastRequest.statusCode !== "200"){
if(this.onError){
this.onError(myDrivecastRequest.statusCode,myDrivecastRequest.statusDesc)
}
return
}
if(this.onFinish){
if(myDrivecastRequest.result)
this.onFinish(myDrivecastRequest.result,myDrivecastRequest.statusCode,myDrivecastRequest.statusDesc)
else
this.onFinish(myDrivecastRequest.statusCode,myDrivecastRequest.statusDesc)
}
return
}
}
}
setTimeout(function( that ) { that.checkLoad();},100,this)
}
Drivecast.prototype.checkResource = function (string){
var check = false
string.indexOf("feed") != -1 ? check=true :
string.indexOf("playlist") != -1 ? check=true :
string.indexOf("library") != -1 ? check=true :
string.indexOf("podcast") != -1 ? check=true :
string.indexOf("recording") != -1 ? check=true :
return check
}
Drivecast.prototype.checkAction = function (string){
switch(string){
case "read":
return true;
break;
case "modify":
return true;
break;
case "create":
return true;
break;
case "delete":
return true;
break;
default:
return false;
break;
}
}
Drivecast.prototype.logger = function (){
this.logDiv = document.createElement("div");
this.logDiv.setAttribute("id","dcLog");
this.logDiv.style.width = "100%";
this.logDiv.style.fontSize = ".9em";
this.logDiv.style.cssFloat = "right";
this.body = document.getElementsByTagName("body").item(0)
this.body.appendChild(this.logDiv)
}
Drivecast.prototype.log = function (txt){
this.logDiv = document.getElementById("dcLog")
if(!this.logDiv){
this.logger()
}
this.html = this.logDiv.innerHTML
this.logDiv.innerHTML = this.html + txt
}
/**
* LibraryItem object
**/
DrivecastLibraryItem = function(author, title, description) {
this.author = author
this.title = title
this.description = description
}
/**
* Recording object
**/
DrivecastRecording = function (radioID, timezone_used, from_time, to_time, repeat, tectype, keep, title){
this.radioID = radioID
this.timezone_used = timezone_used
this.from_time = from_time
this.to_time = to_time
this.repeat = repeat
this.rectype = recording
this.keep = keep
this.title = title
}
/**
* Podcast object
**/
DrivecastPodcast = function (podcast_url, podcast_title, podcast_description){
this.podcast_url = podcast_url
this.podcast_title = podcast_title
this.podcast_description = podcast_description
}
/***************************************************************
** Javascript inc function, includes dynamically an external .js
****************************************************************/
Drivecast.prototype.inc = function(filename, id){
var head = document.getElementsByTagName('head').item(0);
var script = document.createElement('script');
if(id){
script.setAttribute("id","dcScr"+this.incrementalId);
var retId = this.incrementalId;
this.incrementalId++;
}
script.setAttribute("charset","utf-8");
script.setAttribute("src",filename);
script.setAttribute("type","text/javascript");
head.appendChild(script);
return "dcScr"+retId;
}
Drivecast.prototype.removeInc = function(id){
var head = document.getElementsByTagName('head').item(0);
var script = document.getElementById(id)
head.removeChild(script);
}
function addMetaTagUTF8(){
var meta = document.createElement("meta")
meta.setAttribute("http-equiv","Content-Type")
meta.setAttribute("content","text/html;charset=utf-8")
var head = document.getElementsByTagName('head').item(0)
head.appendChild(meta)
}
// adds the meta tag Content-Type UTF-8
addMetaTagUTF8()
var dc = new Drivecast();
// base64 library
dc.inc("http://beta.drivecast.eu/lib/1.0/webtoolkit.base64.js")
// JSON library
dc.inc("http://beta.drivecast.eu/lib/1.0/JSON.js")