@kelvdra/scraper
Version:
A simple scraper by kelvdra.
74 lines (66 loc) • 1.96 kB
JavaScript
import axios from 'axios'
class Videy {
constructor() {
this.h = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
Referer: "https://videy.co/",
Origin: "https://videy.co"
};
this.cdn = "https://cdn.videy.co";
}
sz(b) {
const i = b > 0 ? Math.floor(Math.log(b) / Math.log(1024)) : 0;
return `${(b / Math.pow(1024, i)).toFixed(2)} ${"BKMGTPY"[i]}${i > 0 ? "B" : ""}`;
}
l(d) {
return !d || d.length === 8 || d.length === 9 && d.endsWith("1") ? "mp4" : d.length === 9 && d.endsWith("2") ? "mov" : "mp4";
}
pid(u) {
const s = u?.trim() || "";
const q = s.match(/[?&]id=([^&]+)/)?.[1];
return q || s.split("/").filter(Boolean).pop()?.split(".")[0] || "";
}
async download(url, rest = {}) {
console.log(`[LOG] Memproses: ${url}`);
try {
const i = this.pid(url);
if (!i || i.length < 5) throw new Error("ID_NOT_FOUND");
const ext = this.l(i);
const dl = `${this.cdn}/${i}.${ext}`;
console.log(`[LOG] Validasi HEAD: ${dl}`);
const res = await axios.head(dl, {
headers: this.h,
timeout: 10000,
...rest
});
const h = res?.headers || {};
const b = parseInt(h["content-length"] || 0, 10);
console.log(`[OK] Selesai: ${i}`);
return {
ok: true,
result: dl,
id: i,
ext,
type: h["content-type"] || `video/${ext}`,
size: this.sz(b),
bytes: b,
meta: {
server: h["server"] || "N/A",
etag: h["etag"]?.replace(/"/g, "") || null,
last_mod: h["last-modified"] || null,
status: res?.status || 200
},
at: new Date().toISOString()
};
} catch (e) {
console.error(`[ERR] ${e.message}`);
return {
ok: false,
result: null,
status: e?.response?.status || 500,
msg: e?.message || "ERROR"
};
}
}
}
export const videy = new Videy()