Added Insex. Renamed q's stand-alone date function. Separated q's trim function. Release tile uses cover if available, and poster is not available.

This commit is contained in:
2020-02-12 04:39:57 +01:00
parent 2f894edda5
commit b8074205ef
21 changed files with 212 additions and 23 deletions

View File

@@ -4,6 +4,10 @@ const { JSDOM } = require('jsdom');
const moment = require('moment');
const bhttp = require('bhttp');
function trim(str) {
return str.trim().replace(/\s+/g, ' ');
}
function prefixProtocol(url, protocol = 'https') {
if (protocol && /^\/\//.test(url)) {
return `${protocol}:${url}`;
@@ -12,7 +16,7 @@ function prefixProtocol(url, protocol = 'https') {
return url;
}
function q(context, selector, attrArg, trim = true) {
function q(context, selector, attrArg, applyTrim = true) {
const attr = attrArg === true ? 'textContent' : attrArg;
if (attr) {
@@ -20,52 +24,52 @@ function q(context, selector, attrArg, trim = true) {
? context.querySelector(selector)?.[attr] || context.querySelector(selector)?.attributes[attr]?.value
: context[attr] || context[attr]?.attributes[attr]?.value;
return trim ? value?.trim() : value;
return applyTrim && value ? trim(value) : value;
}
return selector ? context.querySelector(selector) : context;
}
function qall(context, selector, attrArg, trim = true) {
function qall(context, selector, attrArg, applyTrim = true) {
const attr = attrArg === true ? 'textContent' : attrArg;
if (attr) {
return Array.from(context.querySelectorAll(selector), el => (trim ? el[attr]?.trim() : el[attr]));
return Array.from(context.querySelectorAll(selector), el => (applyTrim && el[attr] ? trim(el[attr]) : el[attr]));
}
return Array.from(context.querySelectorAll(selector));
}
function qtext(context, selector, trim = true) {
const el = q(context, selector, null, trim);
function qtext(context, selector, applyTrim = true) {
const el = q(context, selector, null, applyTrim);
if (!el) return null;
const text = Array.from(el.childNodes)
.filter(node => node.nodeName === '#text')
.map(node => (trim ? node.textContent : node.textContent.trim()))
.map(node => (applyTrim ? node.textContent : trim(node.textContent)))
.join(' ');
if (trim) return text.trim();
if (applyTrim) return trim(text);
return text;
}
function qmeta(context, selector, attrArg = 'content', trim = true) {
function qmeta(context, selector, attrArg = 'content', applyTrim = true) {
if (/meta\[.*\]/.test(selector)) {
return q(context, selector, attrArg, trim);
return q(context, selector, attrArg, applyTrim);
}
return q(context, `meta[${selector}]`, attrArg, trim);
return q(context, `meta[${selector}]`, attrArg, applyTrim);
}
function date(dateString, format, match) {
function formatDate(dateString, format, match) {
if (match) {
const dateStamp = dateString.trim().match(match);
const dateStamp = trim(dateString).match(match);
if (dateStamp) return moment.utc(dateStamp[0], format).toDate();
return null;
}
return moment.utc(dateString.trim(), format).toDate();
return moment.utc(trim(dateString), format).toDate();
}
function qdate(context, selector, format, match, attr = 'textContent') {
@@ -73,7 +77,7 @@ function qdate(context, selector, format, match, attr = 'textContent') {
if (!dateString) return null;
return date(dateString, format, match);
return formatDate(dateString, format, match);
}
function qimage(context, selector = 'img', attr = 'src', protocol = 'https') {
@@ -226,7 +230,7 @@ async function getAll(url, selector, headers) {
}
module.exports = {
date,
formatDate,
extract,
extractAll,
init,
@@ -235,11 +239,12 @@ module.exports = {
getAll,
context: init,
contextAll: initAll,
d: date,
fd: formatDate,
ex: extract,
exa: extractAll,
ctx: init,
ctxa: initAll,
geta: getAll,
fdate: formatDate,
...funcs,
};