fix: fix the downloader to work in a proxy environment (resolve #46)

This commit is contained in:
JellyBrick
2023-10-06 23:22:07 +09:00
parent 59efba4dec
commit 1806d5a0a2

View File

@ -2,7 +2,7 @@ import { createWriteStream, existsSync, mkdirSync, writeFileSync, } from 'node:f
import { join } from 'node:path';
import { randomBytes } from 'node:crypto';
import { app, BrowserWindow, dialog, ipcMain } from 'electron';
import { app, BrowserWindow, dialog, ipcMain, net } from 'electron';
import { ClientType, Innertube, UniversalCache, Utils } from 'youtubei.js';
import is from 'electron-is';
import ytpl from 'ytpl';
@ -80,6 +80,24 @@ export default async (win_: BrowserWindow) => {
cache: new UniversalCache(false),
cookie,
generate_session_locally: true,
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
const url =
typeof input === 'string' ?
new URL(input) :
input instanceof URL ?
input : new URL(input.url);
if (init?.body && !init.method) {
init.method = 'POST';
}
const request = new Request(
url,
input instanceof Request ? input : undefined,
);
return net.fetch(request, init);
}
});
ipcMain.on('download-song', (_, url: string) => downloadSong(url));
ipcMain.on('video-src-changed', (_, data: GetPlayerResponse) => {
@ -107,7 +125,6 @@ export async function downloadSong(
increasePlaylistProgress,
);
} catch (error: unknown) {
console.log('maybe?????', error);
sendError(error as Error, resolvedName || url);
}
}
@ -117,8 +134,7 @@ async function downloadSongUnsafe(
setName: (name: string) => void,
playlistFolder: string | undefined = undefined,
trackId: string | undefined = undefined,
increasePlaylistProgress: (value: number) => void = () => {
},
increasePlaylistProgress: (value: number) => void = () => {},
) {
const sendFeedback = (message: unknown, progress?: number) => {
if (!playlistFolder) {
@ -320,7 +336,6 @@ async function iterableStreamToMP3(
ffmpeg.FS('unlink', `${safeVideoName}.mp3`);
}
} catch (error: unknown) {
console.log('maybe?', error);
sendError(error as Error, safeVideoName);
} finally {
releaseFFmpegMutex();
@ -373,7 +388,6 @@ async function writeID3(buffer: Buffer, metadata: CustomSongInfo, sendFeedback:
return NodeID3.write(tags, buffer);
} catch (error: unknown) {
console.log('fallback', error);
sendError(error as Error, `${metadata.artist} - ${metadata.title}`);
return null;
}
@ -488,7 +502,6 @@ export async function downloadPlaylist(givenUrl?: string | URL) {
counter++;
}
} catch (error: unknown) {
console.log('also?', error);
sendError(error as Error);
} finally {
win.setProgressBar(-1); // Close progress bar
@ -514,7 +527,6 @@ async function ffmpegWriteTags(filePath: string, metadata: CustomSongInfo, prese
filePath,
);
} catch (error: unknown) {
console.log('ffmpeg?', error);
sendError(error as Error);
} finally {
releaseFFmpegMutex();
@ -548,11 +560,7 @@ const getPlaylistID = (aURL: URL) => {
};
const getVideoId = (url: URL | string): string | null => {
if (typeof url === 'string') {
url = new URL(url);
}
return url.searchParams.get('v');
return (new URL(url)).searchParams.get('v');
};
const getMetadata = (info: TrackInfo): CustomSongInfo => ({