Skip to content

Commit

Permalink
Merge pull request #206 from hjdhnx/main
Browse files Browse the repository at this point in the history
写源工具导出文件默认为服务目录。修复导出识别title匹配不准确的问题
  • Loading branch information
Hiram-Wong authored May 14, 2024
2 parents 4a8d96d + 22b9b54 commit 469436c
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 47 deletions.
10 changes: 6 additions & 4 deletions src/main/core/server/routes/lab/ad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios';
import logger from '../../../logger';

if (typeof Array.prototype.toReversed !== 'function') {
Array.prototype.toReversed = function () {
Array.prototype.toReversed = function() {
const clonedList = this.slice();
// 倒序新数组
const reversedList = clonedList.reverse();
Expand Down Expand Up @@ -34,10 +34,12 @@ const urljoin = (fromPath, nowPath) => {
/**
* 智能对比去除广告。支持嵌套m3u8。只需要传入播放地址
* @param m3u8_url m3u8播放地址
* @param headers 自定义访问m3u8的请求头,可以不传
* @returns {string}
*/
const fixAdM3u8Ai = async(m3u8_url: string) => {
const fixAdM3u8Ai = async (m3u8_url: string, headers: object = null) => {
let ts = new Date().getTime();
let option = headers ? { headers: headers } : {};

function b(s1, s2) {
let i = 0;
Expand All @@ -55,7 +57,7 @@ const fixAdM3u8Ai = async(m3u8_url: string) => {
}

//log('播放的地址:' + m3u8_url);
const m3u8_response = await axios.get(m3u8_url);
const m3u8_response = await axios.get(m3u8_url, option);
let m3u8 = m3u8_response.data;
//log('m3u8处理前:' + m3u8);
m3u8 = m3u8
Expand All @@ -74,7 +76,7 @@ const fixAdM3u8Ai = async(m3u8_url: string) => {
if (last_url.includes('.m3u8') && last_url !== m3u8_url) {
m3u8_url = urljoin(m3u8_url, last_url);
logger.info('嵌套的m3u8_url:' + m3u8_url);
const m3u8_nest_response = await axios.get(m3u8_url);
const m3u8_nest_response = await axios.get(m3u8_url, option);
m3u8 = m3u8_nest_response.data;
}
//log('----处理有广告的地址----');
Expand Down
50 changes: 35 additions & 15 deletions src/main/core/server/routes/lab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const api: FastifyPluginAsync = async (fastify): Promise<void> => {
try {
// @ts-ignore
db = await req.server.db.getData('/debug-edit-source');
} catch (error) {}
} catch (error) {
}

if (action === 'all') {
res = db?.text || '';
Expand All @@ -46,7 +47,8 @@ const api: FastifyPluginAsync = async (fastify): Promise<void> => {
try {
// @ts-ignore
db = await req.server.db.getData('/debug-edit-source');
} catch (error) {}
} catch (error) {
}
const { oldSiteId: dbOldSiteId, debugSiteId: dbDebugSiteId } = db;

if (dbDebugSiteId) {
Expand Down Expand Up @@ -87,7 +89,8 @@ const api: FastifyPluginAsync = async (fastify): Promise<void> => {
try {
// @ts-ignore
db = await req.server.db.getData('/debug-edit-source');
} catch (error) {}
} catch (error) {
}

const { oldSiteId: dbOldSiteId, debugSiteId: dbDebugSiteId } = db;

Expand All @@ -110,35 +113,52 @@ const api: FastifyPluginAsync = async (fastify): Promise<void> => {
`/${API_VERSION}/lab/removeAd`,
async (req: FastifyRequest<{ Querystring: { [key: string]: string } }>, reply: FastifyReply) => {
try {
const { url, type } = req.query;
const { url, type, headers } = req.query;
let _headers = null;
if (headers) {
try {
_headers = JSON.parse(headers);
} catch (e) {
}
}

let data = {
code: 500,
msg: 'fail',
url,
url: url,
headers: _headers,
type: type,
content: null,
};

if (type !== 'm3u8') {
if (!type || !type.includes('.m3u8')) {
data.msg = 'fail';
data.code = 500;
} else {
// 逻辑处理
const content = await fixAdM3u8Ai(url);
const content = await fixAdM3u8Ai(url, _headers);
// 结果处理
if (content.indexOf('.ts') > -1) {
const id = nanoid();
// @ts-ignore
await req.server.db.push(`/ad/${id}`, content);
// const id = nanoid();
// // @ts-ignore
// await req.server.db.push(`/ad/${id}`, content);
data.code = 200;
data.url = `http://127.0.0.1:9978/api/v1/lab/stream/${id}.m3u8`;
// data.url = `http://127.0.0.1:9978/api/v1/lab/stream/${id}.m3u8`;
data.msg = 'success';
data.content = content;
} else {
data.code = 500;
data.msg = 'fail';
}
}

reply.code(200).send(data);
if (data.content) {
reply.code(data.code).header('Content-Type', 'application/vnd.apple.mpegurl').send(data.content);
} else if (url && url.startsWith('http')) {
reply.redirect(302, url);
} else {
reply.code(200).send(data);
}
// reply.code(200).send(data);
} catch (err) {
reply.code(500).send(err);
}
Expand All @@ -161,11 +181,11 @@ const api: FastifyPluginAsync = async (fastify): Promise<void> => {
try {
// @ts-ignore
const db = await req.server.db.getData(`/ad/${id}`);
logger.info(db)
logger.info(db);

data = db;
} catch (err) {
logger.info(err)
logger.info(err);
code = 500;
}

Expand Down
3 changes: 2 additions & 1 deletion src/renderer/src/api/lab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ export function fetchStream(url) {
});
}

export function setStream(url, type) {
export function setStream(url, type, headers: object = null) {
return request({
url: `/v1/lab/removeAd`,
params: {
url,
type,
headers,
},
method: 'GET',
});
Expand Down
31 changes: 25 additions & 6 deletions src/renderer/src/pages/setting/tool/EditSource.vue
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ import { createDependencyProposals } from '@/utils/drpy/drpy_suggestions/drpy_su
import drpyObjectInner from '@/utils/drpy/drpy_suggestions/drpy_object_inner.ts?raw';
const remote = window.require('@electron/remote');
const { app } = remote.require('electron');
const { join } = remote.require('path');
const fse = remote.require('fs-extra');
const router = useRouter();
const emitReload = useEventBus<string>('film-reload');
const storeSetting = useSettingStore();
Expand Down Expand Up @@ -649,15 +652,31 @@ const exportFileEvent = async () => {
try {
let title = 'source.js'
const regex = /title:\s*'([^']+)'/i; // 匹配"title": 后紧跟着双引号内的任何非双引号字符
const matchResult = regex.exec(str);
if (matchResult) {
title = `${matchResult[1]}.js`; // 匹配到的title值
};
// const regex = /title:\s*'([^']+)'/i; // 匹配"title": 后紧跟着双引号内的任何非双引号字符
// const matchResult = regex.exec(str);
// if (matchResult) {
// title = `${matchResult[1]}.js`; // 匹配到的title值
// };
try {
title = str.match(/title:(.*?),/)[1].replace(/['|"]/g,'').trim();
}catch (e) {
console.log(`匹配导出文件名发生了错误:${e.message}`)
}
// console.log(window.electron.process.env.APPDATA);
// const app = remote.require('electron').app;
// const { join } = remote.require('path');
// const dataPath = path.join(window.electron.process.env.APPDATA,window.electron.process.env.npm_package_name);
const dataPath = app.getPath('userData');
const BASE_PATH = join(dataPath, 'file/js'); // 文件路径
// 如果路径存在则创建
fse.ensureDirSync(BASE_PATH)
console.log(BASE_PATH);
title = join(BASE_PATH,title);
const saveDialogResult = await remote.dialog.showSaveDialog(remote.getCurrentWindow(), {
defaultPath: title,
filters: [{ name: 'JavaScript Files', extensions: ['js'] }],
filters: [{ name: 'JavaScript Files', extensions: ['js']},{ name: 'ALL', extensions: ['*'] }],
});
if (!saveDialogResult.canceled) {
Expand Down
33 changes: 22 additions & 11 deletions src/renderer/src/utils/common/film.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const fetchAnalyzeData = async (): Promise<{ default: any; flag: any[]; active:
};

// Ad
const removeAd = async (url: string, type: string) => {
const removeAd = async (url: string, type: string, headers: object = null) => {
console.log('[film_common][removeAd][start]开始移除广告流程');
let data = {
url,
Expand All @@ -178,7 +178,7 @@ const removeAd = async (url: string, type: string) => {
};
try {
if (type === 'm3u8') {
data = await setStream(url, type);
data = await setStream(url, '.m3u8', headers);
}
console.log(`[film_common][removeAd][return]`, data);
} catch (err) {
Expand All @@ -204,30 +204,33 @@ const removeAd = async (url: string, type: string) => {
* @param site 源信息
* @param analyze 解析 url type flag
* @param flimSource 当前选中线路
* @param adFlag 是否去广告
* @returns
*/
const playHelper = async (snifferMode, url: string, site, analyze, flimSource, adFlag = false) => {
console.log(`[film_common][playHelper][before_start]准备处理地址:${url}`);
console.log(`[film_common][playHelper][start]播放处理流程开始`);

let data: { url: string; mediaType: string | null; isOfficial: boolean } = {
let data: { url: string; mediaType: string; isOfficial: boolean; headers: object } = {
url: '',
mediaType: '',
isOfficial: false,
headers: null,
};

try {
let playerUrl = url;
let script: string = '';
let extra: string = '';
let isOfficial: boolean = false;
let headers: object = null;
let parse = true;
let playData: any = { playUrl: url, script: '', extra: '', parse: parse };

// 解析播放
const jxPlay = async (url: string, analyze: any, snifferMode: any): Promise<any> => {
let playerUrl = url;
const urlObj = url.startsWith('http') ? new URL(url) : null;
const urlObj = url && url.startsWith('http') ? new URL(url) : null;
const hostname = urlObj?.hostname;

// 官方解析条件
Expand Down Expand Up @@ -308,7 +311,7 @@ const playHelper = async (snifferMode, url: string, site, analyze, flimSource, a
if (playerUrl) {
const mediaType = await checkMediaType(playerUrl);
if (mediaType !== 'unknown' && mediaType !== 'error') {
data = { url: playerUrl, mediaType, isOfficial };
data = { url: playerUrl, mediaType, isOfficial, headers };
return;
}
}
Expand All @@ -323,7 +326,9 @@ const playHelper = async (snifferMode, url: string, site, analyze, flimSource, a
: '';

const snifferPlayUrl = `${snifferApi}?url=${playerUrl}&script=${script}${extra}`;
data.url = await sniffer(snifferMode.type, snifferPlayUrl);
let snifferResult = await sniffer(snifferMode.type, snifferPlayUrl);
data.url = snifferResult.data;
data.headers = snifferResult.headers;
data.mediaType = (await checkMediaType(data.url)) || 'm3u8';
data.isOfficial = isOfficial;

Expand All @@ -332,10 +337,16 @@ const playHelper = async (snifferMode, url: string, site, analyze, flimSource, a
console.error(`[film_common][playHelper][error]`, err);
} finally {
console.log(`[film_common][playHelper][end]播放处理流程结束`);
if (adFlag) {
const response = await removeAd(data.url, data.mediaType!);
if (response.code === 200) data.url = response?.url;
};
if (adFlag && data.url && !data.url.startsWith('http://127.0.0.1') && data.mediaType.includes('m3u8')) {
// const response = await removeAd(data.url, data.mediaType!, data.headers);
// if (response.code === 200) data.url = response?.url;
data.url = `http://127.0.0.1:9978/api/v1/lab/removeAd?url=${data.url}`;
if (data.headers && Object.keys(data.headers).length > 0) {
data.url += '&headers=' + JSON.stringify(data.headers);
}
data.url += '&type=.m3u8';
console.log(`本地代理去广告链接: ${data.url}`);
}
return data;
}
};
Expand Down Expand Up @@ -517,7 +528,7 @@ const fetchJxWebPlayUrlHelper = async (type: string, url: string): Promise<strin
let data: string = '';
try {
const res = await sniffer(type, url);
data = res;
data = res.data;
console.log(`[film_common][fetchJxWebPlayUrlHelper][return]`, data);
} catch (err) {
console.log(`[film_common][fetchJxWebPlayUrlHelper][error]`, err);
Expand Down
10 changes: 6 additions & 4 deletions src/renderer/src/utils/drpy/drpy3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const pre = () => {
let rule = {};
// @ts-ignore
let vercode = typeof pdfl === 'function' ? 'drpy3.1' : 'drpy3';
const VERSION = `${vercode} 3.9.50beta11 202400512`;
const VERSION = `${vercode} 3.9.50beta12 202400514`;
/** 已知问题记录
* 1.影魔的jinjia2引擎不支持 {{fl}}对象直接渲染 (有能力解决的话尽量解决下,支持对象直接渲染字符串转义,如果加了|safe就不转义)[影魔牛逼,最新的文件发现这问题已经解决了]
* Array.prototype.append = Array.prototype.push; 这种js执行后有毛病,for in 循环列表会把属性给打印出来 (这个大毛病需要重点排除一下)
Expand Down Expand Up @@ -926,10 +926,12 @@ const fixAdM3u8 = (m3u8_text: string, m3u8_url: string, ad_remove: string) => {
/**
* 智能对比去除广告。支持嵌套m3u8。只需要传入播放地址
* @param m3u8_url m3u8播放地址
* @param headers 自定义访问m3u8的请求头,可以不传
* @returns {string}
*/
const fixAdM3u8Ai = (m3u8_url: string) => {
const fixAdM3u8Ai = (m3u8_url: string, headers:object=null) => {
let ts = new Date().getTime();
let option = headers ? {headers:headers}:{};

function b(s1, s2) {
let i = 0;
Expand All @@ -947,7 +949,7 @@ const fixAdM3u8Ai = (m3u8_url: string) => {
}

//log('播放的地址:' + m3u8_url);
let m3u8 = request(m3u8_url);
let m3u8 = request(m3u8_url, option);
//log('m3u8处理前:' + m3u8);
m3u8 = m3u8
.trim()
Expand All @@ -965,7 +967,7 @@ const fixAdM3u8Ai = (m3u8_url: string) => {
if (last_url.includes('.m3u8') && last_url !== m3u8_url) {
m3u8_url = urljoin2(m3u8_url, last_url);
log('嵌套的m3u8_url:' + m3u8_url);
m3u8 = request(m3u8_url);
m3u8 = request(m3u8_url, option);
}
//log('----处理有广告的地址----');
let s = m3u8
Expand Down
Loading

0 comments on commit 469436c

Please sign in to comment.