UNPKG

node-tunein-radio

Version:

NodeJS Module to browse, search and play streams from TuneIn Radio

378 lines (332 loc) 11.2 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../"> <title data-ice="title">src/index.js | node-tunein-radio</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> <meta name="description" content="NodeJS Module to browse, search and play streams from TuneIn Radio"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="node-tunein-radio"><meta property="twitter:description" content="NodeJS Module to browse, search and play streams from TuneIn Radio"></head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> <a style="position:relative; top:3px;" href="https://github.com/piffio/node-tunein-radio"><img width="20px" src="./image/github.png"></a></header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/index.js~TuneIn.html">TuneIn</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">src/index.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">&apos;use strict&apos;; const axios = require(&apos;axios&apos;); const axiosExtensions = require(&apos;axios-extensions&apos;); const url = require(&apos;url&apos;); const qs = require(&apos;qs&apos;); /** * TuneIn class, the main class implementing the module * * @example * let tuneinOptions = { * protocol: &apos;https&apos;, // Protocol to use, either http or https * cacheRequests: true, // Wheter to cache results or not, default false * cacheTTL: 1000 * 60 * 60, // Cache TTL, default 5 minutes * partnerId: process.env.TUNEIN_PARTNERID, // PartnerID to be used when interacting with the TuneIn API * }; * * let tunein = new TuneIn(tuneinOptions); */ module.exports = class TuneIn { /** * Constructur for a TuneIn object. * @param {object} options this is an options object with all the optionals parameters used to initialise the TuneIn client * @example * let tuneinOptions = { * protocol: &apos;https&apos;, // Protocol to use, either http or https * cacheRequests: true, // Wheter to cache results or not, default false * cacheTTL: 1000 * 60 * 60, // Cache TTL, default 5 minutes * partnerId: process.env.TUNEIN_PARTNERID, // PartnerID to be used when interacting with the TuneIn API * }; */ constructor(options) { options = options || {}; let protocol = options.protocol || &apos;https&apos;; let cacheRequests = options.cacheRequests || false; let cacheTTL = options.cacheTTL || 1000 * 60 * 10; let partnerId = options.partnerId || &apos;&apos;; axios.defaults.headers.post[&apos;Content-Type&apos;] = &apos;application/x-www-form-urlencoded&apos;; axios.defaults.baseURL = protocol + &apos;://opml.radiotime.com&apos;; axios.defaults.params = { render: &apos;json&apos; }; if (cacheRequests === true) { axios.defaults.adapter = axiosExtensions.cacheAdapterEnhancer( axios.defaults.adapter, true, &apos;cache&apos;, cacheTTL ); } if (partnerId != &apos;&apos;) { axios.defaults.params.partnerId = partnerId; } } /** * Search TuneIn for a match * @param {string} query a query string to search for in the TuneIn catalog * @example * let queryString = &apos;rai radio&apos;; * let search = tunein.search(queryString); * search.then(function(results) { * console.log(results); * }); */ search(query) { let req = {}; req.params = {}; req.url = &apos;/Search.ashx&apos;; req.params.query = query; return this._call_tunein_get(req); } /** * @private */ _call_tunein_get(req) { return new Promise( (resolve, reject) =&gt; { axios.get(req.url, req) .then(function(results) { let head = results.data.head; if (head.status != 200) { reject(results.data); return new Error(`TuneIn Request error: ${head.fault}`); } for (var i in results.data.body) { if (results.data.body[i].URL) { results.data.body[i].URLObj = url.parse(results.data.body[i].URL, true); } } return resolve((results.data)); }) .catch(function(err) { return new Error(`TuneIn Request error: ${err}`); }); }); } /** * @private */ _call_tunein_post(req, postData) { return new Promise( (resolve, reject) =&gt; { axios.post(req.url, qs.stringify(postData), req) .then(function(results) { let head = results.data.head; if (head.status != 200) { reject(results.data); return new Error(`TuneIn Request error: ${head.fault}`); } return resolve((results.data)); }) .catch(function(err) { return new Error(`TuneIn Request error: ${err}`); }); }); } /** * Tune a stream. This will return an object containing details for the specific stream, including URL and format. * @param {string} id The ID of the stream to tune, usually resulting from a browsing or a search call */ tune_radio(id) { let req = {}; req.params = {}; req.url = &apos;/Tune.ashx&apos;; req.params.id = id; return this._call_tunein_get(req); } /** * Describe a stream. This method will retrieve detailed information about a stream. * * @param {string} id The ID of the stream to fetch information for * @param {boolean} nowplaying If true it also fetches information about the show being currently aired */ describe(id, nowplaying = false) { let req = {}; req.params = {}; req.url = &apos;/Describe.ashx&apos;; if (nowplaying == true) { req.params.c = &apos;nowplaying&apos;; } req.params.id = id; return this._call_tunein_get(req); } /** * Authenticate with supplied username / password, to get access to favorite and customised results * @param {object} options Options to be passed to the POST call, must contain user&apos;s credential * @example * let authOptions = { * username: &apos;user@example.com&apos;, * password: &apos;secretpassword&apos; * }; * let auth = tunein.authenticate(authOptions); * auth.then(function(results) { * console.log(results[0].AccountId); * }); */ authenticate(options) { options = options || {}; let req = {}; req.url = &apos;/Account.ashx&apos;; req.params = {}; req.params.c = &apos;auth&apos;; return this._call_tunein_post(req, options); } /** * @private */ _call_browse(options, req) { options = options || {}; let c = options.c || &apos;&apos;; let id = options.id || &apos;&apos;; let filter = options.filter || &apos;&apos;; let offset = options.offset || &apos;&apos;; let pivot = options.pivot || &apos;&apos;; let username = options.username || &apos;&apos;; if (c) { req.params.c = c; } if (id) { req.params.id = id; } if (filter) { req.params.filter = filter; } if (offset) { req.params.offset = offset; } if (pivot) { req.params.pivot = pivot; } if (username) { req.params.username = username; } return this._call_tunein_get(req); } /** * Browse a podcast (Show) for episodes details * @param {object} options - Options object including the show ID * @example * let showOptions = { * c: &apos;pbrowse&apos;, * id: &apos;p191418&apos; * }; * let show = tunein.browse_show(showOptions); * show.then(function(results) { * console.log(results); * }); */ browse_show(options) { let req = {}; req.params = {}; req.url = &apos;/Tune.ashx&apos;; return this._call_browse(options, req); } /** * Browse will browse the TuneIn directory depending on the supplied options paramethers * * @param {object} options - Options object for browsing * @return {object} results - A JSON object with results from the remote API */ browse(options) { let req = {}; req.params = {}; req.url = &apos;/Browse.ashx&apos;; return this._call_browse(options, req); } /** * Browse the local streams category * @param {string} username - Currently ignored */ browse_local(username) { return this.browse({c: &apos;local&apos;, username: username}); } /** * Browse the Music streams category * @param {string} username - Currently ignored */ browse_music(username) { return this.browse({c: &apos;music&apos;}); } /** * Browse the talk streams category * @param {string} username - Currently ignored */ browse_talk(username) { return this.browse({c: &apos;talk&apos;}); } /** * Browse the sports streams category * @param {string} username - Currently ignored */ browse_sports(username) { return this.browse({c: &apos;sports&apos;}); } /** * Browse streams locations * @param {string} username - Currently ignored */ browse_locations(username) { return this.browse({id : &apos;r0&apos;}); } /** * Browse streams languages * @param {string} username - Currently ignored */ browse_langs(username) { return this.browse({c: &apos;lang&apos;}); } /** * Browse the podcasts directory * @param {string} username - Currently ignored */ browse_podcast(username) { return this.browse({c: &apos;podcast&apos;}); } /** * Browse the popular streams category * @param {string} username - Currently ignored */ browse_popular(username) { return this.browse({c: &apos;popular&apos;}); } /** * Browse the best streams category * @param {string} username - Currently ignored */ browse_best(username) { return this.browse({c: &apos;best&apos;}); } } </code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.0.4)</span><img src="./image/esdoc-logo-mini-black.png"></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>