This reverts commit 46712450b26cb834c4157fff1bf0667a0b829aa4.
This commit is contained in:
parent
46712450b2
commit
4d5e10d37c
@ -40,7 +40,6 @@
|
||||
"tough-cookie": "^6.0.0",
|
||||
"tsup": "^8.5.0",
|
||||
"typescript": "catalog:",
|
||||
"ytdlp-nodejs": "^2.3.5",
|
||||
"zod": "catalog:"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@ -9,8 +9,6 @@ import { validateTikTokUrl, validateInstagramUrl, validateYoutubeUrl } from '@/u
|
||||
import { Composer, InputFile } from 'grammy';
|
||||
import { cluster } from 'radashi';
|
||||
import { getYoutubeDownloadUrl } from '@/utils/youtube';
|
||||
import { getDownloadUrl } from '@/utils/yt-dlp';
|
||||
import { logger } from '@/utils/logger';
|
||||
|
||||
const composer = new Composer<Context>();
|
||||
const feature = composer.chatType('private');
|
||||
@ -24,9 +22,7 @@ feature.on('message:text', logHandle('download-message'), async (context) => {
|
||||
const isInstagram = validateInstagramUrl(url);
|
||||
const isYoutube = validateYoutubeUrl(url);
|
||||
|
||||
const isServiceSupported = isTikTok || isInstagram || isYoutube;
|
||||
|
||||
if (!isServiceSupported) {
|
||||
if (!isTikTok && !isInstagram && !isYoutube) {
|
||||
return context.reply(context.t('err-invalid-url'));
|
||||
}
|
||||
|
||||
@ -36,31 +32,28 @@ feature.on('message:text', logHandle('download-message'), async (context) => {
|
||||
}
|
||||
|
||||
let imagesUrls: string[] | undefined;
|
||||
let videoUrl = await getDownloadUrl(url);
|
||||
let videoUrl: string | undefined;
|
||||
|
||||
if (!videoUrl) {
|
||||
logger.info(`Failed to get download URL for ${url}, using fallback`);
|
||||
try {
|
||||
if (isTikTok) {
|
||||
const result = await getTiktokDownloadUrl(url);
|
||||
imagesUrls = result.images;
|
||||
videoUrl = result.play;
|
||||
} else if (isInstagram) {
|
||||
const result = await getInstagramDownloadUrl(url);
|
||||
imagesUrls = result.images;
|
||||
videoUrl = result.play;
|
||||
} else if (isYoutube) {
|
||||
const result = await getYoutubeDownloadUrl(url);
|
||||
videoUrl = result.play;
|
||||
}
|
||||
} catch (err: any) {
|
||||
const message = err?.message ?? String(err);
|
||||
if (typeof message === 'string' && message.startsWith('err-')) {
|
||||
return context.reply(context.t(message));
|
||||
}
|
||||
|
||||
return context.reply(context.t('err-generic'));
|
||||
try {
|
||||
if (isTikTok) {
|
||||
const result = await getTiktokDownloadUrl(url);
|
||||
imagesUrls = result.images;
|
||||
videoUrl = result.play;
|
||||
} else if (isInstagram) {
|
||||
const result = await getInstagramDownloadUrl(url);
|
||||
imagesUrls = result.images;
|
||||
videoUrl = result.play;
|
||||
} else if (isYoutube) {
|
||||
const result = await getYoutubeDownloadUrl(url);
|
||||
videoUrl = result.play;
|
||||
}
|
||||
} catch (err: any) {
|
||||
const message = err?.message ?? String(err);
|
||||
if (typeof message === 'string' && message.startsWith('err-')) {
|
||||
return context.reply(context.t(message));
|
||||
}
|
||||
|
||||
return context.reply(context.t('err-generic'));
|
||||
}
|
||||
|
||||
if (!videoUrl && !imagesUrls?.length) {
|
||||
@ -74,7 +67,11 @@ feature.on('message:text', logHandle('download-message'), async (context) => {
|
||||
chunk.map((imageUrl) => ({ media: imageUrl, type: 'photo' })),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (videoUrl) {
|
||||
const { video } = await context.replyWithVideo(new InputFile({ url: videoUrl }));
|
||||
await redis.set(url, video.file_id, 'EX', TTL_URLS);
|
||||
}
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
import { logger } from './logger';
|
||||
import { YtDlp } from 'ytdlp-nodejs';
|
||||
|
||||
export const getDownloadUrl = async (url: string): Promise<string | undefined> => {
|
||||
try {
|
||||
const ytdlp = new YtDlp();
|
||||
const [downloadUrl] = await ytdlp.getUrlsAsync(url, { format: 'b' });
|
||||
return downloadUrl;
|
||||
} catch (error: any) {
|
||||
if (error.code === 'ENOENT') {
|
||||
throw new Error('yt-dlp not found. Please install yt-dlp.');
|
||||
}
|
||||
|
||||
logger.error(`Failed to get download URL for ${url}: ${error.message}`);
|
||||
}
|
||||
};
|
||||
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
@ -117,9 +117,6 @@ importers:
|
||||
typescript:
|
||||
specifier: 'catalog:'
|
||||
version: 5.9.2
|
||||
ytdlp-nodejs:
|
||||
specifier: ^2.3.5
|
||||
version: 2.3.5
|
||||
zod:
|
||||
specifier: 'catalog:'
|
||||
version: 3.25.76
|
||||
@ -3685,11 +3682,6 @@ packages:
|
||||
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
|
||||
engines: {node: '>=10'}
|
||||
|
||||
ytdlp-nodejs@2.3.5:
|
||||
resolution: {integrity: sha512-7V08DRv8C1K0HxJFvRoaoLYFS/reJ9VJBlaMVhEvdi2IsYK/9Hae1Mah65Y+bhk3RAmx7G9eTfpOhkj3bp0Zbw==}
|
||||
engines: {node: '>=16.0.0'}
|
||||
hasBin: true
|
||||
|
||||
zod@3.25.76:
|
||||
resolution: {integrity: sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==}
|
||||
|
||||
@ -7604,6 +7596,4 @@ snapshots:
|
||||
|
||||
yocto-queue@0.1.0: {}
|
||||
|
||||
ytdlp-nodejs@2.3.5: {}
|
||||
|
||||
zod@3.25.76: {}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user