@davidpunya/web-scraper
Version:
The library web scraper for Restfull API's
206 lines (187 loc) • 7.98 kB
JavaScript
const axios = require("axios");
const util = require("util");
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
module.exports = class Function {
formatDate = (numer) => {
const myMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const myDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const tgl = new Date(numer);
const day = tgl.getDate();
const month = tgl.getMonth();
const thisDay = myDays[tgl.getDay()];
const yy = tgl.getFullYear();
return `${thisDay}, ${day} - ${myMonths[month]} - ${yy}`;
};
thisDay() {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // getMonth() dimulai dari 0
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
formatCount = (count) => {
const suffixes = ['', 'k', 'm', 'b', 't', 'q', 'Q', 's', 'S', 'o', 'n', 'd', 'U', 'D', 'Td', 'qd', 'Qd', 'sd', 'Sd', 'od', 'nd', 'V', 'Uv', 'Dv', 'Tv', 'qv', 'Qv', 'sv', 'Sv', 'ov', 'nv', 'T', 'UT', 'DT', 'TT', 'qt', 'QT', 'st', 'ST', 'ot', 'nt'];
const suffixIndex = Math.floor(Math.log10(count) / 3);
const suffix = suffixes[suffixIndex];
const scaled = count / Math.pow(10, suffixIndex * 3);
return (count < 1000 ? parseFloat(scaled) : scaled.toFixed(2)) + suffix;
};
formatSeconds = (seconds) => {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
return [
hours > 0 ? hours + ' Hour' + (hours > 1 ? 's' : '') : '',
minutes > 0 ? minutes + ' Minute' + (minutes > 1 ? 's' : '') : '',
secs > 0 ? secs + ' Second' + (secs > 1 ? 's' : '') : ''
].filter(Boolean).join(' ');
};
bytesToSize = (bytes, decimals = 2) => {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
};
generateRandomString = () => {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
let result = 'clx';
const length = 25;
for (let i = result.length; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};
jsonFormat = (json) => {
return JSON.stringify(json, null, 2);
};
getBuffer = async(url, options = {}) => {
try {
const res = await axios({
method: 'get',
url,
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
'DNT': 1,
'Upgrade-Insecure-Request': 1
},
...options,
responseType: 'arraybuffer'
});
return res.data;
} catch (err) {
return err;
}
};
fetchJson = async(url, options = {}) => {
try {
const res = await axios({
method: 'GET',
url,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
},
...options
});
return res.data;
} catch (err) {
return err;
}
};
isUrl = (url) => {
return url.match(new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)/, 'gi'));
};
getRandom = (ext) => {
return `${Math.floor(Math.random() * 10000)}${ext}`
};
shortUrl = async(url) => {
let res = await fetch(`https://tinyurl.com/api-create.php?url=${url}`)
return await res.text()
};
ranNumb = (min, max = null) => {
if (max !== null) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
} else {
return Math.floor(Math.random() * min) + 1
}
};
pickRandom = (list) => {
return list[Math.floor(Math.random() * list.length)]
};
msToTime = (ms) => {
if (isNaN(ms)) return '--';
let ye = Math.floor(ms / 31104000000);
let mo = Math.floor(ms / 2592000000) % 12;
let d = Math.floor(ms / 86400000) % 30;
let h = Math.floor(ms / 3600000) % 24;
let m = Math.floor(ms / 60000) % 60;
let s = Math.floor(ms / 1000) % 60;
let yeDisplay = ye > 0 ? ye + (ye == 1 ? " Year, " : " Year, ") : "";
let moDisplay = mo > 0 ? mo + (mo == 1 ? " Month, " : " Month, ") : "";
let dDisplay = d > 0 ? d + (d == 1 ? " Day, " : " Day, ") : "";
let hDisplay = h > 0 ? h + (h == 1 ? " Hour, " : " Hour, ") : "";
let mDisplay = m > 0 ? m + (m == 1 ? " Minute, " : " Minute, ") : "";
let sDisplay = s > 0 ? s + (s == 1 ? " Second" : " Second") : "";
let time = yeDisplay + moDisplay + dDisplay + hDisplay + mDisplay + sDisplay;
return time.trim();
};
formatAgo = (timestamp) => {
const milli = timestamp * 1000;
const now = Date.now();
const diffMilli = now - milli;
const secondsAgo = Math.floor(diffMilli / 1000);
if (secondsAgo < 60) {
return `${secondsAgo} Second Ago`;
}
const minutesAgo = Math.floor(secondsAgo / 60);
if (minutesAgo < 60) {
return `${minutesAgo} Minute Ago`;
}
const hoursAgo = Math.floor(minutesAgo / 60);
if (hoursAgo < 24) {
return `${hoursAgo} Hour Ago`;
}
const daysAgo = Math.floor(hoursAgo / 24);
if (daysAgo < 30) {
return `${daysAgo} Day Ago`;
}
const monthsAgo = Math.floor(daysAgo / 30);
if (monthsAgo < 12) {
return `${monthsAgo} Month Ago`;
}
const yearsAgo = Math.floor(monthsAgo / 12);
return `${yearsAgo} Year Ago`;
};
texted = (type, text) => {
switch (type) {
case 'bold':
return '*' + text + '*'
case 'italic':
return '_' + text + '_'
case 'monospace':
return '```' + text + '```'
case 'monospacev2':
return '`' + text + '`'
case 'strikethrough':
return '~' + text + '~'
}
};
runtime = (seconds) => {
seconds = Number(seconds);
var d = Math.floor(seconds / (3600 * 24));
var h = Math.floor((seconds % (3600 * 24)) / 3600);
var m = Math.floor((seconds % 3600) / 60);
var s = Math.floor(seconds % 60);
var dDisplay = d > 0 ? d + (d === 1 ? " day, " : " days, ") : "";
var hDisplay = h > 0 ? h + (h === 1 ? " hour, " : " hours, ") : "";
var mDisplay = m > 0 ? m + (m === 1 ? " minute, " : " minutes, ") : "";
var sDisplay = s > 0 ? s + (s === 1 ? " second" : " seconds") : "";
return dDisplay + hDisplay + mDisplay + sDisplay;
};
delay = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
};
}