This JavaScript code constitutes a functionality used in a bot...
This JavaScript code constitutes a functionality used in a bot (likely for an automated messaging/chat application). Its purpose is to obtain and provide video download links (preferably from Pixeldrain) for episodes of anime hosted on the website Otakudesu. Here’s a detailed step-by-step explanation of what the code does:
1. Define supporting functions:
-
fetchWithRetry(url, options, retries)
:- A helper function that attempts to fetch data from a URL using
axios
. - Implements retry logic, trying up to 3 times (or a specified number of retries) in case of errors.
- Outputs a warning message to the console if a request fails but retries.
- A helper function that attempts to fetch data from a URL using
-
scrapePixeldrainLink(safelinkUrl)
:- This function is used to extract Pixeldrain direct download links from a safelink or intermediary page.
- Steps:
- Fetch the safelink page using
fetchWithRetry
. - Handle any URL redirects to the final page using
response.request?.res?.responseUrl
. - Parse the HTML content using Cheerio to extract links using specific selectors (e.g.,
<meta>
tags or<a>
elements with a Pixeldrain URL). - Return the extracted Pixeldrain link if found, or
null
otherwise.
- Fetch the safelink page using
2. Main logic:
-
Input validation:
- The user provides a text input containing a URL (e.g., an episode page from Otakudesu), and the code validates it.
- If no valid input is provided, a message is sent to guide the user on how to use the command.
-
Fetch download links for the episode:
- The provided Otakudesu episode URL is sent to a backend API (
https://api.siputzx.my.id/api/anime/otakudesu/download
), which returns information about available download links. - Checks if the API response contains valid and useful data (e.g., for video quality and download links).
- The provided Otakudesu episode URL is sent to a backend API (
-
Filter and prioritize links:
- The code has a preferred quality order—
360p
,480p
,720p
, and1080p
. - For each quality, it iterates through the links provided by the API, looking for preferred hosts:
- If the host is Pixeldrain (
pdrain
), the link is directly used. - If the host is Desustream (or similar), the
scrapePixeldrainLink
function is called to attempt to extract Pixeldrain direct links from the intermediary page.
- If the host is Pixeldrain (
- The code has a preferred quality order—
-
Handle Pixeldrain links:
- Extracted valid Pixeldrain links are added to the
pxdlLinks
array, which stores information about download quality, a description, and a clickable message.
- Extracted valid Pixeldrain links are added to the
3. Respond to the user:
-
If no Pixeldrain links are available for the episode, inform the user that only Pixeldrain links are supported.
-
If Pixeldrain links exist, send a message (e.g., via the bot) with buttons allowing the user to select a download resolution:
- The user receives dynamic buttons corresponding to the available resolutions (360p, 480p, etc.).
- Clicking a button provides the direct Pixeldrain link for the chosen resolution.
-
Any errors (e.g., with API calls or scraping) are logged to the console, and an appropriate error message is sent to the user.
4. In summary:
The code is a scraping and API-interfacing mechanism:
- It retrieves download links for anime episodes from Otakudesu pages.
- It prioritizes fetching Pixeldrain direct links for ease of downloading.
- It provides the results to the user in an interactive format, allowing them to choose the desired resolution of the episode to download.
This code is likely part of a bot implementation in a messaging platform (e.g., WhatsApp, Telegram, etc.), where the workflow revolves around responding to user commands.