2023-05-01 21:20:13 +02:00
|
|
|
const { curly } = require("node-libcurl");
|
|
|
|
const { toJson } = require("xml2json");
|
2023-03-12 07:38:21 +01:00
|
|
|
const fetch = require("node-fetch");
|
2022-03-02 21:05:55 +01:00
|
|
|
|
2023-05-01 21:20:13 +02:00
|
|
|
const YOUTUBE_URL = "https://www.youtube.com/watch?v=";
|
|
|
|
const DISLIKE_API = "https://p.poketube.fun/api?v=";
|
|
|
|
const NEW_API_URL = "https://inner-api.poketube.fun/api/player";
|
2023-02-25 18:47:28 +01:00
|
|
|
|
2023-05-01 21:20:13 +02:00
|
|
|
/**
|
|
|
|
* A class representing a PokeTube API instance for a specific video.
|
|
|
|
*/
|
|
|
|
class PokeTubeAPI {
|
|
|
|
/**
|
|
|
|
* Creates a new PokeTube API instance for the given video ID.
|
|
|
|
* @param {string} videoId - The ID of the YouTube video.
|
|
|
|
*/
|
|
|
|
constructor(videoId) {
|
|
|
|
this.videoId = videoId;
|
|
|
|
this.engagement = null;
|
|
|
|
this.videoData = null;
|
|
|
|
this.headers = {};
|
2023-03-04 15:08:54 +01:00
|
|
|
}
|
2022-12-21 16:42:06 +01:00
|
|
|
|
2023-05-01 21:20:13 +02:00
|
|
|
/**
|
|
|
|
* Retrieves data about the YouTube video from the InnerTube API.
|
|
|
|
* @deprecated This method is deprecated and may no longer work due to changes in the InnerTube API. Please use the `video()` function defined in `libpoketube-core` to retrieve video data.
|
|
|
|
* @returns {Promise<object|null>} A Promise that resolves with the video data, or null if an error occurs.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _getInnerTubeData() {
|
|
|
|
try {
|
|
|
|
const { data } = await curly.get(`${NEW_API_URL}?v=${this.videoId}`, {
|
|
|
|
httpHeader: Object.entries(this.headers).map(([k, v]) => `${k}: ${v}`),
|
|
|
|
});
|
2023-03-12 07:38:21 +01:00
|
|
|
|
2023-05-01 21:20:13 +02:00
|
|
|
const json = toJson(data);
|
|
|
|
return this._getJson(json);
|
|
|
|
} catch (error) {
|
|
|
|
this._handleError(`Error parsing XML: ${error}`);
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-06 14:53:46 +01:00
|
|
|
}
|
2023-03-12 07:38:21 +01:00
|
|
|
|
2023-05-01 21:20:13 +02:00
|
|
|
/**
|
|
|
|
* Parses a JSON string and returns the resulting object.
|
|
|
|
* @param {string} str - The JSON string to parse.
|
|
|
|
* @returns {object|null} The parsed JSON object, or null if an error occurs.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getJson(str) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(str);
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Retrieves engagement data for the YouTube video.
|
|
|
|
* @returns {Promise<object|null>} A Promise that resolves with the engagement data, or null if an error occurs.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _getEngagementData() {
|
|
|
|
const apiUrl = `${DISLIKE_API}${this.videoId}`;
|
|
|
|
const fallbackUrl = `https://p.poketube.fun/${apiUrl}`;
|
2023-02-28 15:12:57 +01:00
|
|
|
|
|
|
|
try {
|
2023-05-01 21:20:13 +02:00
|
|
|
const engagement = await fetch(apiUrl).then((res) => res.json());
|
|
|
|
return engagement.data;
|
2023-02-28 15:12:57 +01:00
|
|
|
} catch {
|
2023-05-01 21:20:13 +02:00
|
|
|
try {
|
|
|
|
const engagement = await fetch(fallbackUrl).then((res) => res.json());
|
|
|
|
return engagement;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
2023-02-28 15:12:57 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-20 14:38:25 +01:00
|
|
|
|
2023-05-01 21:20:13 +02:00
|
|
|
/**
|
|
|
|
* Retrieves data about the YouTube video and its engagement.
|
|
|
|
* @returns {Promise<object>} A Promise that resolves with an object containing video and engagement data.
|
|
|
|
*/
|
|
|
|
async getData() {
|
|
|
|
this.videoData = await this._getInnerTubeData();
|
|
|
|
this.engagement = await this._getEngagementData();
|
|
|
|
|
|
|
|
return {
|
|
|
|
video: this.videoData,
|
|
|
|
engagement: this.engagement,
|
|
|
|
videoUrlYoutube: `${YOUTUBE_URL}${this.videoId}`,
|
|
|
|
};
|
|
|
|
}
|
2022-12-20 14:38:25 +01:00
|
|
|
|
2023-05-01 21:20:13 +02:00
|
|
|
/**
|
|
|
|
* Logs an error message to the console.
|
|
|
|
* @param {string} args - The error message to log.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleError(args) {
|
|
|
|
console.error(`[LIBPT FETCHER ERROR] ${args}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns basic data about a given YouTube video using PokeTubeAPI.
|
|
|
|
@async
|
|
|
|
@function
|
|
|
|
@param {string} videoId - The YouTube video ID to get data for.
|
|
|
|
@returns {Promise<Object>} An object containing the video data and engagement data, as well as the YouTube URL for the video.
|
|
|
|
@throws {Error} If the video ID is invalid or the request fails.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const getBasicPokeTubeData = async (videoId) => {
|
|
|
|
const pokeTubeAPI = new PokeTubeAPI(videoId);
|
|
|
|
return await pokeTubeAPI.getData();
|
2022-06-20 11:00:10 +02:00
|
|
|
};
|
2023-02-25 18:47:28 +01:00
|
|
|
|
2023-04-20 00:09:29 +02:00
|
|
|
module.exports = getBasicPokeTubeData;
|