2023-05-21 22:24:24 +02:00
|
|
|
/**
|
|
|
|
* PokeTube is a Free/Libre youtube front-end !
|
|
|
|
*
|
|
|
|
* 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 don't remove this comment while sharing this code.
|
|
|
|
*/
|
|
|
|
|
2023-05-01 21:20:13 +02:00
|
|
|
/**
|
|
|
|
* A class representing a PokeTube API instance for a specific video.
|
|
|
|
*/
|
2023-11-12 12:55:43 +01:00
|
|
|
class PokeTubeDislikesAPIManager {
|
2023-05-01 21:20:13 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 07:09:06 +02:00
|
|
|
|
2023-11-12 18:25:04 +01:00
|
|
|
/**
|
2023-05-01 21:20:13 +02:00
|
|
|
* 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
|
|
|
|
*/
|
2023-11-20 15:41:54 +01:00
|
|
|
async _getEngagementData() {
|
2023-11-20 15:47:50 +01:00
|
|
|
const apiUrl = `https://prod-poketube.testing.poketube.fun/api?v=${this.videoId}&hash=d0550b6e28c8f93533a569c314d5b4e2`;
|
2023-11-20 15:41:54 +01:00
|
|
|
const fallbackUrl = `https://returnyoutubedislikeapi.com/votes?videoId=${this.videoId}`;
|
2023-11-12 12:55:43 +01:00
|
|
|
|
2023-11-20 15:41:54 +01:00
|
|
|
const { fetch } = await import("undici");
|
|
|
|
|
2023-11-12 12:55:43 +01:00
|
|
|
|
2023-11-20 15:41:54 +01:00
|
|
|
try {
|
2023-11-22 05:54:25 +01:00
|
|
|
const engagement = await fetch(apiUrl).then((res) => res.json());
|
2023-11-20 15:41:54 +01:00
|
|
|
return engagement;
|
|
|
|
} catch {
|
2023-11-22 05:58:00 +01:00
|
|
|
try {
|
2023-11-22 05:54:25 +01:00
|
|
|
const engagement = await fetch(fallbackUrl).then((res) => res.json());
|
2023-11-20 15:41:54 +01:00
|
|
|
return engagement;
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2023-10-21 09:28:29 +02:00
|
|
|
}
|
2023-10-21 07:12:57 +02:00
|
|
|
|
2023-11-12 18:25:04 +01:00
|
|
|
|
2023-11-20 15:41:54 +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.engagement = await this._getEngagementData();
|
|
|
|
|
|
|
|
return {
|
|
|
|
engagement: this.engagement,
|
2023-11-12 12:55:43 +01:00
|
|
|
};
|
2023-05-01 21:20:13 +02:00
|
|
|
}
|
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) {
|
2023-09-30 07:15:04 +02:00
|
|
|
console.error(`[LIBPT DISLIKES ERROR] ${args}`);
|
2023-05-01 21:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-21 09:28:29 +02:00
|
|
|
Returns basic data about a given YouTube video using PokeTubeAPI.
|
2023-05-01 21:20:13 +02:00
|
|
|
@async
|
|
|
|
@function
|
|
|
|
@param {string} videoId - The YouTube video ID to get data for.
|
2023-09-09 18:53:04 +02:00
|
|
|
@returns {Promise<Object>} An object containing the engagement data, as well as the YouTube URL for the video.
|
2023-05-01 21:20:13 +02:00
|
|
|
@throws {Error} If the video ID is invalid or the request fails.
|
|
|
|
*/
|
|
|
|
|
2023-10-21 06:51:20 +02:00
|
|
|
const getDislikesData = async (videoId) => {
|
|
|
|
const pokeTubeAPI = new PokeTubeDislikesAPIManager(videoId);
|
2023-10-21 09:28:29 +02:00
|
|
|
return await pokeTubeAPI.getData();
|
2022-06-20 11:00:10 +02:00
|
|
|
};
|
2023-02-25 18:47:28 +01:00
|
|
|
|
2023-10-21 06:51:20 +02:00
|
|
|
module.exports = getDislikesData;
|