diff --git a/apps/singularity-api/src/app/song/yt.service.ts b/apps/singularity-api/src/app/song/yt.service.ts index c0974db..b8c45c5 100644 --- a/apps/singularity-api/src/app/song/yt.service.ts +++ b/apps/singularity-api/src/app/song/yt.service.ts @@ -2,12 +2,26 @@ import { Injectable } from '@nestjs/common'; import { VideoInfo } from '@singularity/api-interfaces'; import * as ytdl from '@distube/ytdl-core'; import { SongFile } from './interfaces/song-file'; +import { ConfigService } from '@nestjs/config'; +import { Agent } from '@distube/ytdl-core'; @Injectable() export class YtService { + private readonly agent: Agent; + + constructor(configService: ConfigService) { + const cookie = configService.get('YOUTUBE_COOKIE'); + + if(cookie) { + this.agent = ytdl.createAgent(JSON.parse(cookie)); + } else { + this.agent = ytdl.createAgent(); + } + } + public async getInfo(id: string): Promise { - const ytdlVideoInfo = await ytdl.getBasicInfo(id); + const ytdlVideoInfo = await ytdl.getBasicInfo(id, { agent: this.agent }); const videoInfo = new VideoInfo(); videoInfo.id = id; @@ -31,7 +45,7 @@ export class YtService { } public async downloadVideo(id: string): Promise { - const info = await ytdl.getInfo(id); + const info = await ytdl.getInfo(id, { agent: this.agent }); const format = ytdl.chooseFormat(info.formats, { quality: 'highestvideo', filter: 'videoonly' }); const buffer = await this.download(info, format); @@ -40,7 +54,7 @@ export class YtService { } public async downloadAudo(id: string): Promise { - const info = await ytdl.getInfo(id); + const info = await ytdl.getInfo(id, { agent: this.agent }); const format = ytdl.chooseFormat(info.formats, { quality: 'highestaudio', filter: 'audioonly' }); const buffer = await this.download(info, format); @@ -50,7 +64,7 @@ export class YtService { private async download(info: ytdl.videoInfo, format: ytdl.videoFormat): Promise { return new Promise((resolve, reject) => { - const stream = ytdl(info.videoDetails.video_url, { format: format }) + const stream = ytdl(info.videoDetails.video_url, { format: format, agent: this.agent }) const chunks: Buffer[] = []; stream.on('data', (chunk: Buffer) => chunks.push(chunk)); stream.on('end', () => resolve(Buffer.concat(chunks))); diff --git a/package-lock.json b/package-lock.json index f65662a..c57e15c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ "@automapper/classes": "^8.7.7", "@automapper/core": "^8.7.7", "@automapper/nestjs": "^8.7.7", - "@distube/ytdl-core": "^4.15.1", + "@distube/ytdl-core": "^4.15.3", "@fastify/static": "^6.10.1", "@nestjs/axios": "^3.0.2", "@nestjs/common": "10.0.2", @@ -3176,11 +3176,12 @@ } }, "node_modules/@distube/ytdl-core": { - "version": "4.15.1", - "resolved": "https://registry.npmjs.org/@distube/ytdl-core/-/ytdl-core-4.15.1.tgz", - "integrity": "sha512-10voV+ixUVQgam4/GMhjNTX/ipk9odkPnPoRaUESs5E/EW0JujwPDc815z53D86mSRlVc8esH1eiuzFnxLJKcA==", + "version": "4.15.3", + "resolved": "https://registry.npmjs.org/@distube/ytdl-core/-/ytdl-core-4.15.3.tgz", + "integrity": "sha512-n+dgGw3k3GzFFpIkGl9w19rHGOZownr46oF3j+zx4axiNUv5ydW244nYfNp0a+8cwbADT1StPQdYY7BUq42TrA==", "dependencies": { - "http-cookie-agent": "^6.0.6", + "http-cookie-agent": "^6.0.8", + "https-proxy-agent": "^7.0.6", "m3u8stream": "^0.8.6", "miniget": "^4.2.3", "sax": "^1.4.1", @@ -3194,6 +3195,18 @@ "url": "https://github.com/distubejs/ytdl-core?sponsor" } }, + "node_modules/@distube/ytdl-core/node_modules/https-proxy-agent": { + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz", + "integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==", + "dependencies": { + "agent-base": "^7.1.2", + "debug": "4" + }, + "engines": { + "node": ">= 14" + } + }, "node_modules/@emnapi/runtime": { "version": "0.45.0", "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-0.45.0.tgz", diff --git a/package.json b/package.json index 54f59b9..8ff00d3 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@automapper/classes": "^8.7.7", "@automapper/core": "^8.7.7", "@automapper/nestjs": "^8.7.7", - "@distube/ytdl-core": "^4.15.1", + "@distube/ytdl-core": "^4.15.3", "@fastify/static": "^6.10.1", "@nestjs/axios": "^3.0.2", "@nestjs/common": "10.0.2",