const URL = require('url').URL;
const ConvertHtml = require('./ConvertHtml');
const sitesFilter = {
'www.fanfiction.net': {
converter: 'cutter',
begin: `
`,
end: `
`,
},
'archiveofourown.org': {
converter: 'cutter',
begin: ``,
end: ``,
},
'flibusta.is': {
converter: 'flibusta'
},
};
class ConvertSites extends ConvertHtml {
check(data, opts) {
const {url, dataType} = opts;
const parsedUrl = new URL(url);
if (dataType && dataType.ext == 'html') {
if (sitesFilter[parsedUrl.hostname])
return {hostname: parsedUrl.hostname};
}
return false;
}
async run(data, opts) {
if (!opts.enableSitesFilter)
return false;
const checkResult = this.check(data, opts);
if (!checkResult)
return false;
const {hostname} = checkResult;
let text = this.decode(data).toString();
text = this[sitesFilter[hostname].converter](text, sitesFilter[hostname]);
if (text === false)
return false;
return await super.run(Buffer.from(text), {skipCheck: true, cutTitle: true});
}
getTitle(text) {
let title = '';
const m = text.match(/
([\s\S]*?)<\/title>/);
if (m)
title = m[1];
return title.trim();
}
cutter(text, opts) {
const title = `${this.getTitle(text)}`;
const l = text.indexOf(opts.begin) + opts.begin.length;
const r = text.indexOf(opts.end);
if (l < 0 || r < 0 || r <= l)
return false;
return text.substring(l, r) + title;
}
flibusta(text) {
let author = '';
let m = text.match(/- ([\s\S]*?)<\/a>
/);
if (m)
author = m[1];
let book = this.getTitle(text);
book = book.replace(' (fb2) | Флибуста', '');
const title = `${author}${(author ? ' - ' : '')}${book}`;
let begin = '';
if (text.indexOf(begin) <= 0)
begin = '';
const end = '