-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverifyUrl.js
More file actions
62 lines (57 loc) · 2.06 KB
/
verifyUrl.js
File metadata and controls
62 lines (57 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const axios = require('axios');
// Function to extract video ID from a YouTube URL
function extractYouTubeVideoId(url) {
const match = url.match(/(?:v=|\/)([0-9A-Za-z_-]{11})/);
return match ? match[1] : null;
}
// Function to extract tweet ID from a Twitter URL
function extractTweetId(url) {
const match = url.match(/twitter\.com\/.*\/status\/(\d+)/);
return match ? match[1] : null;
}
// Check if a YouTube video exists
async function checkYouTube(videoId) {
try {
const url = `https://www.googleapis.com/youtube/v3/videos?part=id&id=${videoId}&key=${process.env.YOUTUBE_API_KEY}`;
const response = await axios.get(url);
return response.data.items.length > 0;
} catch (error) {
console.error("YouTube API Error:", error.response?.data || error.message);
return false;
}
}
// Check if a tweet exists
async function checkTwitter(tweetId) {
try {
const url = `https://api.twitter.com/2/tweets/${tweetId}`;
const response = await axios.get(url, {
headers: { Authorization: `Bearer ${TWITTER_BEARER_TOKEN}` },
});
return response.status === 200;
} catch (error) {
console.error("Twitter API Error:", error.response?.data || error.message);
return false;
}
}
// Main function to check if a given URL is valid
module.exports = async function verifyURL(url) {
if (url.includes("youtube.com") || url.includes("youtu.be")) {
const videoId = extractYouTubeVideoId(url);
if (videoId) {
const exists = await checkYouTube(videoId);
return {success: exists, msg: `YouTube Video ${exists ? "exists" : "does NOT exist"}`};
} else {
return {success: false, msg: `Invalid YouTube URL`};
}
} else if (url.includes("twitter.com")) {
const tweetId = extractTweetId(url);
if (tweetId) {
const exists = await checkTwitter(tweetId);
return {success: exists, msg: `Tweet ${exists ? "exists" : "does NOT exist"}`};
} else {
return {success: false, msg: `Invalid Twitter URL`};
}
} else {
return {success: true, msg: `URL is neither a YouTube nor a Twitter link `};
}
}