From 31e824e6c1f0df854a3007fe7c76cc6c3018b483 Mon Sep 17 00:00:00 2001 From: Himanshu Date: Thu, 20 Nov 2025 22:24:36 +0530 Subject: [PATCH] fix: Refactor hubcloudExtracter for improved link handling and readability --- dist/hubcloudExtractor.js | 2 +- providers/hubcloudExtractor.ts | 164 +++++++++++++++++++++++---------- 2 files changed, 114 insertions(+), 52 deletions(-) diff --git a/dist/hubcloudExtractor.js b/dist/hubcloudExtractor.js index cc4662a..71b85d8 100644 --- a/dist/hubcloudExtractor.js +++ b/dist/hubcloudExtractor.js @@ -1 +1 @@ -"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var desc=Object.getOwnPropertyDescriptor(m,k);desc&&!("get"in desc?!m.__esModule:desc.writable||desc.configurable)||(desc={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,desc)}:function(o,m,k,k2){void 0===k2&&(k2=k),o[k2]=m[k]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}:function(o,v){o.default=v}),__importStar=this&&this.__importStar||function(){var ownKeys=function(o){return ownKeys=Object.getOwnPropertyNames||function(o){var ar=[];for(var k in o)Object.prototype.hasOwnProperty.call(o,k)&&(ar[ar.length]=k);return ar},ownKeys(o)};return function(mod){if(mod&&mod.__esModule)return mod;var result={};if(null!=mod)for(var k=ownKeys(mod),i=0;i=300&&newLinkRes.status<400?newLinkRes.headers.get("location")||link:newLinkRes.url&&newLinkRes.url!==link?newLinkRes.url:newLinkRes.headers.get("location")||link,newLink.includes("googleusercontent"))newLink=newLink.split("?link=")[1];else{const newLinkRes2=yield fetch(newLink,{method:"HEAD",headers:headers_1.headers,signal:signal,redirect:"manual"});newLink=newLinkRes2.status>=300&&newLinkRes2.status<400?(null===(_c=newLinkRes2.headers.get("location"))||void 0===_c?void 0:_c.split("?link=")[1])||newLink:newLinkRes2.url&&newLinkRes2.url!==newLink?newLinkRes2.url.split("?link=")[1]||newLinkRes2.url:(null===(_d=newLinkRes2.headers.get("location"))||void 0===_d?void 0:_d.split("?link=")[1])||newLink}streamLinks.push({server:"hubcloud",link:newLink,type:"mkv"})}catch(error){}break;case null==link?void 0:link.includes("cloudflarestorage"):streamLinks.push({server:"CfStorage",link:link,type:"mkv"});break;case(null==link?void 0:link.includes("fastdl"))||(null==link?void 0:link.includes("fsl.")):streamLinks.push({server:"FastDl",link:link,type:"mkv"});break;case link.includes("hubcdn")&&!link.includes("/?id="):streamLinks.push({server:"HubCdn",link:link,type:"mkv"});break;default:if(null==link?void 0:link.includes(".mkv")){const serverName=(null===(_f=null===(_e=link.match(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/i))||void 0===_e?void 0:_e[1])||void 0===_f?void 0:_f.replace(/\./g," "))||"Unknown";streamLinks.push({server:serverName,link:link,type:"mkv"})}}}return streamLinks}catch(error){return[]}})} \ No newline at end of file diff --git a/providers/hubcloudExtractor.ts b/providers/hubcloudExtractor.ts index 129fb24..ed19041 100644 --- a/providers/hubcloudExtractor.ts +++ b/providers/hubcloudExtractor.ts @@ -1,85 +1,147 @@ -import axios from 'axios'; -import * as cheerio from 'cheerio'; -import {Stream} from './types'; -import {headers} from './headers'; +import axios from "axios"; +import * as cheerio from "cheerio"; +import { Stream } from "./types"; +import { headers } from "./headers"; const decode = function (value: string) { if (value === undefined) { - return ''; + return ""; } return atob(value.toString()); }; export async function hubcloudExtracter(link: string, signal: AbortSignal) { try { - console.log('hubcloudExtracter', link); - const baseUrl = link.split('/').slice(0, 3).join('/'); + console.log("hubcloudExtracter", link); + const baseUrl = link.split("/").slice(0, 3).join("/"); const streamLinks: Stream[] = []; - const vLinkRes = await axios(`${link}`, {headers, signal}); + const vLinkRes = await axios(`${link}`, { headers, signal }); const vLinkText = vLinkRes.data; const $vLink = cheerio.load(vLinkText); const vLinkRedirect = vLinkText.match(/var\s+url\s*=\s*'([^']+)';/) || []; let vcloudLink = - decode(vLinkRedirect[1]?.split('r=')?.[1]) || + decode(vLinkRedirect[1]?.split("r=")?.[1]) || vLinkRedirect[1] || - $vLink('.fa-file-download.fa-lg').parent().attr('href') || + $vLink(".fa-file-download.fa-lg").parent().attr("href") || link; - console.log('vcloudLink', vcloudLink); - if (vcloudLink?.startsWith('/')) { + console.log("vcloudLink", vcloudLink); + if (vcloudLink?.startsWith("/")) { vcloudLink = `${baseUrl}${vcloudLink}`; - console.log('New vcloudLink', vcloudLink); + console.log("New vcloudLink", vcloudLink); } const vcloudRes = await fetch(vcloudLink, { headers, signal, - redirect: 'follow', + redirect: "follow", }); const $ = cheerio.load(await vcloudRes.text()); // console.log('vcloudRes', $.text()); - const linkClass = $('.btn-success.btn-lg.h6,.btn-danger,.btn-secondary'); + const linkClass = $(".btn-success.btn-lg.h6,.btn-danger,.btn-secondary"); for (const element of linkClass) { const itm = $(element); - let link = itm.attr('href') || ''; - if (link?.includes('.dev') && !link?.includes('/?id=')) { - streamLinks.push({server: 'Cf Worker', link: link, type: 'mkv'}); - } - if (link?.includes('pixeld')) { - if (!link?.includes('api')) { - const token = link.split('/').pop(); - const baseUrl = link.split('/').slice(0, -2).join('/'); - link = `${baseUrl}/api/file/${token}?download`; - } - streamLinks.push({server: 'Pixeldrain', link: link, type: 'mkv'}); - } - if (link?.includes('hubcloud') || link?.includes('/?id=')) { - try { - const newLinkRes = await axios.head(link, {headers, signal}); - const newLink = - newLinkRes.request?.responseURL?.split('link=')?.[1] || link; - streamLinks.push({server: 'hubcloud', link: newLink, type: 'mkv'}); - } catch (error) { - console.log('hubcloudExtracter error in hubcloud link: ', error); - } - } - if (link?.includes('cloudflarestorage')) { - streamLinks.push({server: 'CfStorage', link: link, type: 'mkv'}); - } - if (link?.includes('fastdl')) { - streamLinks.push({server: 'FastDl', link: link, type: 'mkv'}); - } - if (link.includes('hubcdn')) { - streamLinks.push({ - server: 'HubCdn', - link: link, - type: 'mkv', - }); + let link = itm.attr("href") || ""; + + switch (true) { + case link?.includes(".dev") && !link?.includes("/?id="): + streamLinks.push({ server: "Cf Worker", link: link, type: "mkv" }); + break; + + case link?.includes("pixeld"): + if (!link?.includes("api")) { + const token = link.split("/").pop(); + const baseUrl = link.split("/").slice(0, -2).join("/"); + link = `${baseUrl}/api/file/${token}?download`; + } + streamLinks.push({ server: "Pixeldrain", link: link, type: "mkv" }); + break; + + case link?.includes("hubcloud") || link?.includes("/?id="): + try { + const newLinkRes = await fetch(link, { + method: "HEAD", + headers, + signal, + redirect: "manual", + }); + + // Check if response is a redirect (301, 302, etc.) + let newLink = link; + if (newLinkRes.status >= 300 && newLinkRes.status < 400) { + newLink = newLinkRes.headers.get("location") || link; + } else if (newLinkRes.url && newLinkRes.url !== link) { + // Fallback: check if URL changed (redirect was followed) + newLink = newLinkRes.url; + } else { + newLink = newLinkRes.headers.get("location") || link; + } + if (newLink.includes("googleusercontent")) { + newLink = newLink.split("?link=")[1]; + } else { + const newLinkRes2 = await fetch(newLink, { + method: "HEAD", + headers, + signal, + redirect: "manual", + }); + + // Check if response is a redirect + if (newLinkRes2.status >= 300 && newLinkRes2.status < 400) { + newLink = + newLinkRes2.headers.get("location")?.split("?link=")[1] || + newLink; + } else if (newLinkRes2.url && newLinkRes2.url !== newLink) { + // Fallback: URL changed due to redirect + newLink = newLinkRes2.url.split("?link=")[1] || newLinkRes2.url; + } else { + newLink = + newLinkRes2.headers.get("location")?.split("?link=")[1] || + newLink; + } + } + + streamLinks.push({ + server: "hubcloud", + link: newLink, + type: "mkv", + }); + } catch (error) { + console.log("hubcloudExtracter error in hubcloud link: ", error); + } + break; + + case link?.includes("cloudflarestorage"): + streamLinks.push({ server: "CfStorage", link: link, type: "mkv" }); + break; + + case link?.includes("fastdl") || link?.includes("fsl."): + streamLinks.push({ server: "FastDl", link: link, type: "mkv" }); + break; + + case link.includes("hubcdn") && !link.includes("/?id="): + streamLinks.push({ + server: "HubCdn", + link: link, + type: "mkv", + }); + break; + + default: + if (link?.includes(".mkv")) { + const serverName = + link + .match(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/i)?.[1] + ?.replace(/\./g, " ") || "Unknown"; + streamLinks.push({ server: serverName, link: link, type: "mkv" }); + } + break; } } - console.log('streamLinks', streamLinks); + + console.log("streamLinks", streamLinks); return streamLinks; } catch (error) { - console.log('hubcloudExtracter error: ', error); + console.log("hubcloudExtracter error: ", error); return []; } }