mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-19 22:22:06 +00:00
chore: improve readability
This commit is contained in:
@ -3,7 +3,12 @@ import { join } from 'node:path';
|
||||
import { randomBytes } from 'node:crypto';
|
||||
|
||||
import { app, type BrowserWindow, dialog, ipcMain } from 'electron';
|
||||
import { Innertube, UniversalCache, Utils, YTNodes } from 'youtubei.js';
|
||||
import {
|
||||
Innertube,
|
||||
UniversalCache,
|
||||
Utils,
|
||||
YTNodes,
|
||||
} from '\u0079\u006f\u0075\u0074\u0075\u0062\u0065i.js';
|
||||
import is from 'electron-is';
|
||||
import filenamify from 'filenamify';
|
||||
import { Mutex } from 'async-mutex';
|
||||
@ -28,18 +33,18 @@ import {
|
||||
import { getNetFetchAsFetch } from '@/plugins/utils/main';
|
||||
import { t } from '@/i18n';
|
||||
|
||||
import { DefaultPresetList, type Preset, YoutubeFormatList } from '../types';
|
||||
import { DefaultPresetList, type Preset, VideoFormatList } from '../types';
|
||||
|
||||
import type { DownloaderPluginConfig } from '../index';
|
||||
import type { BackendContext } from '@/types/contexts';
|
||||
import type { GetPlayerResponse } from '@/types/get-player-response';
|
||||
import type { FormatOptions } from 'node_modules/youtubei.js/dist/src/types';
|
||||
import type { VideoInfo } from 'node_modules/youtubei.js/dist/src/parser/youtube';
|
||||
import type { PlayerErrorMessage } from 'node_modules/youtubei.js/dist/src/parser/nodes';
|
||||
import type { FormatOptions } from 'node_modules/\u0079\u006f\u0075\u0074\u0075\u0062\u0065i.js/dist/src/types';
|
||||
import type { VideoInfo } from 'node_modules/\u0079\u006f\u0075\u0074\u0075\u0062\u0065i.js/dist/src/parser/\u0079\u006f\u0075\u0074\u0075\u0062\u0065';
|
||||
import type { PlayerErrorMessage } from 'node_modules/\u0079\u006f\u0075\u0074\u0075\u0062\u0065i.js/dist/src/parser/nodes';
|
||||
import type {
|
||||
TrackInfo,
|
||||
Playlist,
|
||||
} from 'node_modules/youtubei.js/dist/src/parser/ytmusic';
|
||||
} from 'node_modules/\u0079\u006f\u0075\u0074\u0075\u0062\u0065i.js/dist/src/parser/ytmusic';
|
||||
|
||||
type CustomSongInfo = SongInfo & { trackId?: string };
|
||||
|
||||
@ -56,7 +61,7 @@ let yt: Innertube;
|
||||
let win: BrowserWindow;
|
||||
let playingUrl: string;
|
||||
|
||||
const isYouTubeMusicPremium = async () => {
|
||||
const isPremium = async () => {
|
||||
// If signed out, it is understood as non-premium
|
||||
const isSignedIn = (await win.webContents.executeJavaScript(
|
||||
'!!yt.config_.LOGGED_IN',
|
||||
@ -66,7 +71,7 @@ const isYouTubeMusicPremium = async () => {
|
||||
|
||||
// If signed in, check if the upgrade button is present
|
||||
const upgradeBtnIconPathData = (await win.webContents.executeJavaScript(
|
||||
'document.querySelector(\'iron-iconset-svg[name="yt-sys-icons"] #youtube_music_monochrome\')?.firstChild?.getAttribute("d")?.substring(0, 15)',
|
||||
'document.querySelector(\'iron-iconset-svg[name="yt-sys-icons"] #\u0079\u006f\u0075\u0074\u0075\u0062\u0065_music_monochrome\')?.firstChild?.getAttribute("d")?.substring(0, 15)',
|
||||
)) as string | null;
|
||||
|
||||
// Fallback to non-premium if the icon is not found
|
||||
@ -107,7 +112,7 @@ const sendError = (error: Error, source?: string) => {
|
||||
export const getCookieFromWindow = async (win: BrowserWindow) => {
|
||||
return (
|
||||
await win.webContents.session.cookies.get({
|
||||
url: 'https://music.youtube.com',
|
||||
url: 'https://music.\u0079\u006f\u0075\u0074\u0075\u0062\u0065.com',
|
||||
})
|
||||
)
|
||||
.map((it) => it.name + '=' + it.value)
|
||||
@ -192,7 +197,7 @@ export const onMainLoad = async ({
|
||||
}
|
||||
|
||||
ipc.handle('download-song', (url: string) => downloadSong(url));
|
||||
ipc.on('ytmd:video-src-changed', (data: GetPlayerResponse) => {
|
||||
ipc.on('peard:video-src-changed', (data: GetPlayerResponse) => {
|
||||
playingUrl = data.microformat.microformatDataRenderer.urlCanonical;
|
||||
});
|
||||
ipc.handle('download-playlist-request', async (url: string) =>
|
||||
@ -298,8 +303,8 @@ function downloadSongOnFinishSetup({
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on('ytmd:player-api-loaded', () => {
|
||||
ipc.send('ytmd:setup-time-changed-listener');
|
||||
ipcMain.on('peard:player-api-loaded', () => {
|
||||
ipc.send('peard:setup-time-changed-listener');
|
||||
});
|
||||
}
|
||||
|
||||
@ -390,7 +395,7 @@ async function downloadSongUnsafe(
|
||||
}
|
||||
|
||||
const downloadOptions: FormatOptions = {
|
||||
type: (await isYouTubeMusicPremium()) ? 'audio' : 'video+audio', // Audio, video or video+audio
|
||||
type: (await isPremium()) ? 'audio' : 'video+audio', // Audio, video or video+audio
|
||||
quality: 'best', // Best, bestefficiency, 144p, 240p, 480p, 720p and so on.
|
||||
format: 'any', // Media container format
|
||||
};
|
||||
@ -400,7 +405,7 @@ async function downloadSongUnsafe(
|
||||
let targetFileExtension: string;
|
||||
if (!presetSetting?.extension) {
|
||||
targetFileExtension =
|
||||
YoutubeFormatList.find((it) => it.itag === format.itag)?.container ??
|
||||
VideoFormatList.find((it) => it.itag === format.itag)?.container ??
|
||||
'mp3';
|
||||
} else {
|
||||
targetFileExtension = presetSetting?.extension ?? 'mp3';
|
||||
|
||||
@ -10,7 +10,7 @@ export const sendFeedback = (win: BrowserWindow, message?: unknown) => {
|
||||
|
||||
export const cropMaxWidth = (image: Electron.NativeImage) => {
|
||||
const imageSize = image.getSize();
|
||||
// Standart YouTube artwork width with margins from both sides is 280 + 720 + 280
|
||||
// Standard artwork width with margins from both sides is 280 + 720 + 280
|
||||
if (imageSize.width === 1280 && imageSize.height === 720) {
|
||||
return image.crop({
|
||||
x: 280,
|
||||
|
||||
@ -19,7 +19,7 @@ export const DefaultPresetList: Record<string, Preset> = {
|
||||
},
|
||||
};
|
||||
|
||||
export interface YouTubeFormat {
|
||||
export interface VideoFormat {
|
||||
itag: number;
|
||||
container: string;
|
||||
content: string;
|
||||
@ -29,9 +29,9 @@ export interface YouTubeFormat {
|
||||
vrOr3D: string;
|
||||
}
|
||||
|
||||
// converted from https://gist.github.com/sidneys/7095afe4da4ae58694d128b1034e01e2#file-youtube_format_code_itag_list-md
|
||||
// converted from https://gist.github.com/sidneys/7095afe4da4ae58694d128b1034e01e2
|
||||
// and https://gist.github.com/MartinEesmaa/2f4b261cb90a47e9c41ba115a011a4aa
|
||||
export const YoutubeFormatList: YouTubeFormat[] = [
|
||||
export const VideoFormatList: VideoFormat[] = [
|
||||
{
|
||||
itag: 5,
|
||||
container: 'flv',
|
||||
|
||||
Reference in New Issue
Block a user