shoutcast.js
Go to the documentation of this file.
1 /*
2  * This file contains information proprietary to Nullsoft and Radionomy. For
3  * further information on rules and restrictions governing the use of the
4  * SHOUTcast API, the SHOUTcast Directory and other related SHOUTcast
5  * services, please visit: http://www.shoutcast.com/TermsOfService
6  *
7  * For the API we use there is a Developer ID needed. You are not allowed
8  * to use the key we use here in your own Applications. You can request a
9  * key by filling the Partner Request Form at
10  * http://www.shoutcast.com/Developer#Shoutcast-apiProgram
11  */
12 
13 // Import URL helper
14 Components.utils.import("resource://app/jsmodules/URLUtils.jsm");
15 
16 // This is our Developer ID ("API Key") we need to access the SHOUTcast API.
17 const SB_SHOUTCAST_APIKEY = "ia9p4XYXmOPEtXzL";
18 
19 // The URL parts we'll use to connect to SHOUTcast's API
20 const SB_SHOUTCAST_APIURL = "http://api.shoutcast.com/legacy/";
21 const SB_SHOUTCAST_APITOP500 = "Top500";
22 const SB_SHOUTCAST_APIGENRE = "genresearch";
23 // Params we need in every API call:
24 // - restricting to MP3 to eliminate dependency on
25 // AAC decoder being installed
26 // - the API Key
27 const SB_SHOUTCAST_APIPARAMS = {mt: "audio/mpeg", k: SB_SHOUTCAST_APIKEY};
28 
29 // The URL we'll use to tune into a station.
30 const SB_SHOUTCAST_TUNEURL = "http://yp.shoutcast.com/sbin/tunein-station.pls";
31 
32 // The class doin' ALL stuff that is SHOUTcast-API related
33 var ShoutcastRadio = {
34 
35  // Returns a URL to the pls with the Stream for the given SHOUTcast Stream ID
36  getListenURL : function(aId) {
37  return URLUtils.addQuery(SB_SHOUTCAST_TUNEURL, {id : aId});
38  },
39 
40  // Returns a URL to get the station list for the given genre.
41  getListURL : function(aGenre) {
42  var tmpurl;
43  // If aGenre is null, we'll search for top stations
44  if (aGenre)
45  tmpurl = URLUtils.addQuery(SB_SHOUTCAST_APIURL + SB_SHOUTCAST_APIGENRE, {genre : aGenre});
46  else
48  return URLUtils.addQuery(tmpurl, SB_SHOUTCAST_APIPARAMS);
49  },
50 
51  // Parses the data from the xml text generated by the SHOUTcast API
52  _parseStationListFromXML : function(aXml, aGenre) {
53  var result = [];
54  // Make sure we recived XML
55  if (!aXml)
56  return result;
57  // Go through all stations and add them to result
58  var entries = aXml.getElementsByTagName("station");
59  for (var i = 0; i < entries.length; i++){
60  var ent = entries[i];
61  result.push(
62  {
63  id : ent.getAttribute("id"),
64  // We will use the given genre or the first genre if we're in Top500
65  genre : aGenre ? aGenre : ent.getAttribute("genre").split(" ")[0],
66  bitrate : ent.getAttribute("br"),
67  numListeners : ent.getAttribute("lc"),
68  currentTrack : ent.getAttribute("ct"),
69  // We will remove the '- a SHOUTcast.com member station' at every name's end
70  name : ent.getAttribute("name").split(" - a SHOUTcast.com member station")[0]
71  });
72  }
73  return result;
74  },
75 
76  // Returns a Stream-Array for the given genre
77  getStationList : function(aGenre) {
78  var req = new XMLHttpRequest();
79  // Set aGenre null if we search for the top streams
80  if (aGenre == "sbITop")
81  aGenre = null;
82  req.open("POST",
83  this.getListURL(aGenre), false);
84  req.genre = aGenre;
85  var stationList = [];
86  try {
87  req.send(null);
88  stationList = this._parseStationListFromXML(req.responseXML, aGenre);
89  } catch (e) {} // Drop connection errors, we'll return an empty array
90  return (stationList);
91  }
92 }
const SB_SHOUTCAST_APIURL
Definition: shoutcast.js:20
const SB_SHOUTCAST_APITOP500
Definition: shoutcast.js:21
const SB_SHOUTCAST_APIPARAMS
Definition: shoutcast.js:27
return null
Definition: FeedWriter.js:1143
const SB_SHOUTCAST_APIGENRE
Definition: shoutcast.js:22
const SB_SHOUTCAST_TUNEURL
Definition: shoutcast.js:30
_getSelectedPageStyle s i
const SB_SHOUTCAST_APIKEY
Definition: shoutcast.js:17