mirror of
https://codeberg.org/ashley/poke.git
synced 2025-06-17 11:57:03 -04:00
rename fetcher to dislikes :3
This commit is contained in:
parent
6006946ce5
commit
7c035cafac
1 changed files with 2 additions and 9 deletions
108
src/libpoketube/libpoketube-dislikes.js
Normal file
108
src/libpoketube/libpoketube-dislikes.js
Normal file
|
@ -0,0 +1,108 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
const { curly } = require("node-libcurl");
|
||||
const { toJson } = require("xml2json");
|
||||
|
||||
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";
|
||||
|
||||
/**
|
||||
* 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 = {};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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://returnyoutubedislikeapi.com/votes?videoId=${this.videoId}`;
|
||||
|
||||
const { fetch } = await import("undici");
|
||||
|
||||
|
||||
try {
|
||||
const engagement = await fetch(apiUrl).then((res) => res.json());
|
||||
return engagement.data;
|
||||
} catch {
|
||||
try {
|
||||
const engagement = await fetch(fallbackUrl).then((res) => res.json());
|
||||
return engagement;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
videoUrlYoutube: `${YOUTUBE_URL}${this.videoId}`,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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();
|
||||
};
|
||||
|
||||
module.exports = getBasicPokeTubeData;
|
Loading…
Add table
Add a link
Reference in a new issue