Skip to content

Commit

Permalink
Merge pull request #95 from caorushizi/dev/terminal
Browse files Browse the repository at this point in the history
Dev/terminal
  • Loading branch information
caorushizi authored Feb 25, 2024
2 parents 12ca2d9 + da69002 commit 3414e39
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 16 deletions.
Empty file.
Binary file added packages/main/bin/win32/N_m3u8DL-CLI.exe
Binary file not shown.
Binary file removed packages/main/bin/win32/N_m3u8DL-RE.exe
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/main/src/helper/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export const PERSIST_WEBVIEW = "persist:webview";
export const db = resolve(workspace, "app.db");

// bin path
const downloaderBinName = isWin ? "N_m3u8DL-CLI" : "N_m3u8DL-RE";
export const ffmpegPath = resolveBin("ffmpeg");
export const biliDownloaderBin = resolveBin("BBDown");
export const m3u8DownloaderBin = resolveBin("N_m3u8DL-RE");
export const m3u8DownloaderBin = resolveBin(downloaderBinName);
export const videoServerBin = resolveBin("server");

// mobile path
Expand Down
87 changes: 81 additions & 6 deletions packages/main/src/services/DownloadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { TYPES } from "../types";
import ElectronLogger from "../vendor/ElectronLogger";
import ElectronStore from "../vendor/ElectronStore";
import VideoRepository from "../repository/VideoRepository";
import { biliDownloaderBin, m3u8DownloaderBin, stripColors } from "../helper";
import {
biliDownloaderBin,
formatHeaders,
isWin,
m3u8DownloaderBin,
stripColors,
} from "../helper";
import * as pty from "node-pty";
import stripAnsi from "strip-ansi";

Expand Down Expand Up @@ -163,6 +169,7 @@ export default class DownloadService extends EventEmitter {
name: "xterm-color",
cols: 500,
rows: 500,
useConpty: false,
});

if (onMessage) {
Expand All @@ -180,13 +187,12 @@ export default class DownloadService extends EventEmitter {

abortSignal.signal.addEventListener("abort", () => {
ptyProcess.kill();
reject(new Error("AbortError"));
});

ptyProcess.onExit(({ exitCode, signal }) => {
if (exitCode === 0 && (signal === 0 || signal == null)) {
ptyProcess.onExit(({ exitCode }) => {
if (exitCode === 0) {
resolve();
} else if (exitCode === 0 && signal === 1) {
reject(new Error("AbortError"));
} else {
reject(new Error("未知错误"));
}
Expand Down Expand Up @@ -341,13 +347,82 @@ export default class DownloadService extends EventEmitter {
});
}

async m3u8DownloaderWin32(params: DownloadParams): Promise<void> {
const {
id,
abortSignal,
url,
local,
name,
deleteSegments,
headers,
callback,
proxy,
} = params;
const progressReg = /Progress:\s(\d+)\/(\d+)\s\(.+?\).+?\((.+?\/s).*?\)/g;
const isLiveReg = /识别为直播流, 开始录制/g;
const startDownloadReg = /开始下载文件/g;

const spawnParams = [url, "--workDir", local, "--saveName", name];

if (headers) {
spawnParams.push("--headers", formatHeaders(headers));
}

if (deleteSegments) {
spawnParams.push("--enableDelAfterDone");
}

if (proxy) {
spawnParams.push("--proxyAddress", proxy);
}

let isLive = false;
await this._execa(m3u8DownloaderBin, spawnParams, {
abortSignal,
onMessage: (message) => {
if (isLiveReg.test(message) || startDownloadReg.test(message)) {
callback({
id,
type: "ready",
isLive,
cur: "",
total: "",
speed: "",
});
isLive = true;
}

const result = progressReg.exec(message);
if (!result) {
return;
}

const [, cur, total, speed] = result;
const progress: DownloadProgress = {
id,
type: "progress",
cur,
total,
speed,
isLive,
};
callback(progress);
},
});
}

process(params: DownloadParams): Promise<void> {
if (params.type === "bilibili") {
return this.biliDownloader(params);
}

if (params.type === "m3u8") {
return this.m3u8Downloader(params);
if (isWin) {
return this.m3u8DownloaderWin32(params);
} else {
return this.m3u8Downloader(params);
}
}

return Promise.reject();
Expand Down
12 changes: 6 additions & 6 deletions packages/main/src/services/WebviewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BrowserView, session } from "electron";
import { inject, injectable } from "inversify";
import { TYPES } from "../types";
import isDev from "electron-is-dev";
import { PERSIST_WEBVIEW, isWin, mobileUA, pcUA, pluginPath } from "../helper";
import { PERSIST_WEBVIEW, mobileUA, pcUA, pluginPath } from "../helper";
import { ElectronBlocker } from "@cliqz/adblocker-electron";
import fetch from "cross-fetch";
import ElectronLogger from "../vendor/ElectronLogger";
Expand Down Expand Up @@ -128,11 +128,11 @@ export default class WebviewService {
}

setBounds(bounds: Electron.Rectangle): void {
if (isWin) {
bounds.y = bounds.y + 30;
} else {
bounds.y = bounds.y - 0;
}
// if (isWin) {
// bounds.y = bounds.y + 30;
// } else {
// bounds.y = bounds.y - 0;
// }
this.view.setBounds(bounds);
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env zx

import { echo,$ } from "zx";
import { echo, $ } from "zx";

echo("开始构建 production ...");
echo("当前所在的目录是:", process.cwd());
Expand All @@ -11,7 +11,7 @@ await $`npm run build:server`;

await $`npm run build:plugin`;

await $`npm run build:mobile`;
await $`npm run build:player`;

await $`npm run build:main`;

Expand Down
2 changes: 1 addition & 1 deletion scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (!fs.existsSync(filename)) {
}

if (!fs.existsSync(path.resolve(app, "mobile"))) {
await $`npm run build:mobile`;
await $`npm run build:player`;
}

if (!fs.existsSync(path.resolve(app, "plugin"))) {
Expand Down

0 comments on commit 3414e39

Please sign in to comment.