hinet-hichannel-taiwan-radio
Version:
取得 HiNet hichannel 台灣電台的 m3u8 串流網址、節目表和其他資訊!
256 lines • 9.82 kB
JavaScript
"use strict";
/*
* Copyright (c) 2014-2022 旋風之音 GoneTone
*
* Website: https://blog.reh.tw/
* GitHub: https://github.com/GoneTone
* Facebook: https://www.facebook.com/GoneToneDY
* Twitter: https://twitter.com/TPGoneTone
*
* _oo0oo_
* o8888888o
* 88" . "88
* (| -_- |)
* 0\ = /0
* ___/`---'\___
* .' \\| |# '.
* / \\||| : |||# \
* / _||||| -:- |||||- \
* | | \\\ - #/ | |
* | \_| ''\---/'' |_/ |
* \ .-\__ '-' ___/-. /
* ___'. .' /--.--\ `. .'___
* ."" '< `.___\_<|>_/___.' >' "".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `_. \_ __\ /__ _/ .-` / /
* =====`-.____`.___ \_____/___.-`___.-'=====
* `=---='
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* 佛祖保佑 永無 BUG
*
* Project GitHub: https://github.com/GoneToneStudio/node-google-play-api
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.HiNetHichannel = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const https_proxy_agent_1 = require("https-proxy-agent");
class HiNetHichannel {
/**
* HiNetHichannel constructor.
*
* @param {Proxy} proxy 代理伺服器 (預設不使用代理)
*/
constructor(proxy) {
Object.defineProperty(this, "_hichannelUrl", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_hichannelRadioPath", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_hichannelImagePath", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_hichannelApiChannelList", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_hichannelApiRanking", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_hichannelApiPlayerUrl", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_hichannelApiProgramList", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_hichannelApiNowProgram", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_axiosInstance", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_hichannelChannelName", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this._hichannelUrl = 'https://hichannel.hinet.net';
this._hichannelRadioPath = '/radio/';
this._hichannelImagePath = '/upload/radio/channel/';
this._hichannelApiChannelList = 'channelList.do';
this._hichannelApiRanking = 'getRanking.do';
this._hichannelApiPlayerUrl = 'cp.do';
this._hichannelApiProgramList = 'getProgramList.do';
this._hichannelApiNowProgram = 'getNowProgram.do';
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36';
this._axiosInstance = axios_1.default.create({
baseURL: this._hichannelUrl,
headers: {
'User-Agent': userAgent,
Referer: `${this._hichannelUrl}${this._hichannelRadioPath}index.do`
}
});
if (proxy)
this._axiosInstance.defaults.httpsAgent = new https_proxy_agent_1.HttpsProxyAgent(proxy.get());
this._hichannelChannelName = '';
}
/**
* 取得電台頻道列表
*
* @param {string} keyword 搜尋關鍵字
*
* @returns {Promise<ChannelListAPIInterface.List[]>}
*/
async getChannels(keyword) {
try {
let response;
if (keyword) {
response = await this._axiosInstance.get(`${this._hichannelRadioPath}${this._hichannelApiChannelList}`, {
params: {
keyword
}
});
}
else {
response = await this._axiosInstance.get(`${this._hichannelRadioPath}${this._hichannelApiChannelList}`);
}
const datas = response.data.list;
return datas.map((data) => {
data.channel_image = `${this._hichannelUrl}${this._hichannelImagePath}${data.channel_image}`;
return data;
});
}
catch (e) {
throw Error(`取得 Hichannel 電台頻道列表時發生錯誤:${e.message}`);
}
}
/**
* 取得熱門排行電台頻道
*
* @returns {Promise<GetRankingAPIInterface.List[]>}
*/
async getRankingChannels() {
try {
const response = await this._axiosInstance.get(`${this._hichannelRadioPath}${this._hichannelApiRanking}`);
const datas = response.data.list;
return datas.map((data) => {
data.channel_image = `${this._hichannelUrl}${this._hichannelImagePath}${data.channel_image}`;
return data;
});
}
catch (e) {
throw Error(`取得 Hichannel 熱門排行電台頻道時發生錯誤:${e.message}`);
}
}
/**
* 取得指定電台頻道資料
*
* @param {string} hichannelChannelName Hichannel 電台頻道名稱
*
* @returns {Promise<GetRankingAPIInterface.List>}
*/
async getChannel(hichannelChannelName) {
try {
const channels = await this.getChannels(hichannelChannelName);
const channel = channels.find(channel => channel.channel_title === hichannelChannelName);
if (channel)
return channel;
}
catch (e) {
throw Error(`取得 Hichannel 電台頻道「${hichannelChannelName}」時發生錯誤:${e.message}`);
}
throw Error(`Hichannel 電台頻道名稱「${hichannelChannelName}」未找到。`);
}
/**
* 設定電台頻道
*
* @param {string} hichannelChannelName Hichannel 電台頻道名稱
*/
setChannel(hichannelChannelName) {
this._hichannelChannelName = hichannelChannelName;
}
/**
* 取得電台頻道 m3u8 串流網址
*
* @returns {Promise<string>}
*/
async getChannelM3u8Url() {
if (this._hichannelChannelName) {
const channel = await this.getChannel(this._hichannelChannelName);
try {
const response = await this._axiosInstance.get(`${this._hichannelRadioPath}${this._hichannelApiPlayerUrl}`, {
params: {
id: channel.channel_id
}
});
const data = response.data;
return data._adc;
}
catch (e) {
throw Error(`取得 Hichannel 電台頻道「${channel.channel_title}」m3u8 網址時發生錯誤:${e.message}`);
}
}
throw Error('未設定 Hichannel 電台頻道,請使用 "hichannel.setChannel(\'電台頻道名稱\')" 來設定頻道。');
}
/**
* 取得電台頻道節目資訊
*
* @returns {Promise<ChannelProgramInfo>}
*/
async getChannelProgramInfo() {
if (this._hichannelChannelName) {
const channel = await this.getChannel(this._hichannelChannelName);
try {
const programListAxios = await this._axiosInstance.get(`${this._hichannelRadioPath}${this._hichannelApiProgramList}`, {
params: {
channelId: channel.channel_id
}
});
const programList = programListAxios.data;
programList.channel_image = `${this._hichannelUrl}${this._hichannelImagePath}${programList.channel_image}`;
const nowProgramAxios = await this._axiosInstance.get(`${this._hichannelRadioPath}${this._hichannelApiNowProgram}`, {
params: {
id: channel.channel_id,
program: 1
}
});
const nowProgram = nowProgramAxios.data;
return { ...programList, ...nowProgram };
}
catch (e) {
throw Error(`取得 Hichannel 電台頻道「${channel.channel_title}」節目資訊時發生錯誤:${e.message}`);
}
}
throw Error('未設定 Hichannel 電台頻道,請使用 "hichannel.setChannel(\'電台頻道名稱\')" 來設定頻道。');
}
}
exports.HiNetHichannel = HiNetHichannel;
//# sourceMappingURL=HiNetHichannel.js.map