const got = require('@/utils/got');
const get_header = require('./header');
async function get_twitter(t) {
try {
const resp = await got(`https://publish.twitter.com/oembed?url=${t}`);
return resp.data.html;
} catch (_) {
return `
This tweet may not exist
`;
}
}
async function get_fanbox(p) {
try {
const m = p.match(/creator\/([0-9]+)\/post\/([0-9]+)/);
const post_id = m[2];
const api_url = `https://api.fanbox.cc/post.info?postId=${post_id}`;
const resp = await got(api_url, { headers: get_header() });
const post = resp.data.body;
const home_url = `https://${post.creatorId}.fanbox.cc`;
const web_url = `${home_url}/posts/${post.id}`;
const datetime = new Date(post.updatedDatetime).toLocaleString('ja');
const box_html = `
`;
return { url: web_url, html: box_html };
} catch (_) {
return { url: null, html: `fanbox post (${p}) may not exist
` };
}
}
// embedded items
async function embed_map(e) {
const id = e.contentId || e.videoId;
const sp = e.serviceProvider;
let ret = `Unknown host: ${sp}, with ID: ${id}`;
let url = null;
try {
switch (sp) {
case 'youtube':
url = `https://www.youtube.com/embed/${id}`;
ret = ``;
break;
case 'vimeo':
url = `https://player.vimeo.com/video/${id}`;
ret = ``;
break;
case 'soundcloud':
url = `https://soundcloud.com/${id}`;
ret = ``;
break;
case 'twitter':
url = `https://twitter.com/i/status/${id}`;
ret = await get_twitter(url);
break;
case 'google_forms':
url = `https://docs.google.com/forms/d/e/${id}/viewform?embedded=true`;
ret = ``;
break;
case 'fanbox': {
const info = await get_fanbox(id);
url = info.url;
ret = info.html;
break;
}
case 'gist':
url = `https://gist.github.com/${id}`;
ret = ``;
break;
}
if (url) {
ret += `
Click here if embedded content is not loaded.`;
}
} catch (_) {
_;
}
return ret;
}
// render blocks
function passage_conv(p) {
const seg = p.text.split('');
// seg.push('');
if (p.styles) {
p.styles.map((s) => {
switch (s.type) {
case 'bold':
seg[s.offset] = `` + seg[s.offset];
seg[s.offset + s.length - 1] += ``;
break;
}
return s;
});
}
if (p.links) {
p.links.map((l) => {
seg[l.offset] = `` + seg[l.offset];
seg[l.offset + l.length - 1] += ``;
return l;
});
}
const ret = seg.join('');
// console.log(ret)
return ret;
}
// article types
function text_t(body) {
return body.text || '';
}
function image_t(body) {
let ret = body.text || '';
body.images.map((i) => (ret += `
`));
return ret;
}
function file_t(body) {
let ret = body.text || '';
body.files.map((f) => (ret += `
${f.name}.${f.extension}`));
return ret;
}
async function video_t(body) {
let ret = body.text || '';
ret += (await embed_map(body.video)) || '';
return ret;
}
async function blog_t(body) {
let ret = [];
for (let x = 0; x < body.blocks.length; ++x) {
const b = body.blocks[x];
ret.push('');
switch (b.type) {
case 'p':
ret.push(passage_conv(b));
break;
case 'header':
ret.push(`
${b.text}
`);
break;
case 'image': {
const i = body.imageMap[b.imageId];
ret.push(`
`);
break;
}
case 'file': {
const f = body.fileMap[b.fileId];
ret.push(`${f.name}.${f.extension}`);
break;
}
case 'embed':
ret.push(embed_map(body.embedMap[b.embedId])); // Promise object
break;
}
}
ret = await Promise.all(ret); // get real data
return ret.join('');
}
// parse by type
async function conv_article(i) {
let ret = '';
if (i.title) {
ret += `[${i.type}] ${i.title}
`;
}
if (i.feeRequired !== 0) {
ret += `Fee Required: ${i.feeRequired} JPY/month
`;
}
if (i.coverImageUrl) {
ret += `
`;
}
if (!i.body) {
ret += `${i.excerpt}`;
return ret;
}
// console.log(i);
// skip paywall
switch (i.type) {
case 'text':
ret += text_t(i.body);
break;
case 'file':
ret += file_t(i.body);
break;
case 'image':
ret += image_t(i.body);
break;
case 'video':
ret += await video_t(i.body);
break;
case 'article':
ret += await blog_t(i.body);
break;
default:
ret += 'Unsupported content (RSSHub)';
}
return ret;
}
// render wrapper
module.exports = async (i) => ({
title: i.title || `No title`,
description: await conv_article(i),
pubDate: new Date(i.publishedDatetime).toUTCString(),
link: `https://${i.creatorId}.fanbox.cc/posts/${i.id}`,
category: i.tags,
});