Compare commits
3 Commits
ce78952fec
...
8a8a21b155
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8a8a21b155 | ||
|
|
1f5b4073e0 | ||
|
|
8919fbb65f |
@ -4,17 +4,157 @@ import { logHandle } from '../helpers/logging';
|
|||||||
import { TTL_URLS } from '@/config/redis';
|
import { TTL_URLS } from '@/config/redis';
|
||||||
import { getInstagramDownloadUrl } from '@/utils/instagram';
|
import { getInstagramDownloadUrl } from '@/utils/instagram';
|
||||||
import { getRedisInstance } from '@/utils/redis';
|
import { getRedisInstance } from '@/utils/redis';
|
||||||
|
import { removeHashtags } from '@/utils/text';
|
||||||
import { getTiktokDownloadUrl } from '@/utils/tiktok';
|
import { getTiktokDownloadUrl } from '@/utils/tiktok';
|
||||||
import { validateInstagramUrl, validateTikTokUrl, validateYoutubeUrl } from '@/utils/urls';
|
import { validateInstagramUrl, validateTikTokUrl, validateYoutubeUrl } from '@/utils/urls';
|
||||||
import { getYoutubeDownloadUrl } from '@/utils/youtube';
|
import { getYoutubeDownloadUrl } from '@/utils/youtube';
|
||||||
|
import { code, expandableBlockquote, fmt } from '@grammyjs/parse-mode';
|
||||||
import { Composer, InputFile } from 'grammy';
|
import { Composer, InputFile } from 'grammy';
|
||||||
import { cluster } from 'radashi';
|
import { cluster } from 'radashi';
|
||||||
|
|
||||||
const composer = new Composer<Context>();
|
const composer = new Composer<Context>();
|
||||||
const feature = composer.chatType('private');
|
const feature = composer.chatType('private');
|
||||||
|
|
||||||
const redis = getRedisInstance();
|
const redis = getRedisInstance();
|
||||||
|
|
||||||
|
type DownloadResult = {
|
||||||
|
caption?: string;
|
||||||
|
imagesUrls?: string[];
|
||||||
|
videoUrl?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
async function checkCacheAndReply(context: Context, url: string) {
|
||||||
|
let contentMessageId: number | undefined;
|
||||||
|
|
||||||
|
const cachedVideoId = await redis.get(url);
|
||||||
|
if (cachedVideoId) {
|
||||||
|
const cachedMessage = await context.replyWithVideo(cachedVideoId);
|
||||||
|
contentMessageId = cachedMessage.message_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentMessageId) {
|
||||||
|
const cachedCaption = await redis.get(`caption:${url}`);
|
||||||
|
if (cachedCaption) {
|
||||||
|
const { entities, text } = formatCaption(cachedCaption);
|
||||||
|
|
||||||
|
if (text.trim().length) {
|
||||||
|
await context.reply(text, {
|
||||||
|
entities,
|
||||||
|
reply_parameters: { message_id: contentMessageId },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { captionSent: true, contentMessageId };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { contentMessageId };
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatCaption(caption: string) {
|
||||||
|
const cleanCaption = removeHashtags(caption);
|
||||||
|
return fmt`${expandableBlockquote} ${code} ${cleanCaption} ${code} ${expandableBlockquote}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getDownloadData(
|
||||||
|
url: string,
|
||||||
|
opts: {
|
||||||
|
isInstagram: boolean;
|
||||||
|
isTikTok: boolean;
|
||||||
|
isYoutube: boolean;
|
||||||
|
},
|
||||||
|
): Promise<DownloadResult> {
|
||||||
|
const { isInstagram, isTikTok, isYoutube } = opts;
|
||||||
|
|
||||||
|
if (isTikTok) {
|
||||||
|
const result = await getTiktokDownloadUrl(url);
|
||||||
|
return {
|
||||||
|
caption: result.title,
|
||||||
|
imagesUrls: result.images,
|
||||||
|
videoUrl: result.play,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isInstagram) {
|
||||||
|
const result = await getInstagramDownloadUrl(url);
|
||||||
|
return {
|
||||||
|
caption: result.caption,
|
||||||
|
imagesUrls: result.images,
|
||||||
|
videoUrl: result.play,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isYoutube) {
|
||||||
|
const result = await getYoutubeDownloadUrl(url);
|
||||||
|
return {
|
||||||
|
videoUrl: result.play,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendCaptionAndCache(
|
||||||
|
context: Context,
|
||||||
|
caption: string | undefined,
|
||||||
|
url: string,
|
||||||
|
contentMessageId?: number,
|
||||||
|
) {
|
||||||
|
if (!caption) return;
|
||||||
|
|
||||||
|
const { entities, text } = formatCaption(caption);
|
||||||
|
await redis.set(`caption:${url}`, caption, 'EX', TTL_URLS);
|
||||||
|
|
||||||
|
if (text.trim().length) {
|
||||||
|
await context.reply(text, {
|
||||||
|
entities,
|
||||||
|
reply_parameters: contentMessageId ? { message_id: contentMessageId } : undefined,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendImages(
|
||||||
|
context: Context,
|
||||||
|
imagesUrls: string[],
|
||||||
|
existingContentMessageId?: number,
|
||||||
|
) {
|
||||||
|
if (!imagesUrls.length) return existingContentMessageId;
|
||||||
|
|
||||||
|
const chunks = cluster(imagesUrls, 10);
|
||||||
|
let contentMessageId = existingContentMessageId;
|
||||||
|
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
const imageMessages = await context.replyWithMediaGroup(
|
||||||
|
chunk.map((imageUrl) => ({ media: imageUrl, type: 'photo' })),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!contentMessageId && imageMessages.length) {
|
||||||
|
contentMessageId = imageMessages.at(0)?.message_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return contentMessageId;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendVideoAndCache(
|
||||||
|
context: Context,
|
||||||
|
videoUrl: string | undefined,
|
||||||
|
url: string,
|
||||||
|
existingContentMessageId?: number,
|
||||||
|
) {
|
||||||
|
let contentMessageId = existingContentMessageId;
|
||||||
|
|
||||||
|
if (videoUrl && !contentMessageId) {
|
||||||
|
const { video, ...videoMessage } = await context.replyWithVideo(
|
||||||
|
new InputFile({ url: videoUrl }),
|
||||||
|
);
|
||||||
|
|
||||||
|
contentMessageId = videoMessage.message_id;
|
||||||
|
await redis.set(url, video.file_id, 'EX', TTL_URLS);
|
||||||
|
}
|
||||||
|
|
||||||
|
return contentMessageId;
|
||||||
|
}
|
||||||
|
|
||||||
feature.on('message:text', logHandle('download-message'), async (context) => {
|
feature.on('message:text', logHandle('download-message'), async (context) => {
|
||||||
const url = context.message.text.trim();
|
const url = context.message.text.trim();
|
||||||
|
|
||||||
@ -22,36 +162,31 @@ feature.on('message:text', logHandle('download-message'), async (context) => {
|
|||||||
const isInstagram = validateInstagramUrl(url);
|
const isInstagram = validateInstagramUrl(url);
|
||||||
const isYoutube = validateYoutubeUrl(url);
|
const isYoutube = validateYoutubeUrl(url);
|
||||||
|
|
||||||
const isSupportedService = isTikTok || isInstagram || isYoutube;
|
if (!isTikTok && !isInstagram && !isYoutube) {
|
||||||
|
|
||||||
if (!isSupportedService) {
|
|
||||||
return context.reply(context.t('err-invalid-url'));
|
return context.reply(context.t('err-invalid-url'));
|
||||||
}
|
}
|
||||||
|
|
||||||
const cachedFileId = await redis.get(url);
|
const cacheResult = await checkCacheAndReply(context, url);
|
||||||
if (cachedFileId) {
|
if (cacheResult.captionSent) return;
|
||||||
return context.replyWithVideo(cachedFileId);
|
|
||||||
}
|
let contentMessageId = cacheResult.contentMessageId;
|
||||||
|
|
||||||
let imagesUrls: string[] | undefined;
|
let imagesUrls: string[] | undefined;
|
||||||
let videoUrl: string | undefined;
|
let videoUrl: string | undefined;
|
||||||
|
let caption: string | undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (isTikTok) {
|
const result = await getDownloadData(url, {
|
||||||
const result = await getTiktokDownloadUrl(url);
|
isInstagram,
|
||||||
imagesUrls = result.images;
|
isTikTok,
|
||||||
videoUrl = result.play;
|
isYoutube,
|
||||||
} else if (isInstagram) {
|
});
|
||||||
const result = await getInstagramDownloadUrl(url);
|
|
||||||
imagesUrls = result.images;
|
imagesUrls = result.imagesUrls;
|
||||||
videoUrl = result.play;
|
videoUrl = result.videoUrl;
|
||||||
} else if (isYoutube) {
|
caption = result.caption;
|
||||||
const result = await getYoutubeDownloadUrl(url);
|
} catch (error: unknown) {
|
||||||
videoUrl = result.play;
|
const message = (error as Error)?.message ?? String(error);
|
||||||
}
|
|
||||||
} catch (error_: unknown) {
|
|
||||||
const error = error_ as Error;
|
|
||||||
const message = error?.message ?? String(error);
|
|
||||||
if (typeof message === 'string' && message.startsWith('err-')) {
|
if (typeof message === 'string' && message.startsWith('err-')) {
|
||||||
return context.reply(context.t(message));
|
return context.reply(context.t(message));
|
||||||
}
|
}
|
||||||
@ -63,21 +198,9 @@ feature.on('message:text', logHandle('download-message'), async (context) => {
|
|||||||
return context.reply(context.t('err-invalid-download-urls'));
|
return context.reply(context.t('err-invalid-download-urls'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (imagesUrls?.length) {
|
contentMessageId = await sendImages(context, imagesUrls ?? [], contentMessageId);
|
||||||
const chunks = cluster(imagesUrls, 10);
|
contentMessageId = await sendVideoAndCache(context, videoUrl, url, contentMessageId);
|
||||||
for (const chunk of chunks) {
|
await sendCaptionAndCache(context, caption, url, contentMessageId);
|
||||||
await context.replyWithMediaGroup(
|
|
||||||
chunk.map((imageUrl) => ({ media: imageUrl, type: 'photo' })),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (videoUrl) {
|
|
||||||
const { video } = await context.replyWithVideo(new InputFile({ url: videoUrl }));
|
|
||||||
await redis.set(url, video.file_id, 'EX', TTL_URLS);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export { composer as download };
|
export { composer as download };
|
||||||
|
|||||||
@ -7,3 +7,5 @@ export const INSTAGRAM_URL_REGEX =
|
|||||||
|
|
||||||
export const YOUTUBE_URL_REGEX =
|
export const YOUTUBE_URL_REGEX =
|
||||||
/(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:watch\?v=|embed\/|v\/|shorts\/))([\w-]{11})(?:[?&]\S*)?/u;
|
/(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:watch\?v=|embed\/|v\/|shorts\/))([\w-]{11})(?:[?&]\S*)?/u;
|
||||||
|
|
||||||
|
export const TAGS = /#[\p{L}\p{N}_]+/gu;
|
||||||
|
|||||||
@ -14,6 +14,7 @@ export type CarouselItem = {
|
|||||||
|
|
||||||
export type Root = {
|
export type Root = {
|
||||||
authorInfo: AuthorInfo;
|
authorInfo: AuthorInfo;
|
||||||
|
caption: string;
|
||||||
carouselItems: CarouselItem[];
|
carouselItems: CarouselItem[];
|
||||||
id: number;
|
id: number;
|
||||||
mediaUrls: string[];
|
mediaUrls: string[];
|
||||||
@ -33,12 +34,14 @@ export async function getInstagramDownloadUrl(url: string) {
|
|||||||
|
|
||||||
if (isVideo) {
|
if (isVideo) {
|
||||||
return {
|
return {
|
||||||
|
caption: data.caption,
|
||||||
images: [],
|
images: [],
|
||||||
play: data.mediaUrls.at(0),
|
play: data.mediaUrls.at(0),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
caption: data.caption,
|
||||||
images: data.mediaUrls,
|
images: data.mediaUrls,
|
||||||
play: undefined,
|
play: undefined,
|
||||||
};
|
};
|
||||||
|
|||||||
3
apps/bot/src/utils/text.ts
Normal file
3
apps/bot/src/utils/text.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export function removeHashtags(caption: string): string {
|
||||||
|
return caption.replaceAll(/#[\p{L}\p{N}_-]+/gu, '').trim();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user