Compare commits
2 Commits
88aa8eb34a
...
4e5631487f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e5631487f | ||
|
|
7cc225ca47 |
@ -64,7 +64,8 @@ RUN apk add --no-cache \
|
|||||||
pixman \
|
pixman \
|
||||||
pangomm \
|
pangomm \
|
||||||
libjpeg-turbo \
|
libjpeg-turbo \
|
||||||
freetype
|
freetype \
|
||||||
|
yt-dlp
|
||||||
|
|
||||||
RUN addgroup --system --gid 1001 nodejs
|
RUN addgroup --system --gid 1001 nodejs
|
||||||
RUN adduser --system --uid 1001 botuser
|
RUN adduser --system --uid 1001 botuser
|
||||||
|
|||||||
@ -10,9 +10,9 @@ description =
|
|||||||
⚡ Simply send the video link, and the bot will download it for you.
|
⚡ Simply send the video link, and the bot will download it for you.
|
||||||
|
|
||||||
short-description =
|
short-description =
|
||||||
Download TikTok, Instagram and YouTube videos and images.
|
Bot downloads videos and pictures
|
||||||
|
Support: @v_dev_support
|
||||||
For any questions: @v_dev_support
|
Donate: { $donateLink }
|
||||||
|
|
||||||
|
|
||||||
start =
|
start =
|
||||||
|
|||||||
@ -10,9 +10,9 @@ description =
|
|||||||
⚡ Просто отправьте ссылку на видео и изображения, и бот сразу скачает его для вас!
|
⚡ Просто отправьте ссылку на видео и изображения, и бот сразу скачает его для вас!
|
||||||
|
|
||||||
short-description =
|
short-description =
|
||||||
Скачивай видео и изображения из TikTok, Instagram и YouTube.
|
Бот скачивает видео и изображения
|
||||||
|
Поддержка: @v_dev_support
|
||||||
По всем вопросам: @v_dev_support
|
Поблагодарить: { $donateLink }
|
||||||
|
|
||||||
|
|
||||||
start =
|
start =
|
||||||
|
|||||||
@ -1,10 +1,13 @@
|
|||||||
import { type Context } from '../context';
|
import { type Context } from '../context';
|
||||||
import { i18n } from '../i18n';
|
import { i18n } from '../i18n';
|
||||||
|
import { env } from '@/config/env';
|
||||||
import { type Api, type Bot, type RawApi } from 'grammy';
|
import { type Api, type Bot, type RawApi } from 'grammy';
|
||||||
|
|
||||||
export async function setInfo({ api }: Bot<Context, Api<RawApi>>) {
|
export async function setInfo({ api }: Bot<Context, Api<RawApi>>) {
|
||||||
for (const locale of i18n.locales) {
|
for (const locale of i18n.locales) {
|
||||||
await api.setMyDescription(i18n.t(locale, 'description'));
|
await api.setMyDescription(i18n.t(locale, 'description'));
|
||||||
await api.setMyShortDescription(i18n.t(locale, 'short-description'));
|
await api.setMyShortDescription(
|
||||||
|
i18n.t(locale, 'short-description', { donateLink: env.DONATE_LINK }),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { z } from 'zod';
|
|||||||
|
|
||||||
export const envSchema = z.object({
|
export const envSchema = z.object({
|
||||||
BOT_TOKEN: z.string(),
|
BOT_TOKEN: z.string(),
|
||||||
|
DONATE_LINK: z.string(),
|
||||||
RATE_LIMIT: z
|
RATE_LIMIT: z
|
||||||
.string()
|
.string()
|
||||||
.transform((value) => Number.parseInt(value, 10))
|
.transform((value) => Number.parseInt(value, 10))
|
||||||
@ -18,6 +19,7 @@ export const envSchema = z.object({
|
|||||||
.transform((value) => Number.parseInt(value, 10))
|
.transform((value) => Number.parseInt(value, 10))
|
||||||
.default('6379'),
|
.default('6379'),
|
||||||
TELEGRAM_API_ROOT: z.string(),
|
TELEGRAM_API_ROOT: z.string(),
|
||||||
|
YTDLP_PATH: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const env = envSchema.parse(process.env);
|
export const env = envSchema.parse(process.env);
|
||||||
|
|||||||
@ -1,69 +0,0 @@
|
|||||||
import { getClient } from './client';
|
|
||||||
import { MAX_VIDEO_DURATION_SECONDS } from '@/constants/limits';
|
|
||||||
|
|
||||||
export type DownloadRoot = {
|
|
||||||
duration: number;
|
|
||||||
filename: string;
|
|
||||||
status: string;
|
|
||||||
url: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type InfoRoot = {
|
|
||||||
duration: number;
|
|
||||||
medias: Media[];
|
|
||||||
thumbnail: string;
|
|
||||||
title: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type Media = {
|
|
||||||
extension: string;
|
|
||||||
fileSize: number;
|
|
||||||
quality: string;
|
|
||||||
type: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const qualityOrder = ['144p', '240p', '360p', '480p', '1080p', '720p'].reverse();
|
|
||||||
|
|
||||||
export async function getYoutubeDownloadUrl(url: string) {
|
|
||||||
const client = await getClient();
|
|
||||||
// fetch info
|
|
||||||
const { data: infoData } = await client.post<InfoRoot>(
|
|
||||||
'https://downr.org/.netlify/functions/video-info',
|
|
||||||
{
|
|
||||||
url,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!infoData?.medias.length) throw new Error('err-invalid-youtube-response');
|
|
||||||
if (infoData.duration > MAX_VIDEO_DURATION_SECONDS)
|
|
||||||
throw new Error('err-youtube-duration-exceeded');
|
|
||||||
|
|
||||||
let quality: string | undefined;
|
|
||||||
|
|
||||||
for (const q of qualityOrder) {
|
|
||||||
const hasQuality = infoData.medias.find(
|
|
||||||
(media) => media.type === 'video' && media.quality === q && media.extension === 'mp4',
|
|
||||||
);
|
|
||||||
|
|
||||||
if (hasQuality) {
|
|
||||||
quality = q;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!quality) throw new Error('err-youtube-no-quality');
|
|
||||||
|
|
||||||
// fetch download link
|
|
||||||
const { data: downloadData } = await client.post<DownloadRoot>(
|
|
||||||
'https://downr.org/.netlify/functions/youtube-download',
|
|
||||||
{
|
|
||||||
downloadMode: 'video',
|
|
||||||
url,
|
|
||||||
videoQuality: quality,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
play: downloadData.url,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
40
apps/bot/src/utils/youtube/api.ts
Normal file
40
apps/bot/src/utils/youtube/api.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
type DlsrvResponse = {
|
||||||
|
duration: number;
|
||||||
|
filename: string;
|
||||||
|
status: string;
|
||||||
|
url: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function getYoutubeDownload(url: string) {
|
||||||
|
const videoId = getYouTubeVideoId(url);
|
||||||
|
|
||||||
|
const { data } = await axios.post<DlsrvResponse>('https://embed.dlsrv.online/api/download/mp4', {
|
||||||
|
format: 'mp4',
|
||||||
|
quality: '720',
|
||||||
|
videoId,
|
||||||
|
});
|
||||||
|
|
||||||
|
return data.url;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getYouTubeVideoId(link: string) {
|
||||||
|
const url = new URL(link);
|
||||||
|
|
||||||
|
// 1. shorts
|
||||||
|
if (url.pathname.startsWith('/shorts/')) {
|
||||||
|
return url.pathname.split('/')[2] || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. обычное видео: watch?v=
|
||||||
|
const vParam = url.searchParams.get('v');
|
||||||
|
if (vParam) return vParam;
|
||||||
|
|
||||||
|
// 3. короткая ссылка youtu.be
|
||||||
|
if (url.hostname.includes('youtu.be')) {
|
||||||
|
return url.pathname.slice(1) || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
21
apps/bot/src/utils/youtube/index.ts
Normal file
21
apps/bot/src/utils/youtube/index.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { getInfo, ytDlpGetUrl } from '../yt-dlp';
|
||||||
|
import { getYoutubeDownload } from './api';
|
||||||
|
import { MAX_VIDEO_DURATION_SECONDS } from '@/constants/limits';
|
||||||
|
|
||||||
|
export async function getYoutubeDownloadUrl(url: string) {
|
||||||
|
const infoData = await getInfo(url);
|
||||||
|
|
||||||
|
if (!infoData) throw new Error('err-invalid-youtube-response');
|
||||||
|
|
||||||
|
if (infoData.duration > MAX_VIDEO_DURATION_SECONDS)
|
||||||
|
throw new Error('err-youtube-duration-exceeded');
|
||||||
|
|
||||||
|
let play: string | undefined;
|
||||||
|
try {
|
||||||
|
play = await getYoutubeDownload(url);
|
||||||
|
} catch {
|
||||||
|
play = await ytDlpGetUrl(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { play, title: infoData.title };
|
||||||
|
}
|
||||||
22
apps/bot/src/utils/yt-dlp/get-download-url.ts
Normal file
22
apps/bot/src/utils/yt-dlp/get-download-url.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { runYtDlp } from './yt-dlp';
|
||||||
|
|
||||||
|
export async function ytDlpGetUrl(url: string): Promise<string> {
|
||||||
|
const output = await runYtDlp([
|
||||||
|
'-f',
|
||||||
|
// '298+ba/136+ba/22+ba/247+251/best[height<=720]+ba/bv+ba',
|
||||||
|
'best[height<=720]+ba/bv+ba',
|
||||||
|
'-g',
|
||||||
|
'--no-playlist',
|
||||||
|
'--no-warnings',
|
||||||
|
url,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// yt-dlp может вернуть несколько строк — берём первую
|
||||||
|
const directUrl = output.split('\n')[0];
|
||||||
|
|
||||||
|
if (!directUrl?.startsWith('http')) {
|
||||||
|
throw new Error('Failed to get direct video URL');
|
||||||
|
}
|
||||||
|
|
||||||
|
return directUrl;
|
||||||
|
}
|
||||||
1710
apps/bot/src/utils/yt-dlp/get-info.ts
Normal file
1710
apps/bot/src/utils/yt-dlp/get-info.ts
Normal file
File diff suppressed because it is too large
Load Diff
2
apps/bot/src/utils/yt-dlp/index.ts
Normal file
2
apps/bot/src/utils/yt-dlp/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export { ytDlpGetUrl } from './get-download-url';
|
||||||
|
export { getInfo } from './get-info';
|
||||||
32
apps/bot/src/utils/yt-dlp/yt-dlp.ts
Normal file
32
apps/bot/src/utils/yt-dlp/yt-dlp.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { env } from '@/config/env';
|
||||||
|
import { spawn } from 'node:child_process';
|
||||||
|
|
||||||
|
export function runYtDlp(args: string[]): Promise<string> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const ytDlpPath = getYtDlpPath();
|
||||||
|
const process = spawn(ytDlpPath, args);
|
||||||
|
|
||||||
|
let output = '';
|
||||||
|
let error = '';
|
||||||
|
|
||||||
|
process.stdout.on('data', (data) => {
|
||||||
|
output += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
process.stderr.on('data', (data) => {
|
||||||
|
error += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on('close', (code) => {
|
||||||
|
if (code === 0) {
|
||||||
|
resolve(output.trim());
|
||||||
|
} else {
|
||||||
|
reject(new Error(`yt-dlp error: ${error}`));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getYtDlpPath(): string {
|
||||||
|
return env.YTDLP_PATH || 'yt-dlp';
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user