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