inital release
This commit is contained in:
commit
1d708c14cf
26 changed files with 34335 additions and 0 deletions
132
routes/avatar.js
Normal file
132
routes/avatar.js
Normal file
|
@ -0,0 +1,132 @@
|
|||
const axios = require('axios');
|
||||
const cheerio = require('cheerio');
|
||||
|
||||
const avatarCache = new Map();
|
||||
const pendingFetches = new Map();
|
||||
const CACHE_TTL = 24 * 60 * 60 * 1000;
|
||||
|
||||
async function fetchAvatarImage(channelId) {
|
||||
try {
|
||||
|
||||
const channelUrl = `https://www.youtube.com/${channelId.startsWith('UC') ? 'channel/' : ''}${channelId}`;
|
||||
|
||||
console.log("loading" + channelUrl);
|
||||
const htmlResponse = await axios.get(channelUrl, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
}
|
||||
});
|
||||
|
||||
const $ = cheerio.load(htmlResponse.data);
|
||||
const imageUrl = $('meta[property="og:image"]').attr('content');
|
||||
|
||||
if (!imageUrl) {
|
||||
throw new Error('Profile picture URL not found in page');
|
||||
}
|
||||
|
||||
const imageResponse = await axios.get(imageUrl, {
|
||||
responseType: 'arraybuffer',
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
}
|
||||
});
|
||||
|
||||
const buffer = Buffer.from(imageResponse.data);
|
||||
const contentType = imageResponse.headers['content-type'] || 'image/jpeg';
|
||||
|
||||
avatarCache.set(channelId, {
|
||||
buffer,
|
||||
contentType,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
return { buffer, contentType };
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to fetch avatar: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function avatarRouteHandler(app) {
|
||||
app.get("/api/avatar/:channelId.png", async (req, res) => {
|
||||
try {
|
||||
let channelId = req.params.channelId;
|
||||
if (!channelId) {
|
||||
return res.status(400).json({ error: "Invalid channel ID" });
|
||||
}
|
||||
|
||||
if (!channelId.startsWith('UC') && !channelId.startsWith('@')) {
|
||||
channelId = `@${channelId}`;
|
||||
}
|
||||
|
||||
|
||||
const cached = avatarCache.get(channelId);
|
||||
if (cached && (Date.now() - cached.timestamp) < CACHE_TTL) {
|
||||
console.log(`Cache hit for ${channelId}`);
|
||||
res.set('Content-Type', cached.contentType);
|
||||
return res.send(cached.buffer);
|
||||
}
|
||||
|
||||
if (pendingFetches.has(channelId)) {
|
||||
console.log(`Awaiting pending fetch for ${channelId}`);
|
||||
const { buffer, contentType } = await pendingFetches.get(channelId);
|
||||
res.set('Content-Type', contentType);
|
||||
return res.send(buffer);
|
||||
}
|
||||
|
||||
console.log(`Fetching avatar for ${channelId}`);
|
||||
const fetchPromise = fetchAvatarImage(channelId);
|
||||
pendingFetches.set(channelId, fetchPromise);
|
||||
|
||||
try {
|
||||
const { buffer, contentType } = await fetchPromise;
|
||||
res.set('Content-Type', contentType);
|
||||
res.send(buffer);
|
||||
} finally {
|
||||
pendingFetches.delete(channelId);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error fetching avatar: ${err.message} ${err.stack}`);
|
||||
res.status(500).json({ error: "Error fetching avatar" });
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/api/avatar/channel/:channelId.png", async (req, res) => {
|
||||
try {
|
||||
let channelId = req.params.channelId;
|
||||
if (!channelId && !channelId.startsWith('UC')) {
|
||||
return res.status(400).json({ error: "Invalid channel ID" });
|
||||
}
|
||||
|
||||
const cached = avatarCache.get(channelId);
|
||||
if (cached && (Date.now() - cached.timestamp) < CACHE_TTL) {
|
||||
console.log(`Cache hit for ${channelId}`);
|
||||
res.set('Content-Type', cached.contentType);
|
||||
return res.send(cached.buffer);
|
||||
}
|
||||
|
||||
if (pendingFetches.has(channelId)) {
|
||||
console.log(`Awaiting pending fetch for ${channelId}`);
|
||||
const { buffer, contentType } = await pendingFetches.get(channelId);
|
||||
res.set('Content-Type', contentType);
|
||||
return res.send(buffer);
|
||||
}
|
||||
|
||||
console.log(`Fetching avatar for ${channelId}`);
|
||||
const fetchPromise = fetchAvatarImage(channelId);
|
||||
pendingFetches.set(channelId, fetchPromise);
|
||||
|
||||
try {
|
||||
const { buffer, contentType } = await fetchPromise;
|
||||
res.set('Content-Type', contentType);
|
||||
res.send(buffer);
|
||||
} finally {
|
||||
pendingFetches.delete(channelId);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error fetching avatar: ${err.message} ${err.stack}`);
|
||||
res.status(500).json({ error: "Error fetching avatar" });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = avatarRouteHandler;
|
431
routes/prepare.js
Normal file
431
routes/prepare.js
Normal file
|
@ -0,0 +1,431 @@
|
|||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { exec } = require("child_process");
|
||||
const isWindows = process.platform === "win32";
|
||||
const isArm = process.arch === "arm" || process.arch === "arm64";
|
||||
const isAarch64 = process.arch === "arm64";
|
||||
|
||||
const ytDlpPath = path.join(
|
||||
"deps",
|
||||
isWindows ? "yt-dlp.exe" :
|
||||
isArm ? (isAarch64 ? "yt-dlp_linux_aarch64" : "yt-dlp_linux_armv7l") :
|
||||
"yt-dlp"
|
||||
);
|
||||
|
||||
if (!isWindows) {
|
||||
fs.chmodSync(ytDlpPath, 0o755);
|
||||
}
|
||||
|
||||
process.env.PATH = `${path.join(process.cwd(), "deps")}${path.delimiter}${process.env.PATH}`;
|
||||
|
||||
|
||||
const activeVideoProcesses = new Map();
|
||||
const PROCESS_TIMEOUT = 30 * 60 * 1000;
|
||||
|
||||
function logWithVideoId(videoId, message, isError = false) {
|
||||
const timestamp = new Date().toISOString();
|
||||
const logFn = isError ? console.error : console.log;
|
||||
logFn(`[${timestamp}][${videoId || "NO_VIDEO_ID"}] ${message}`);
|
||||
}
|
||||
|
||||
async function processVideo(videoId) {
|
||||
function updateProgress(percentage, state) {
|
||||
if (activeVideoProcesses.has(videoId)) {
|
||||
const processInfo = activeVideoProcesses.get(videoId);
|
||||
processInfo.progress = percentage;
|
||||
if (state) {
|
||||
processInfo.state = state;
|
||||
}
|
||||
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
`Progress update: ${percentage.toFixed(2)}% (${
|
||||
state || processInfo.state
|
||||
})`
|
||||
);
|
||||
|
||||
for (const client of processInfo.clients) {
|
||||
if (client.ws.readyState === 1) {
|
||||
client.ws.send(JSON.stringify({ preparing: percentage }));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logWithVideoId(videoId, "Starting video download");
|
||||
updateProgress(0, "downloading");
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const videoDirectory = `videohost/${videoId}`;
|
||||
if (!fs.existsSync(videoDirectory)) {
|
||||
fs.mkdirSync(videoDirectory, { recursive: true });
|
||||
}
|
||||
const process = exec(
|
||||
`${ytDlpPath} --cookies cookies.txt --force-ipv4 --prefer-insecure -f "bestvideo[ext=mp4][height<=1440][vcodec^=avc]+bestaudio[ext=m4a]" --merge-output-format mp4 -o "videohost/${videoId}.mp4" "https://youtube.com/watch?v=${videoId}"`
|
||||
);
|
||||
|
||||
console.log(
|
||||
`${ytDlpPath} --cookies cookies.txt --force-ipv4 --prefer-insecure -f "bestvideo[ext=mp4][height<=1440][vcodec^=avc]+bestaudio[ext=m4a]" --merge-output-format mp4 -o "videohost/${videoId}.mp4" "https://youtube.com/watch?v=${videoId}"`
|
||||
);
|
||||
|
||||
let isFirstDownload = true;
|
||||
let currentDestination = "";
|
||||
|
||||
process.stdout.on("data", (data) => {
|
||||
const output = data.toString();
|
||||
|
||||
if (output.includes("[download] Destination:")) {
|
||||
isFirstDownload = !currentDestination;
|
||||
currentDestination = output;
|
||||
}
|
||||
|
||||
const match = output.match(/\[download\]\s+(\d+\.\d+)% of/);
|
||||
if (match) {
|
||||
let percentage = parseFloat(match[1]);
|
||||
if (!isFirstDownload) {
|
||||
percentage = 72 + percentage * 0.08;
|
||||
} else {
|
||||
percentage = percentage * 0.72;
|
||||
}
|
||||
updateProgress(percentage, "downloading");
|
||||
}
|
||||
});
|
||||
|
||||
process.stderr.on("data", (data) => {
|
||||
logWithVideoId(videoId, `yt-dlp stderr: ${data}`, true);
|
||||
});
|
||||
|
||||
process.on("close", async (code) => {
|
||||
if (code === 0) {
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
"Download completed, starting conversion to HLS"
|
||||
);
|
||||
updateProgress(80, "preparing");
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
logWithVideoId(videoId, "Starting conversion to HLS");
|
||||
updateProgress(80, "converting");
|
||||
|
||||
try {
|
||||
await convertToHLS(videoId, updateProgress);
|
||||
|
||||
fs.unlink(`videohost/${videoId}.mp4`, (err) => {
|
||||
if (err) {
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
`Error deleting original file: ${err.message}`,
|
||||
true
|
||||
);
|
||||
} else {
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
"Original mp4 file deleted successfully"
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
resolve();
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
} else {
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
`Download process exited with code ${code}`,
|
||||
true
|
||||
);
|
||||
reject(new Error("Unable to prepare video. Please try again later."));
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
reject(new Error(`Failed to start download process: ${error.message}`));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function convertToHLS(videoId, updateProgress) {
|
||||
logWithVideoId(videoId, "Starting HLS conversion");
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const videoDirectory = `videohost/${videoId}`;
|
||||
|
||||
const conversionProcess = exec(
|
||||
`ffmpeg -i "videohost/${videoId}.mp4" -c:v copy -c:a copy -hls_time 6 -hls_list_size 0 -hls_segment_type mpegts -hls_playlist_type vod -hls_segment_filename "${videoDirectory}/%03d.ts" "${videoDirectory}/playlist.m3u8"`
|
||||
);
|
||||
|
||||
let totalDuration = 0;
|
||||
|
||||
conversionProcess.stderr.on("data", (data) => {
|
||||
const output = data.toString();
|
||||
console.log("ffmpeg stderr:", output);
|
||||
|
||||
const durationMatch = output.match(
|
||||
/Duration: (\d{2}):(\d{2}):(\d{2})\.(\d{2})/
|
||||
);
|
||||
if (durationMatch) {
|
||||
const [_, hours, minutes, seconds, centiseconds] = durationMatch;
|
||||
totalDuration =
|
||||
parseFloat(hours) * 3600 +
|
||||
parseFloat(minutes) * 60 +
|
||||
parseFloat(seconds) +
|
||||
parseFloat(centiseconds) / 100;
|
||||
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
`Video duration: ${totalDuration.toFixed(2)} seconds`
|
||||
);
|
||||
}
|
||||
|
||||
const timeMatch = output.match(/time=(\d{2}):(\d{2}):(\d{2})\.(\d{2})/);
|
||||
if (timeMatch && totalDuration > 0) {
|
||||
const [_, hours, minutes, seconds, centiseconds] = timeMatch;
|
||||
const currentTime =
|
||||
parseFloat(hours) * 3600 +
|
||||
parseFloat(minutes) * 60 +
|
||||
parseFloat(seconds) +
|
||||
parseFloat(centiseconds) / 100;
|
||||
|
||||
const percentage = 80 + (currentTime / totalDuration) * 20;
|
||||
updateProgress(Math.min(percentage, 100), "converting");
|
||||
}
|
||||
});
|
||||
|
||||
conversionProcess.stdout.on("data", (data) => {
|
||||
logWithVideoId(videoId, `ffmpeg stdout: ${data}`);
|
||||
});
|
||||
|
||||
conversionProcess.on("close", (code) => {
|
||||
if (code === 0) {
|
||||
logWithVideoId(videoId, "Conversion completed successfully");
|
||||
updateProgress(100, "completed");
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error(`Conversion process exited with code ${code}`));
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
reject(new Error(`Failed to start conversion process: ${error.message}`));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function setupCleanupInterval() {
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const [videoId, info] of activeVideoProcesses.entries()) {
|
||||
if (now - info.startTime > PROCESS_TIMEOUT) {
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
"Cleaning up stalled process in periodic check",
|
||||
true
|
||||
);
|
||||
|
||||
for (const client of info.clients) {
|
||||
if (client.ws.readyState === 1) {
|
||||
client.ws.send(
|
||||
JSON.stringify({
|
||||
ok: false,
|
||||
error: "Video processing timed out",
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (info.timeoutId) {
|
||||
clearTimeout(info.timeoutId);
|
||||
}
|
||||
|
||||
activeVideoProcesses.delete(videoId);
|
||||
}
|
||||
}
|
||||
}, 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
|
||||
function prepareRouteHandler(app) {
|
||||
app.ws("/api/v1/prepare", (ws, req) => {
|
||||
let videoId = null;
|
||||
let clientId = Date.now() + Math.random().toString(36).substring(2, 15);
|
||||
|
||||
logWithVideoId(null, `New WebSocket connection established (${clientId})`);
|
||||
|
||||
ws.on("message", async (msg) => {
|
||||
try {
|
||||
const parsedMessage = JSON.parse(msg);
|
||||
|
||||
if (parsedMessage.videoId) {
|
||||
videoId = parsedMessage.videoId;
|
||||
ws.send(JSON.stringify({ ok: true }));
|
||||
logWithVideoId(videoId, `Request for video (client: ${clientId})`);
|
||||
|
||||
const isValidYoutubeId = /^[a-zA-Z0-9_-]{11}$/.test(videoId);
|
||||
if (!isValidYoutubeId) {
|
||||
ws.send(
|
||||
JSON.stringify({ ok: false, error: "Invalid YouTube video ID" })
|
||||
);
|
||||
logWithVideoId(videoId, "Invalid YouTube video ID", true);
|
||||
return;
|
||||
}
|
||||
|
||||
const videoPath = `videohost/${videoId}/playlist.m3u8`;
|
||||
if (fs.existsSync(videoPath)) {
|
||||
logWithVideoId(videoId, "Video already exists - sending stream URL");
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
done: true,
|
||||
streamUrl: "/videocd/" + videoId + "/playlist.m3u8",
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (activeVideoProcesses.has(videoId)) {
|
||||
const processInfo = activeVideoProcesses.get(videoId);
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
`Video is already being processed (state: ${
|
||||
processInfo.state
|
||||
}, progress: ${processInfo.progress.toFixed(
|
||||
2
|
||||
)}%), attaching client ${clientId}`
|
||||
);
|
||||
|
||||
processInfo.clients.push({ ws, clientId });
|
||||
|
||||
ws.send(JSON.stringify({ preparing: processInfo.progress }));
|
||||
return;
|
||||
}
|
||||
|
||||
const processInfo = {
|
||||
clients: [{ ws, clientId }],
|
||||
state: "initializing",
|
||||
progress: 0,
|
||||
startTime: Date.now(),
|
||||
timeoutId: null,
|
||||
};
|
||||
|
||||
processInfo.timeoutId = setTimeout(() => {
|
||||
if (activeVideoProcesses.has(videoId)) {
|
||||
logWithVideoId(videoId, "Process timed out after 30 minutes", true);
|
||||
|
||||
const info = activeVideoProcesses.get(videoId);
|
||||
for (const client of info.clients) {
|
||||
if (client.ws.readyState === 1) {
|
||||
client.ws.send(
|
||||
JSON.stringify({
|
||||
ok: false,
|
||||
error: "Video processing timed out",
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
activeVideoProcesses.delete(videoId);
|
||||
}
|
||||
}, PROCESS_TIMEOUT);
|
||||
|
||||
activeVideoProcesses.set(videoId, processInfo);
|
||||
logWithVideoId(videoId, "Starting new video processing");
|
||||
|
||||
try {
|
||||
await processVideo(videoId);
|
||||
|
||||
if (processInfo.timeoutId) {
|
||||
clearTimeout(processInfo.timeoutId);
|
||||
}
|
||||
|
||||
if (activeVideoProcesses.has(videoId)) {
|
||||
const info = activeVideoProcesses.get(videoId);
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
`Processing completed. Notifying ${info.clients.length} clients`
|
||||
);
|
||||
|
||||
for (const client of info.clients) {
|
||||
if (client.ws.readyState === 1) {
|
||||
client.ws.send(
|
||||
JSON.stringify({
|
||||
done: true,
|
||||
streamUrl: "/videocd/" + videoId + "/playlist.m3u8",
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
activeVideoProcesses.delete(videoId);
|
||||
}
|
||||
|
||||
logWithVideoId(videoId, "Video prepared successfully");
|
||||
} catch (error) {
|
||||
if (processInfo.timeoutId) {
|
||||
clearTimeout(processInfo.timeoutId);
|
||||
}
|
||||
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
`Error processing video: ${error.message}`,
|
||||
true
|
||||
);
|
||||
|
||||
if (activeVideoProcesses.has(videoId)) {
|
||||
const info = activeVideoProcesses.get(videoId);
|
||||
for (const client of info.clients) {
|
||||
if (client.ws.readyState === 1) {
|
||||
client.ws.send(
|
||||
JSON.stringify({
|
||||
ok: false,
|
||||
error:
|
||||
"Unable to prepare video. Please try reloading the page.",
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
activeVideoProcesses.delete(videoId);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logWithVideoId(videoId, `Unexpected error: ${error.message}`, true);
|
||||
ws.send(JSON.stringify({ ok: false, error: "Server error occurred" }));
|
||||
}
|
||||
});
|
||||
|
||||
ws.on("close", () => {
|
||||
logWithVideoId(videoId, `WebSocket closed for client ${clientId}`);
|
||||
|
||||
if (videoId && activeVideoProcesses.has(videoId)) {
|
||||
const processInfo = activeVideoProcesses.get(videoId);
|
||||
const clientIndex = processInfo.clients.findIndex(
|
||||
(client) => client.clientId === clientId
|
||||
);
|
||||
|
||||
if (clientIndex !== -1) {
|
||||
processInfo.clients.splice(clientIndex, 1);
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
`Removed client ${clientId} from process. Remaining clients: ${processInfo.clients.length}`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ws.on("error", (error) => {
|
||||
logWithVideoId(
|
||||
videoId,
|
||||
`WebSocket error for client ${clientId}: ${error.message}`,
|
||||
true
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
setupCleanupInterval();
|
||||
}
|
||||
|
||||
module.exports = prepareRouteHandler;
|
46
routes/search.js
Normal file
46
routes/search.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
const https = require("https");
|
||||
const yts = require('yt-search');
|
||||
|
||||
function searchRouteHandler(app) {
|
||||
app.get("/api/search", async (req, res) => {
|
||||
try {
|
||||
const query = req.query.q;
|
||||
if (!query) {
|
||||
return res.status(400).json({ error: "Search query is required" });
|
||||
}
|
||||
|
||||
const results = await yts(query);
|
||||
console.log(results.videos[0])
|
||||
|
||||
const formattedResults = {
|
||||
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`,
|
||||
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
|
||||
})),
|
||||
nextpage: "",
|
||||
suggestion: "",
|
||||
corrected: false
|
||||
};
|
||||
|
||||
res.json(formattedResults);
|
||||
|
||||
} catch (err) {
|
||||
console.error(`Error searching videos: ${err.message}`);
|
||||
res.status(500).send("Error searching videos");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = searchRouteHandler;
|
11
routes/static.js
Normal file
11
routes/static.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
const express = require("express");
|
||||
|
||||
function staticRouteHandler(app) {
|
||||
// main application
|
||||
app.use("/", express.static("public"));
|
||||
|
||||
// video files
|
||||
app.use("/videocd/", express.static("videohost"));
|
||||
}
|
||||
|
||||
module.exports = staticRouteHandler;
|
71
routes/thumbnail.js
Normal file
71
routes/thumbnail.js
Normal file
|
@ -0,0 +1,71 @@
|
|||
const https = require("https");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
function thumbnailRouteHandler(app) {
|
||||
const cacheDir = path.join(__dirname, "../cache");
|
||||
if (!fs.existsSync(cacheDir)) {
|
||||
fs.mkdirSync(cacheDir);
|
||||
}
|
||||
|
||||
setInterval(() => {
|
||||
fs.readdir(cacheDir, (err, files) => {
|
||||
if (err) {
|
||||
console.error("Error reading cache directory:", err);
|
||||
return;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
files.forEach(file => {
|
||||
const filePath = path.join(cacheDir, file);
|
||||
fs.stat(filePath, (err, stats) => {
|
||||
if (err) {
|
||||
console.error("Error getting file stats:", err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (now - stats.mtime.getTime() > 30 * 24 * 60 * 60 * 1000) {
|
||||
fs.unlink(filePath, err => {
|
||||
if (err) console.error("Error deleting cached file:", err);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 24 * 60 * 60 * 1000);
|
||||
|
||||
app.get("/api/thumbnail/:videoId", (req, res) => {
|
||||
const videoId = req.params.videoId;
|
||||
const cachePath = path.join(cacheDir, `${videoId}.jpg`);
|
||||
|
||||
if (fs.existsSync(cachePath)) {
|
||||
return fs.createReadStream(cachePath).pipe(res);
|
||||
}
|
||||
|
||||
const thumbnailUrl = `https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`;
|
||||
|
||||
https.get(thumbnailUrl, (response) => {
|
||||
if (response.statusCode === 404) {
|
||||
const fallbackUrl = `https://img.youtube.com/vi/${videoId}/hqdefault.jpg`;
|
||||
https.get(fallbackUrl, (fallbackResponse) => {
|
||||
const fileStream = fs.createWriteStream(cachePath);
|
||||
fallbackResponse.pipe(fileStream);
|
||||
fallbackResponse.pipe(res);
|
||||
}).on("error", (err) => {
|
||||
console.error(`Error fetching fallback thumbnail: ${err.message}`);
|
||||
res.status(500).send("Error fetching thumbnail");
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const fileStream = fs.createWriteStream(cachePath);
|
||||
response.pipe(fileStream);
|
||||
response.pipe(res);
|
||||
}).on("error", (err) => {
|
||||
console.error(`Error fetching thumbnail: ${err.message}`);
|
||||
res.status(500).send("Error fetching thumbnail");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = thumbnailRouteHandler;
|
55
routes/trending.js
Normal file
55
routes/trending.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
const https = require("https");
|
||||
|
||||
function trendingRouteHandler(app) {
|
||||
let cachedData = null;
|
||||
let cacheTimestamp = null;
|
||||
const CACHE_DURATION = 10 * 60 * 1000;
|
||||
|
||||
app.get("/api/trending", (req, res) => {
|
||||
if (cachedData && cacheTimestamp && Date.now() - cacheTimestamp < CACHE_DURATION) {
|
||||
return res.json(cachedData);
|
||||
}
|
||||
|
||||
https.get("https://pipedapi.wireway.ch/trending?region=US", (response) => {
|
||||
let data = "";
|
||||
|
||||
response.on("data", (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
response.on("end", () => {
|
||||
try {
|
||||
const jsonData = JSON.parse(data);
|
||||
|
||||
jsonData.forEach(item => {
|
||||
if (item.url && item.url.startsWith("/watch?v=")) {
|
||||
const videoId = item.url.replace("/watch?v=", "");
|
||||
item.thumbnail = `/api/thumbnail/${videoId}`;
|
||||
}
|
||||
});
|
||||
|
||||
cachedData = jsonData;
|
||||
cacheTimestamp = Date.now();
|
||||
|
||||
res.json(jsonData);
|
||||
} catch (err) {
|
||||
console.error(`Error parsing trending data: ${err.message}`);
|
||||
if (cachedData) {
|
||||
cacheTimestamp = Date.now();
|
||||
return res.json(cachedData);
|
||||
}
|
||||
res.status(500).send("Error processing trending data");
|
||||
}
|
||||
});
|
||||
}).on("error", (err) => {
|
||||
console.error(`Error fetching trending data: ${err.message}`);
|
||||
if (cachedData) {
|
||||
cacheTimestamp = Date.now();
|
||||
return res.json(cachedData);
|
||||
}
|
||||
res.status(500).send("Error fetching trending data");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = trendingRouteHandler;
|
Loading…
Add table
Add a link
Reference in a new issue