Create buddy.js
Browse files- lib/buddy.js +117 -0
lib/buddy.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const { Source, RunnerScraper, Hoster } = require("source-scraper-core");
|
| 2 |
+
const { PuppeteerRunner } = require("source-scraper-puppeteer-runner");
|
| 3 |
+
const cheerio = require("cheerio");
|
| 4 |
+
const u = require("url");
|
| 5 |
+
|
| 6 |
+
class NinexbuddySource extends Source {
|
| 7 |
+
constructor(source) {
|
| 8 |
+
super(source);
|
| 9 |
+
this.quality = source.quality;
|
| 10 |
+
}
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
function parseSourceQuality(raw) {
|
| 14 |
+
const ar1 = /^([a-zA-Z\d]+)\s*-\s*(\d+)([a-zA-Z]+)?$/i.exec(raw);
|
| 15 |
+
if (ar1 !== null && ar1.length >= 4)
|
| 16 |
+
return {
|
| 17 |
+
raw,
|
| 18 |
+
description: ar1[1],
|
| 19 |
+
value: Number(ar1[2]),
|
| 20 |
+
unit: ar1[3]
|
| 21 |
+
};
|
| 22 |
+
const ar2 = /^(\d+)([a-zA-Z]+)\s*\(?([a-zA-Z\s]+)\)?$/i.exec(raw);
|
| 23 |
+
if (ar2 !== null && ar2.length >= 3)
|
| 24 |
+
return {
|
| 25 |
+
raw,
|
| 26 |
+
description: ar2[3],
|
| 27 |
+
value: Number(ar2[1]),
|
| 28 |
+
unit: ar2[2]
|
| 29 |
+
};
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
function parseHosterName(raw) {
|
| 33 |
+
const ar = /^(?:embed\s*-)?\s*([a-zA-Z\d.]+)$/i.exec(raw.trim());
|
| 34 |
+
if (ar === null || ar.length < 2)
|
| 35 |
+
return;
|
| 36 |
+
return ar[ar.length - 1];
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
function isDefined(value) {
|
| 40 |
+
return typeof value !== 'undefined';
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
class NinexbuddyScraper extends RunnerScraper {
|
| 44 |
+
constructor() {
|
| 45 |
+
super();
|
| 46 |
+
this.name = 'ninexbuddy';
|
| 47 |
+
this.domains = [];
|
| 48 |
+
this.urlPattern = /.*/i;
|
| 49 |
+
this.runner = new PuppeteerRunner();
|
| 50 |
+
this.defaultOptions = {
|
| 51 |
+
runnerOptions: {
|
| 52 |
+
navigationOptions: {
|
| 53 |
+
waitUntil: 'networkidle0'
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
};
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
transformUrl(url) {
|
| 60 |
+
return `https://9xbuddy.com/process?url=${url}`;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
async execWithArgs({ page, url: ninexbuddyUrl }) {
|
| 64 |
+
const isHoster = !!(await page.$('.playlist_el').catch(() => false));
|
| 65 |
+
if (isHoster) {
|
| 66 |
+
const items = await page.$$('.playlist_el .text-blue');
|
| 67 |
+
const hosters = (await Promise.all(items.map(async (item) => {
|
| 68 |
+
const urlPromise = item.getProperty('href').then(async (e) => e.jsonValue()).catch(() => undefined);
|
| 69 |
+
const namePromise = item.getProperty('textContent').then(async (e) => e.jsonValue()).catch(() => undefined);
|
| 70 |
+
const [url, name] = await Promise.all([urlPromise, namePromise]);
|
| 71 |
+
const hosterUrl = u.parse(url, true).query.url;
|
| 72 |
+
if (!hosterUrl)
|
| 73 |
+
return;
|
| 74 |
+
const parsed = name ? parseHosterName(name) : undefined;
|
| 75 |
+
if (!parsed)
|
| 76 |
+
return;
|
| 77 |
+
return new Hoster({
|
| 78 |
+
url: hosterUrl,
|
| 79 |
+
name: parsed,
|
| 80 |
+
});
|
| 81 |
+
}))).filter(isDefined);
|
| 82 |
+
return {
|
| 83 |
+
ninexbuddyUrl,
|
| 84 |
+
hosters
|
| 85 |
+
};
|
| 86 |
+
} else {
|
| 87 |
+
const selector = '.tracking-wide.items-center.night-white';
|
| 88 |
+
const items = await page.$$(selector);
|
| 89 |
+
const sources = (await Promise.all(items.map(async (item) => {
|
| 90 |
+
const formatPromise = item.$eval('.w-24.text-blue', e => e.textContent).catch(() => undefined);
|
| 91 |
+
const qualityPromise = item.$eval('.w-1\\/2', e => e.textContent).catch(() => undefined);
|
| 92 |
+
const urlPromise = item.$eval('.leading-normal.sm\\:leading-none', e => e.getAttribute('href')).catch(() => undefined);
|
| 93 |
+
const [format, quality, url] = await Promise.all([formatPromise, qualityPromise, urlPromise]);
|
| 94 |
+
if (!url)
|
| 95 |
+
return;
|
| 96 |
+
return new NinexbuddySource({
|
| 97 |
+
url,
|
| 98 |
+
quality: quality ? parseSourceQuality(quality) : undefined,
|
| 99 |
+
type: format || undefined,
|
| 100 |
+
codec: undefined
|
| 101 |
+
});
|
| 102 |
+
}))).filter(isDefined);
|
| 103 |
+
const poster = await page.$eval('img.shadow.border-2.border-grey-lighter.h-48', e => e.src).catch(() => undefined);
|
| 104 |
+
return {
|
| 105 |
+
ninexbuddyUrl,
|
| 106 |
+
title: await page.title(),
|
| 107 |
+
poster: poster && !poster.startsWith('data:image') ? poster : undefined,
|
| 108 |
+
sources
|
| 109 |
+
};
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
module.exports = {
|
| 115 |
+
NinexbuddySource,
|
| 116 |
+
NinexbuddyScraper
|
| 117 |
+
}
|