poke/src/libpoketube/libpoketube-fetcher.js

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-03-02 21:05:55 +01:00
/*
2023-02-09 17:34:45 +01:00
PokeTube is a Free/Libre youtube front-end !
Copyright (C) 2021-2023 POKETUBE
This file is Licensed under LGPL-3.0-or-later. Poketube itself is GPL, Only this file is LGPL.
see a copy here:https://www.gnu.org/licenses/lgpl-3.0.txt
please dont remove this comment while sharing this code
2022-03-02 21:05:55 +01:00
*/
2022-09-23 18:54:05 +02:00
2023-02-09 17:34:45 +01:00
2022-03-02 21:05:55 +01:00
const fetch = require("node-fetch"); //2.5.x
const { toJson } = require("xml2json");
2022-05-17 22:19:35 +02:00
2023-02-25 18:47:28 +01:00
const youtubeUrl = "https://www.youtube.com/watch?v=";
2023-02-28 15:12:57 +01:00
const dislikeApi = "https://returnyoutubedislikeapi.com/votes?videoId=";
2023-02-25 18:47:28 +01:00
const newApiUrl = "https://tube-srv.ashley143.gay/api/player";
const parseXml = async (videoId, headers) => {
const player = await fetch(`${newApiUrl}?v=${videoId}`, headers);
const xml = await player.text();
const json = toJson(xml);
return getJson(json);
};
2022-12-21 16:42:06 +01:00
2023-02-25 18:47:28 +01:00
const getJson = (str) => {
try {
return JSON.parse(str);
} catch {
return null;
2022-03-06 14:53:46 +01:00
}
2023-02-25 18:47:28 +01:00
};
2022-07-10 22:52:00 +02:00
2023-02-25 18:47:28 +01:00
const getEngagement = async (videoId) => {
2023-02-28 15:12:57 +01:00
const apiUrl = `${dislikeApi}${videoId}`;
const fallbackUrl = `https://p.poketube.fun/${apiUrl}`;
try {
const engagement = await fetch(apiUrl).then((res) => res.json());
return engagement;
} catch {
try {
const engagement = await fetch(fallbackUrl).then((res) => res.json());
return engagement;
} catch {
return;
}
}
2023-02-25 18:47:28 +01:00
};
2022-12-20 14:38:25 +01:00
2023-02-28 15:12:57 +01:00
2023-02-25 18:47:28 +01:00
const getPokeTubeData = async (videoId) => {
const headers = {};
const videoData = await parseXml(videoId, headers);
const engagement = await getEngagement(videoId);
2022-12-20 14:38:25 +01:00
2023-02-25 18:47:28 +01:00
return {
video: videoData,
2022-12-20 11:59:28 +01:00
engagement,
2023-02-25 18:47:28 +01:00
videoUrlYoutube: `${youtubeUrl}${videoId}`,
};
};
2023-02-25 18:47:28 +01:00
module.exports = getPokeTubeData