fix: channel id irregular
This commit is contained in:
parent
77b597e5d8
commit
c1a4025cbd
5 changed files with 1152 additions and 2026 deletions
41
routes/channel.js
Normal file
41
routes/channel.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
const axios = require("axios");
|
||||
const cheerio = require("cheerio");
|
||||
|
||||
function channelRouteHandler(app) {
|
||||
app.get("/api/channel/:username", async (req, res) => {
|
||||
try {
|
||||
const username = req.params.username;
|
||||
if (!username) {
|
||||
return res.status(400).json({ error: "Username is required" });
|
||||
}
|
||||
|
||||
const response = await axios.get(`https://www.youtube.com/${username}`);
|
||||
const $ = cheerio.load(response.data);
|
||||
const canonicalLink = $('link[rel="canonical"]').attr("href");
|
||||
|
||||
if (!canonicalLink) {
|
||||
return res.status(404).json({ error: "Channel not found" });
|
||||
}
|
||||
|
||||
const channelIdMatch = canonicalLink.match(/\/channel\/([\w-]+)/);
|
||||
const channelId = channelIdMatch ? channelIdMatch[1] : null;
|
||||
|
||||
if (!channelId) {
|
||||
return res.status(404).json({ error: "Could not extract channel ID" });
|
||||
}
|
||||
|
||||
return res.json({
|
||||
channelId: channelId,
|
||||
username: username,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching channel ID:", error.message);
|
||||
return res.status(500).json({
|
||||
error: "Failed to fetch channel ID",
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = channelRouteHandler;
|
|
@ -1,5 +1,5 @@
|
|||
const https = require("https");
|
||||
const yts = require('yt-search');
|
||||
const yts = require("yt-search");
|
||||
|
||||
function searchRouteHandler(app) {
|
||||
app.get("/api/search", async (req, res) => {
|
||||
|
@ -10,32 +10,36 @@ function searchRouteHandler(app) {
|
|||
}
|
||||
|
||||
const results = await yts(query);
|
||||
console.log(results.videos[0])
|
||||
|
||||
console.log(results.videos[0]);
|
||||
|
||||
const formattedResults = {
|
||||
items: results.videos.map(video => ({
|
||||
items: results.videos.map((video) => ({
|
||||
url: `/watch?v=${video.videoId}`,
|
||||
type: "stream",
|
||||
title: video.title,
|
||||
thumbnail: `/api/thumbnail/${video.videoId}`,
|
||||
uploaderName: video.author.name,
|
||||
uploaderUrl: `/channel/${video.author.url}`,
|
||||
uploaderAvatar: `/api/avatar/${video.author.url.replace("https://youtube.com/", "")}.png`,
|
||||
uploaderUrl: `${video.author.url.replace(
|
||||
"https://youtube.com/",
|
||||
""
|
||||
)}`,
|
||||
uploaderAvatar: `/api/avatar/${video.author.url.replace(
|
||||
"https://youtube.com/",
|
||||
""
|
||||
)}.png`,
|
||||
uploadedDate: video.ago,
|
||||
shortDescription: video.description,
|
||||
duration: video.seconds,
|
||||
views: video.views,
|
||||
uploaded: new Date(video.timestamp * 1000).getTime(),
|
||||
uploaderVerified: false,
|
||||
isShort: video.duration.seconds < 60
|
||||
isShort: video.duration.seconds < 60,
|
||||
})),
|
||||
nextpage: "",
|
||||
suggestion: "",
|
||||
corrected: false
|
||||
corrected: false,
|
||||
};
|
||||
|
||||
res.json(formattedResults);
|
||||
|
||||
} catch (err) {
|
||||
console.error(`Error searching videos: ${err.message}`);
|
||||
res.status(500).send("Error searching videos");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue