UNPKG

radio-browser-api

Version:

Wrapper for free and open-source radio browser api: https://api.radio-browser.info/.

3 lines (2 loc) 5.24 kB
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var s=arguments[e];for(var a in s)({}).hasOwnProperty.call(s,a)&&(t[a]=s[a])}return t},t.apply(null,arguments)}const e={name:"name",url:"url",homepage:"homepage",favicon:"favicon",tags:"tags",country:"country",state:"state",language:"language",votes:"votes",codec:"codec",bitrate:"bitrate",lastCheckOK:"lastCheckOK",lastCheckTime:"lastCheckTime",clickTimeStamp:"clickTimeStamp",clickCount:"clickCount",clickTrend:"clickTrend",random:"random"},s={byUuid:"byUuid",byName:"byName",byNameExact:"byNameExact",byCodec:"byCodec",byCodexExact:"byCodecExact",byCountry:"byCountry",byCountryExact:"byCountryExact",byCountryCodeExact:"byCountryCodeExact",byState:"byState",byStateExact:"byStateExact",byLanguage:"byLanguage",byLanguageExact:"byLanguageExact",byTag:"byTag",byTagExact:"byTagExact"};class a{constructor(t,e=!0){if(this.appName=void 0,this.hideBroken=void 0,this.baseUrl=void 0,this.fetchConfig={method:"GET",redirect:"follow"},this.appName=t,this.hideBroken=e,!t)throw new Error("appName is required");this.fetchConfig.headers={"user-agent":this.appName}}async resolveBaseUrl(t={}){let e;const s=await fetch("https://all.api.radio-browser.info/json/servers",t);if(s.ok)return e=await s.json(),e;throw s}setBaseUrl(t){this.baseUrl=t}getBaseUrl(){return this.baseUrl}async getCountries(t,e,s){return this.runRequest(this.buildRequest("countries",t,e),s)}async getCountryCodes(t,e,s){return t=t?`${t.toUpperCase()}`:"",this.runRequest(this.buildRequest("countrycodes",t,e),s)}async getCodecs(t,e){return this.runRequest(this.buildRequest("codecs","",t),e)}async getCountryStates(t,e,s){return this.runRequest(this.buildRequest("states",t,e),s)}async getLanguages(t,e,s){return this.runRequest(this.buildRequest("languages",t,e),s)}async getTags(t,e,s){return t=t?t.toLowerCase():"",this.runRequest(this.buildRequest("tags",t,e),s)}async getStationsBy(t,e,a,i,n=!1){if(!s[t])throw new Error(`search type does not exist: ${t}`);e=e?e.toLowerCase():"";const o=await this.runRequest(this.buildRequest(`stations/${t.toLowerCase()}`,e,a),i);return this.normalizeStations(o,n)}normalizeStations(t,e=!1){const s=[],a={};for(const i of t){if(e){const t=`${i.name.toLowerCase().trim()}${i.url.toLowerCase().trim()}`;if(a[t])continue;a[t]=!0}const t={changeId:i.changeuuid,id:i.stationuuid,name:i.name,url:i.url,urlResolved:i.url_resolved,homepage:i.homepage,favicon:i.favicon,country:i.country,countryCode:i.countrycode,state:i.state,votes:i.votes,codec:i.codec,bitrate:i.bitrate,clickCount:i.clickcount,clickTrend:i.clicktrend,hls:Boolean(i.hls),lastCheckOk:Boolean(i.lastcheckok),lastChangeTime:new Date(i.lastchangetime),lastCheckOkTime:new Date(i.lastcheckoktime),clickTimestamp:new Date(i.clicktimestamp),lastLocalCheckTime:new Date(i.lastlocalchecktime),language:i.language.split(","),lastCheckTime:new Date(i.lastchecktime),geoLat:i.geo_lat,geoLong:i.geo_long,tags:Array.from(new Set(i.tags.split(","))).filter(t=>t.length>0&&t.length<10)};s.push(t)}return s}async getAllStations(t,e,s=!1){const a=await this.runRequest(this.buildRequest("stations","",t),e);return this.normalizeStations(a,s)}async searchStations(t,e,s=!1){const a=await this.runRequest(this.buildRequest("stations/search",void 0,t),e);return this.normalizeStations(a,s)}async getStationsByClicks(t,e){return this.resolveGetStations("topclick",t,e)}async getStationsByVotes(t,e){return this.resolveGetStations("topvote",t,e)}async getStationsByRecentClicks(t,e){return this.resolveGetStations("lastclick",t,e)}async sendStationClick(t,e){return this.runRequest(this.buildRequest("url",t,void 0,!1),e)}async voteForStation(t,e){return this.runRequest(this.buildRequest("vote",t),e)}async getStationsById(t,e){const s=t.join(","),a=await this.runRequest(this.buildRequest(`stations/byuuid?uuids=${s}`,void 0,void 0,!1),e);return this.normalizeStations(a)}async getStationByUrl(t,e){const s=await this.runRequest(this.buildRequest(`stations/byurl?url=${t}`,void 0,void 0,!1),e);return this.normalizeStations(s)}async resolveGetStations(t,e,s){const a=e?`/${e}`:"",i=await this.runRequest(this.buildRequest(`stations/${t}${a}`,void 0,void 0,!1),s);return this.normalizeStations(i)}buildRequest(e,s,a,i=!0){let n;return s=s?`/${encodeURIComponent(s)}`:"",a&&(n=t({},a),"tagList"in n&&Array.isArray(n.tagList)&&(n.tagList=[...n.tagList]),i&&void 0===n.hideBroken&&(n.hideBroken=this.hideBroken)),`${e}${s}${n?this.createQueryParams(n):""}`}async runRequest(e,s={}){const a=t({},this.fetchConfig,s,{headers:t({},this.fetchConfig.headers,s.headers)});if(!this.baseUrl){const t=await this.resolveBaseUrl(),e=Math.floor(Math.random()*t.length);this.baseUrl=`https://${t[e].name}`}const i=await fetch(`${this.baseUrl}/json/${e}`,a);if(i.ok)return i.json();throw i}createQueryParams(t){let e="";if(t)for(const[s,a]of Object.entries(t)){let t=s.toLowerCase();switch(t){case"hasgeoinfo":t="has_geo_info";break;case"hidebroken":t="hidebroken";break;case"taglist":t="tagList"}e+=`&${t}=${encodeURIComponent(a)}`}return e.length?`?${e.slice(1)}`:""}}a.version="6.0.3";export{a as RadioBrowserApi,e as StationSearchOrder,s as StationSearchType}; //# sourceMappingURL=index.modern.js.map