Skip to content

Commit

Permalink
feat(core): iptv parser (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
aooiuu committed Sep 3, 2024
1 parent b81667e commit 7d9121a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export * from './exception';
export { analyzerUrl } from './analyzer/analyzerUrl';

export function createRuleManager(rule: Rule, analyzerManager?: AnalyzerManager) {
if (rule.loadJs && typeof rule.loadJs === 'string' && /^\/\/\s*@any-reader\/parser\/iptv\s*$/.test(rule.loadJs)) {
// if (rule.loadJs && typeof rule.loadJs === 'string' && /^\/\/\s*@any-reader\/parser\/iptv\s*$/.test(rule.loadJs)) {
if (rule.loadJs === '// @any-reader/parser/iptv') {
return new IPTV(rule);
}
return new RuleManager(rule, analyzerManager || createAnalyzerManager());
Expand Down
75 changes: 68 additions & 7 deletions packages/core/src/parser/iptv.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,86 @@
import { Rule } from '@any-reader/rule-utils';

interface M3UITtem {
'group-title': string;
'tvg-id': string;
'tvg-logo': string;
url: string;
}

function m3uParse(text: string): M3UITtem[] {
return text
.split('#EXTINF:')
.map((s: string) => {
return Array.from(s.matchAll(/ (.*?)="(.*?)"/g)).reduce((p: any, v: string[]) => {
p[v[1]] = v[2];
const urls = s.split('\n').filter((e) => /^https?:\/\//.test(e));
if (urls.length === 1) {
p.url = urls[0];
}
return p;
}, {});
})
.filter((e) => e.url);
}

export default class IPTV {
private rule: Rule;
private _m3u: M3UITtem[] = [];

constructor(rule: Rule) {
this.rule = rule;
}

async _getM3UList() {
if (this._m3u.length) return;
const res = await fetch(this.rule.host).then((e) => e.text());
this._m3u = m3uParse(res);
}

async search() {
return [];
}
async getChapter() {
return [];
async getChapter(result: string) {
return [
{
url: result,
name: '正文'
}
];
}
async getContent() {
return [];
async getContent(result: string) {
return [result];
}

// 获取分类
async discoverMap() {
return [];
await this._getM3UList();
return this._m3u.reduce((p: any[], v) => {
const row = p.find((e: any) => e.name === v['group-title']);
if (!row) {
p.push({
name: v['group-title'],
pairs: [
{
name: '全部',
value: v['group-title']
}
]
});
}
return p;
}, []);
}
async discover() {
return [];

// 分类下列表
async discover(result: string) {
await this._getM3UList();
return this._m3u
.filter((e) => e['group-title'] === result)
.map((e) => ({
url: e.url,
name: e['tvg-id'],
cover: e['tvg-logo']
}));
}
}

0 comments on commit 7d9121a

Please sign in to comment.