Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

Commit

Permalink
重寫成 TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
GoneTone committed Jun 8, 2022
1 parent 334abcc commit 849e09f
Show file tree
Hide file tree
Showing 23 changed files with 4,664 additions and 4,280 deletions.
15 changes: 11 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
module.exports = {
env: {
commonjs: true,
browser: true,
es2021: true,
node: true,
jest: true
},
extends: [
'standard'
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12
ecmaVersion: 13,
sourceType: 'module'
},
rules: {
}
ignorePatterns: [
'**/dist/*'
],
plugins: [
'@typescript-eslint'
],
rules: {}
}
5 changes: 4 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

matrix:
operating-system: [ ubuntu-latest ]
node-version: [ 16.x ]
node-version: [ 14.x ]

name: NPM Publish
steps:
Expand All @@ -29,6 +29,9 @@ jobs:
- name: Install Dependencies
run: npm install

- name: TypeScript Compiler
run: npm run build

- name: Publish
run: npm publish --access public
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

matrix:
operating-system: [ ubuntu-latest ]
node-version: [ 12.x, 14.x, 15.x, 16.x ]
node-version: [ 14.x, 16.x, 18.x ]

name: Node.js ${{ matrix.node-version }} Test on ${{ matrix.operating-system }}
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.env
/node_modules/
/.idea/
/dist/
84 changes: 20 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

取得 HiNet hichannel 台灣電台的 m3u8 串流網址、節目表和其他資訊!

PHP 版本:[https://github.com/GoneToneStudio/php-hinet-hichannel-taiwan-radio](https://github.com/GoneToneStudio/php-hinet-hichannel-taiwan-radio)
PHP 版本:<https://github.com/GoneToneStudio/php-hinet-hichannel-taiwan-radio>

## 注意

Expand All @@ -11,11 +11,11 @@ PHP 版本:[https://github.com/GoneToneStudio/php-hinet-hichannel-taiwan-radio

## 問題

如果發現任何 BUG,請在此回報:[https://github.com/GoneToneStudio/node-hinet-hichannel-taiwan-radio/issues](https://github.com/GoneToneStudio/node-hinet-hichannel-taiwan-radio/issues)
如果發現任何 BUG,請在此回報:<https://github.com/GoneToneStudio/node-hinet-hichannel-taiwan-radio/issues>

## 安裝

### NPM
需要 Node.js 14.0.0 或更高版本。

```sh-session
npm install hinet-hichannel-taiwan-radio
Expand All @@ -27,76 +27,28 @@ npm install hinet-hichannel-taiwan-radio
2. 點選您想要聽的電台並確認可以播放
3. 複製完整頻道名稱,使用時名稱要完全一樣 (如果不能直接複製可以利用 F12,或者就乖乖用打的XDD)

## 使用方法

### 使用 HiNetHichannel 物件
## 使用範例

```javascript
const hichannel = new HiNetHichannel()
```
const { HiNetHichannel } = require('hinet-hichannel-taiwan-radio')

### 取得所有電台頻道
const hichannel = new HiNetHichannel()

```javascript
/* 取得所有電台頻道列表 */
hichannel.getChannels().then((channels) => {
console.log('所有電台頻道')
console.log('所有電台頻道列表')
console.log(channels)
})
```

### 取得熱門排行電台頻道

```javascript
hichannel.getRankingChannels().then((channels) => {
console.log('熱門排行電台頻道:')
/* 利用關鍵字搜尋取得電台頻道列表 */
hichannel.getChannels('警廣').then((channels) => {
console.log('關鍵字搜尋電台頻道列表:')
console.log(channels)
})
```

### 取得指定電台頻道資料

```javascript
hichannel.getChannel('KISS RADIO 大眾廣播電台').then((channel) => { // 請輸入完整頻道名稱
console.log('指定電台頻道資料:')
console.log(channel)
})
```

### 設定電台頻道

```javascript
hichannel.setChannel('KISS RADIO 大眾廣播電台') // 請輸入完整頻道名稱
```

### 取得電台頻道 m3u8 串流網址

```javascript
hichannel.getChannelM3u8Url().then((m3u8Url) => {
console.log(`電台頻道 m3u8 串流網址:${m3u8Url}`)
})
```

### 取得電台頻道節目資訊

```javascript
hichannel.getChannelProgramInfo().then((info) => {
console.log('電台頻道節目資訊:')
console.log(info)
})
```

## 範例

### 完整範例

```javascript
const { HiNetHichannel } = require('hinet-hichannel-taiwan-radio')

const hichannel = new HiNetHichannel()

/* 取得所有電台頻道 */
hichannel.getChannels().then((channels) => {
console.log('所有電台頻道:')
/* 取得熱門排行電台頻道列表 */
hichannel.getRankingChannels().then((channels) => {
console.log('熱門排行電台頻道列表:')
console.log(channels)
})

Expand All @@ -111,7 +63,7 @@ hichannel.setChannel('KISS RADIO 大眾廣播電台') // 請輸入完整頻道

/* 取得電台頻道 m3u8 串流網址 */
hichannel.getChannelM3u8Url().then((m3u8Url) => {
console.log(`m3u8 串流網址:${m3u8Url}`)
console.log(`電台頻道 m3u8 串流網址:${m3u8Url}`)
})

/* 取得電台頻道節目資訊 */
Expand All @@ -129,7 +81,11 @@ hichannel.getChannelProgramInfo().then((info) => {
const { HiNetHichannel, Proxy } = require('hinet-hichannel-taiwan-radio')

/* 連線到 Proxy 伺服器 */
const proxy = new Proxy('主機名', 3128, 'http')
const proxy = new Proxy({
host: '主機名',
port: 3128,
protocol: 'http'
})
// proxy.login('帳號', '密碼') // 如果 Proxy 伺服器需要驗證,請調用這登入

const hichannel = new HiNetHichannel(proxy)
Expand Down
89 changes: 89 additions & 0 deletions __tests__/HiNetHichannel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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
*/

import dotenv from 'dotenv'
import { HiNetHichannel, Proxy } from '../src'

dotenv.config()

describe('HiNetHichannel', () => {
let hichannel: HiNetHichannel
test('Init', () => {
const proxy = new Proxy({
host: 'gonetone.reh.tw',
port: 3128,
protocol: 'http'
})
proxy.login(process.env.PROXY_TEST_USERNAME as string, process.env.PROXY_TEST_PASSWORD as string)

hichannel = new HiNetHichannel(proxy)
})

test('Get all channels, "length > 0" return "true"', async () => {
const channels = await hichannel.getChannels()
expect(channels.length > 0).toBe(true)
})

test('Get channels by keyword "警廣", "length > 0" return "true"', async () => {
const channels = await hichannel.getChannels('警廣')
expect(channels.length > 0).toBe(true)
})

test('Get ranking channels, "length > 0" return "true"', async () => {
const channels = await hichannel.getRankingChannels()
expect(channels.length > 0).toBe(true)
})

test('Get specified channel "KISS RADIO 大眾廣播電台" data "channel_title", return "KISS RADIO 大眾廣播電台"', async () => {
const channel = await hichannel.getChannel('KISS RADIO 大眾廣播電台')
expect(channel.channel_title).toBe('KISS RADIO 大眾廣播電台')
})

test('Set channel "KISS RADIO 大眾廣播電台"', () => {
hichannel.setChannel('KISS RADIO 大眾廣播電台')
})

test('Get channel "KISS RADIO 大眾廣播電台" m3u8 url, return url match RegExp', async () => {
const m3u8Url = await hichannel.getChannelM3u8Url()
expect(m3u8Url).toMatch(/https?:\/\/(([a-zA-Z]|[a-zA-Z][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z]|[A-Za-z][A-Za-z\d-]*[A-Za-z\d])\/live\/[a-zA-Z\d]+\/playlist\.m3u8\?token=(.*)&expires=(.*)/i)
})

test('Get channel "KISS RADIO 大眾廣播電台" program info "channel_title", return "KISS RADIO 大眾廣播電台"', async () => {
const programInfo = await hichannel.getChannelProgramInfo()
expect(programInfo.channel_title).toBe('KISS RADIO 大眾廣播電台')
})

test('Get channel "KISS RADIO 大眾廣播電台" program info "channel_type", return "音樂"', async () => {
const programInfo = await hichannel.getChannelProgramInfo()
expect(programInfo.channel_type).toBe('音樂')
})
})
24 changes: 21 additions & 3 deletions examples/all_demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,33 @@ const { HiNetHichannel } = require('..')
* 如果運行此程式的伺服器不在台灣,請設定台灣的 Proxy 伺服器,否則取得的串流網址會驗證失敗 (HTTP 403 Forbidden),
* 但如果播放端 IP 和用來取得 m3u8 串流網址的伺服器 IP 不同一樣會被阻擋就是了,可以嘗試在伺服端處理好在串流給播放端,就二次串流。
*/
// const proxy = new Proxy('主機名', 3128, 'http')
/*
const proxy = new Proxy({
host: '主機名',
port: 3128,
protocol: 'http'
})
*/
// proxy.login('帳號', '密碼') // 如果 Proxy 伺服器需要驗證,請調用這登入

const hichannel = new HiNetHichannel()
// const hichannel = new HiNetHichannel(proxy) // Proxy

/* 取得所有電台頻道 */
/* 取得所有電台頻道列表 */
hichannel.getChannels().then((channels) => {
console.log('所有電台頻道:')
console.log('所有電台頻道列表:')
console.log(channels)
})

/* 利用關鍵字搜尋取得電台頻道列表 */
hichannel.getChannels('警廣').then((channels) => {
console.log('關鍵字搜尋電台頻道列表:')
console.log(channels)
})

/* 取得熱門排行電台頻道列表 */
hichannel.getRankingChannels().then((channels) => {
console.log('熱門排行電台頻道列表:')
console.log(channels)
})

Expand Down
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
coverageDirectory: 'coverage',
preset: 'ts-jest',
testEnvironment: 'node'
}
Loading

0 comments on commit 849e09f

Please sign in to comment.