Fix twitter downloader

This commit is contained in:
Divam 2023-10-24 16:05:16 +03:00
parent bf561f12e6
commit 6f743baee0
2 changed files with 13 additions and 2 deletions

View File

@ -2,9 +2,20 @@ const axios = require('axios');
const fs = require('fs'); const fs = require('fs');
const getTwitterMedia = require('get-twitter-media'); const getTwitterMedia = require('get-twitter-media');
async function convertXToTwitterURL(url) {
// Check if the URL is from x.com, and if so, convert it to twitter.com
if (url.includes('x.com')) {
url = url.replace('x.com', 'twitter.com');
}
return url;
}
async function downloadTwitterMedia(ctx, url) { async function downloadTwitterMedia(ctx, url) {
try { try {
let media = await getTwitterMedia(url, {buffer: true}); // Convert x.com URL to twitter.com if needed
url = await convertXToTwitterURL(url);
let media = await getTwitterMedia(url, { buffer: true });
if (media.found) { if (media.found) {
if (media.type === 'video' && media.url) { if (media.type === 'video' && media.url) {

View File

@ -51,7 +51,7 @@ bot.on('text', async (ctx) => {
const youtubeUrlRegex = /(https?:\/\/(?:www\.)?youtube\.com\/watch\?v=|https?:\/\/youtu\.be\/|https?:\/\/(?:www\.)?youtube\.com\/shorts\/)([\w-]{11})/gi; const youtubeUrlRegex = /(https?:\/\/(?:www\.)?youtube\.com\/watch\?v=|https?:\/\/youtu\.be\/|https?:\/\/(?:www\.)?youtube\.com\/shorts\/)([\w-]{11})/gi;
const tiktokUrlRegex = /(https?:\/\/(?:www\.)?tiktok\.com\/(?:@[\w.-]+\/video\/[\w-]+|@[\w.-]+)|vm\.tiktok\.com\/[\w.-]+|vt\.tiktok\.com\/[\w.-]+)/gi; const tiktokUrlRegex = /(https?:\/\/(?:www\.)?tiktok\.com\/(?:@[\w.-]+\/video\/[\w-]+|@[\w.-]+)|vm\.tiktok\.com\/[\w.-]+|vt\.tiktok\.com\/[\w.-]+)/gi;
const instagramUrlRegex = /(https?:\/\/(?:www\.)?instagram\.com\/(p|tv|reels|stories)\/[\w.-]+)/gi; const instagramUrlRegex = /(https?:\/\/(?:www\.)?instagram\.com\/(p|tv|reels|stories)\/[\w.-]+)/gi;
const twitterUrlRegex = /(https?:\/\/(?:www\.)?twitter\.com\/(?:[\w.-]+)\/status\/[\d]+|https?:\/\/t\.co\/[\w.-]+)/gi; const twitterUrlRegex = /(https?:\/\/(?:www\.)?(?:twitter|x)\.com\/(?:[\w.-]+)\/status\/[\d]+|https?:\/\/t\.co\/[\w.-]+)/gi;
const youtubeUrls = text.match(youtubeUrlRegex); const youtubeUrls = text.match(youtubeUrlRegex);
const tiktokUrls = text.match(tiktokUrlRegex); const tiktokUrls = text.match(tiktokUrlRegex);