selfdebrid/extractors/sendcm.js
2022-08-21 20:27:03 -04:00

78 lines
2.1 KiB
JavaScript

const axios = require("axios");
const cheerio = require("cheerio");
const lib = require("../lib");
module.exports = {
get: get,
hostnames: ["send.cm"],
tests: ["https://send.cm/d/E6Rq"]
}
async function get(url, options) {
lib.log(`sendcm`, `Forming POST data and headers...`);
let id;
if (url.includes(`/d/`)) id = await getId(url, options);
else id = new URL(url).pathname.split(`/`)[1].split(`/`)[0].split(`?`)[0];
let dlHeaders = lib.defaultHeaders;
dlHeaders["Content-Type"] = `application/x-www-form-urlencoded`;
dlHeaders["Referer"] = url;
dlHeaders["Origin"] = `https://send.cm`;
lib.log(`sendcm`, `Formed headers.`, [dlHeaders])
let dlBody = `op=download2&id=${id}&rand=&referer=&method_free=&method_premium=`;
lib.log(`sendcm`, `Formed POST data.`, [dlBody]);
lib.log(`sendcm`, `Sending download request...`, [], true, options);
try {
let resp = await axios({
headers: dlHeaders,
data: dlBody,
method: `POST`,
url: `https://send.cm/`,
maxRedirects: 0
});
throw new Error("Download request did not redirect properly. Check if the link is alive.");
} catch(err) {
if (err?.response?.headers?.location) {
lib.log(`sendcm`, `Extractor succeeded, downloading...`, [], true, options);
let dlLink = err?.response?.headers?.location;
let dlName = dlLink.split(`/`)[dlLink.split(`/`).length - 1];
let cloudHeaders = lib.defaultHeaders;
cloudHeaders["Referer"] = `https://send.cm/`;
cloudHeaders["Sec-Fetch-Site"] = `cross-site`;
delete cloudHeaders["Origin"];
delete cloudHeaders["Content-Type"];
lib.log(`sendcm`, `Sending headers:`, [cloudHeaders], false);
return {
axiosData: {
headers: cloudHeaders,
url: dlLink
},
metadata: {
fileName: dlName
},
type: "file"
};
} else throw err;
}
}
async function getId(d_url, options) {
lib.log(`sendcm`, `Getting non-short ID...`, [], true, options);
let resp = await axios({
url: d_url,
headers: lib.defaultHeaders
});
let $ = cheerio.load(resp.data);
return $("[name='F1'] > [name='id']").val();
}