const fetch = require("node-fetch"); const { exec } = require("child_process"); const PM_KEYS = ["9rg4e6tpe2zp23d5", "mmsfyc59gwtis3g8", "tia5hhs9pddz6m7c"]; const apikey = PM_KEYS[Math.floor(Math.random() * PM_KEYS.length)]; const VIDEO_EXTENSIONS = [ "3g2", "3gp", "avi", "flv", "mkv", "mk3d", "mov", "mp2", "mp4", "m4v", "mpe", "mpeg", "mpg", "mpv", "webm", "wmv", "ogm", "ts", "m2ts", ]; const SUBTITLE_EXTENSIONS = [ "aqt", "gsub", "jss", "sub", "ttxt", "pjs", "psb", "rt", "smi", "slt", "ssf", "srt", "ssa", "ass", "usf", "idx", "vtt", ]; let checkPremiumizeRes = (res = {}) => { if ("status" in res) { return res["status"] == "success"; } return false; }; let checkAccount = async () => { let url = `https://www.premiumize.me/api/account/info?apikey=${apikey}`; try { let res = await fetch(url); return checkPremiumizeRes(await res.json()); } catch (error) { console.log({ errorcheckAccount: error }); return false; } }; let checkCached = async (hash = "") => { if (!hash) return false; let url = `https://www.premiumize.me/api/cache/check?apikey=${apikey}&items[]=${hash}`; try { let res = await fetch(url); let resJson = (await res.json()) ?? {}; if (checkPremiumizeRes(resJson)) { if ( "response" in resJson && resJson["response"][0] && "transcoded" in resJson && resJson["transcoded"][0] ) return resJson["filename"][0]; } return false; } catch (error) { console.log({ errorCheckCached: error }); return false; } }; let getDirectDl = async (hash = "") => { if (!hash) return false; let url = `https://www.premiumize.me/api/transfer/directdl?apikey=${apikey}`; let form = new URLSearchParams(); form.append("src", `magnet:?xt=urn:btih:${hash}`); try { let res = await fetch(url, { method: "POST", body: form, timeout: 5000, headers: { "Content-Type": "application/x-www-form-urlencoded", }, }); let resJson = (await res.json()) ?? {}; if (checkPremiumizeRes(resJson)) { if ("content" in resJson && resJson["content"]) return resJson["content"]; } return false; } catch (error) { console.log({ errorCached: error }); return false; } }; let pmTransferList = async (id = "") => { let url = `https://www.premiumize.me/api/transfer/list?apikey=${apikey}`; try { let res = await fetch(url, { method: "GET", timeout: 10000, }); let resJson = (await res.json()) ?? {}; if (checkPremiumizeRes(resJson)) { return "transfers" in resJson ? resJson["transfers"] : []; } return []; } catch (error) { return []; } }; let pmFolderList = async () => { let url = `https://www.premiumize.me/api/folder/list?apikey=${apikey}`; try { let res = await fetch(url, { method: "GET", timeout: 10000, }); let resJson = (await res.json()) ?? {}; if (checkPremiumizeRes(resJson)) { return "content" in resJson ? resJson["content"] : []; } return []; } catch (error) { return []; } }; let pmItemList = async () => { let url = `https://www.premiumize.me/api/item/listall?apikey=${apikey}`; try { let res = await fetch(url, { method: "GET", timeout: 10000, }); let resJson = (await res.json()) ?? {}; if (checkPremiumizeRes(resJson)) { return "files" in resJson ? resJson["files"] : []; } return []; } catch (error) { return []; } }; let pmItemDetails = async (id = "") => { let url = `https://www.premiumize.me/api/item/details?apikey=${apikey}&id=${id}`; try { let res = await fetch(url, { method: "GET", timeout: 5000, }); let resJson = (await res.json()) ?? {}; if ("id" in resJson && resJson["id"]) { return resJson; } return {}; } catch (error) { return {}; } }; let pmFolderDetails = async (id = "") => { let url = `https://www.premiumize.me/api/folder/list?apikey=${apikey}&id=${id}`; try { let res = await fetch(url, { method: "GET", timeout: 5000, }); let resJson = (await res.json()) ?? {}; let response = []; if (checkPremiumizeRes(resJson)) { let tmp = "content" in resJson ? resJson["content"] : []; for (const el of tmp) { if (el["type"] == "file") { response.push(el); } else if ((el["type"] = "folder")) { let res_temp = await pmFolderDetails(el["id"]); response = response.concat(res_temp); } } return response; } return []; } catch (error) { return []; } }; let checkTorrentFileinPM = async (param = "", type = "file") => { if (!param) return null; try { let itemList = await pmFolderList(); if (!itemList || !itemList.length) return null; let file = await new Promise((resolve, reject) => { resolve( itemList.find((el) => { return el["name"] == param; }) ); }); return "id" in file ? file : null; } catch (error) { return null; } }; let pmFolderId = async (transferId = "") => { try { let tranfers = await pmTransferList(); if (!tranfers || !tranfers.length) return null; let folder = await new Promise((resolve, reject) => { resolve( tranfers.find((el) => { return el["id"] == transferId || el["name"] == transferId; }) ); }); return "folder_id" in folder ? folder["folder_id"] : null; } catch (error) { return null; } }; let addMagnetToPM = async (magnet = "") => { // let check = await checkAccount(); // if (!check) return null; let url = `https://www.premiumize.me/api/transfer/create?apikey=${apikey}`; let form = new URLSearchParams(); form.append("src", magnet); try { let res = await fetch(url, { method: "POST", body: form, headers: { "Content-Type": "application/x-www-form-urlencoded", }, timeout: 5000, }); let resJson = (await res.json()) ?? {}; // console.log({ resJson }); if (checkPremiumizeRes(resJson)) { return "id" in resJson ? resJson["id"] : false; // { // "status": "success", // "id": "WsaYYCdDzDIHckfkf8dD0g", // "name": "Game Of Thrones Saison 1 [1080p] MULTI BluRay-Pop .Le TrĂ´ne De Fer 2011", // "type": "torrent" // } } } catch (error) { console.log({ addreserror: error }); return false; } }; let deleteMagnetFromPM = async (id = "") => { let url = `https://www.premiumize.me/api/transfer/delete?apikey=${apikey}`; // let form = new FormData(); let form = new URLSearchParams(); form.append("id", id); console.log(`FORM: id: ${form.get("id")}`); try { let res = await fetch(url, { method: "POST", body: form, timeout: 5000, headers: { "Content-Type": "application/x-www-form-urlencoded", }, }); let resJson = (await res.json()) ?? {}; console.log({ resJson }); if (checkPremiumizeRes(resJson)) { return resJson; // { // "status": "success", // } } return false; } catch (error) { return false; } }; // let deleteMagnetFromPM = async (id = "") => { // if (!id) return false; // let res = exec( // ` // curl --request POST \ // --url 'https://www.premiumize.me/api/transfer/delete?apikey=${apikey}' \ // --form id=${id} // `, // (error, stdout, stderr) => { // console.log(stdout); // console.log({ stderr }); // if (error !== null) { // console.log(`exec error: ${error}`); // } // } // ); // }; let pmDeleteAllStagnedTransfer = async () => { let list = await pmTransferList(); let res = await Promise.all( list .filter((el) => { return ( el["status"] == "running" && (el["message"].includes("unknown left") || el["progress"] == 0) ); }) .map((el) => { return deleteMagnetFromPM(el["id"]); }) ); return res; }; let removeDuplicate = (data = [], key = "name") => { let response = []; data.forEach((one, i) => { let index_ = response.findIndex((el) => el[key] == one[key]); index_ == -1 ? response.push(one) : null; }); return response; }; module.exports = { apikey, checkAccount, checkPremiumizeRes, addMagnetToPM, pmFolderDetails, pmFolderId, checkTorrentFileinPM, pmItemDetails, pmItemList, removeDuplicate, deleteMagnetFromPM, pmDeleteAllStagnedTransfer, checkCached, getDirectDl, };