Skip to content

Commit 11a9d8a

Browse files
committed
feat(env): add YTDL_COOKIE_PATH for YouTube cookie support in content retrieval
1 parent e2371a9 commit 11a9d8a

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,5 @@ logs
7676
docs
7777
data
7878
sqlite.db*
79+
80+
ytdl.json

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ docker run -p 3000:3000 qlaffont-livechatccb \
6262
-e DEFAULT_DURATION='5' \ # <-- Durée par défaut si le contenu n'est pas vidéo ou audio
6363
-e HIDE_COMMANDS_DISABLED='false' \ # <-- Si vous souhaitez désactiver les commandes masquées, vous pouvez modifier la valeur de 'false' à 'true'
6464
-e API_URL='API-URL-TO-REPLACE' # <-- Remplacer par l'adresse où l'utilisateur se connectera (Ex: https://livechat.domainname.com)
65+
-e YTDL_COOKIE_PATH='YTDL-COOKIE-PATH-TO-REPLACE' # <-- Remplacer par le chemin du fichier cookies.json (voir https://github.com/distubejs/ytdl-core#cookies-support)
6566
```
6667

6768
OU
@@ -147,6 +148,7 @@ docker run -p 3000:3000 qlaffont-livechatccb \
147148
-e DEFAULT_DURATION='5' \ # <-- Default duration if content is not video or audio
148149
-e HIDE_COMMANDS_DISABLED='false' \ # <-- If you want to disable hided commands, you can change the value from 'false' to 'true'
149150
-e API_URL='API-URL-TO-REPLACE' \ # <--Replace with the endpoint where user will connect (Ex: https://livechat.domainname.com)
151+
-e YTDL_COOKIE_PATH='YTDL-COOKIE-PATH-TO-REPLACE' # <-- Replace with the path of the cookies.json file (see https://github.com/distubejs/ytdl-core#cookies-support)
150152
-e I18N='en'
151153
```
152154

src/services/content-utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'fs';
12
import fetch from 'node-fetch';
23
import { getVideoDurationInSeconds } from 'get-video-duration';
34
import { fileTypeFromBuffer } from 'file-type';
@@ -49,7 +50,13 @@ export const getContentInformationsFromUrl = async (url: string) => {
4950

5051
//if it is a youtube video, get the duration from the url
5152
if (url.includes('youtube.com') || url.includes('youtu.be')) {
52-
const info = await ytdl.getInfo(url);
53+
let agent;
54+
55+
//if file exist on root of app, use it
56+
if (fs.existsSync(env.YTDL_COOKIE_PATH)) {
57+
agent = ytdl.createAgent(JSON.parse(fs.readFileSync(env.YTDL_COOKIE_PATH, 'utf8')));
58+
}
59+
const info = await ytdl.getInfo(url, { agent });
5360
mediaDuration = info.videoDetails.lengthSeconds;
5461
mediaIsShort = info.videoDetails.isCrawlable;
5562
contentType = 'video/mp4';

src/services/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export const env = createEnv({
2727
.string()
2828
.default('5')
2929
.transform((s) => parseInt(s)),
30+
31+
YTDL_COOKIE_PATH: z.string(),
3032
},
3133
runtimeEnv: process.env,
3234
});

0 commit comments

Comments
 (0)