fix: picnob
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
{{ if item.type === 'video' || item.type === 'igtv' }}
|
||||
<video poster="{{ item.pic }}" controls preload="metadata">
|
||||
<source src="{{ item.video }}" type="video/mp4">
|
||||
</video>
|
||||
{{ else if item.type === 'img_multi' }}
|
||||
{{ each item.images i }}
|
||||
{{ if i.isVideo }}
|
||||
<video poster="{{ i.url }}" controls preload="metadata">
|
||||
<source src="{{ i.ori }}" type="video/mp4">
|
||||
</video>
|
||||
{{ else }}
|
||||
<img src="{{ i.url }}">
|
||||
{{ /if }}
|
||||
<br>
|
||||
{{ /each }}
|
||||
{{ else if item.type === 'img_sig' }}
|
||||
<img src="{{ item.pic }}">
|
||||
{{ /if }}
|
||||
<br>
|
||||
{{@ item.sum }}
|
||||
+63
-130
@@ -1,14 +1,12 @@
|
||||
import { Route, ViewType } from '@/types';
|
||||
|
||||
import cache from '@/utils/cache';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { art } from '@/utils/render';
|
||||
import path from 'node:path';
|
||||
import { parseRelativeDate } from '@/utils/parse-date';
|
||||
import { puppeteerGet } from './utils';
|
||||
import puppeteer from '@/utils/puppeteer';
|
||||
import sanitizeHtml from 'sanitize-html';
|
||||
import cache from '@/utils/cache';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/user/:id/:type?',
|
||||
@@ -37,7 +35,7 @@ export const route: Route = {
|
||||
},
|
||||
],
|
||||
name: 'User Profile - Picnob',
|
||||
maintainers: ['TonyRL', 'micheal-death', 'AiraNadih'],
|
||||
maintainers: ['TonyRL', 'micheal-death', 'AiraNadih', 'DIYgod'],
|
||||
handler,
|
||||
view: ViewType.Pictures,
|
||||
};
|
||||
@@ -51,145 +49,80 @@ async function handler(ctx) {
|
||||
|
||||
const browser = await puppeteer();
|
||||
// TODO: can't bypass cloudflare 403 error without puppeteer
|
||||
|
||||
try {
|
||||
const profile = (await cache.tryGet(`picnob:user:${id}`, async () => {
|
||||
let html;
|
||||
let usePuppeteer = false;
|
||||
try {
|
||||
const data = await ofetch(profileUrl, {
|
||||
headers: {
|
||||
accept: 'text/html',
|
||||
referer: 'https://www.google.com/',
|
||||
},
|
||||
});
|
||||
html = data;
|
||||
} catch {
|
||||
html = await puppeteerGet(profileUrl, browser);
|
||||
usePuppeteer = true;
|
||||
}
|
||||
const $ = load(html);
|
||||
const name = $('h1.fullname').text();
|
||||
const userId = $('input[name=userid]').attr('value');
|
||||
let html;
|
||||
try {
|
||||
const data = await ofetch(profileUrl);
|
||||
html = data;
|
||||
} catch {
|
||||
html = await puppeteerGet(profileUrl, browser);
|
||||
}
|
||||
const $ = load(html);
|
||||
const name = $('h1.fullname').text();
|
||||
|
||||
if (!userId) {
|
||||
throw new Error('Failed to get user ID');
|
||||
}
|
||||
const profile = {
|
||||
name,
|
||||
description: $('.info .sum').text(),
|
||||
image: $('.ava .pic img').attr('src'),
|
||||
items: $('.post_box')
|
||||
.toArray()
|
||||
.map((item) => {
|
||||
const $item = $(item);
|
||||
const sum = $item.find('.sum').text();
|
||||
const coverLink = $item.find('.cover_link').attr('href');
|
||||
const shortcode = coverLink?.split('/')?.[2];
|
||||
|
||||
return {
|
||||
name,
|
||||
userId,
|
||||
description: $('.info .sum').text(),
|
||||
image: $('.ava .pic img').attr('src'),
|
||||
usePuppeteer,
|
||||
};
|
||||
})) as {
|
||||
name: string;
|
||||
userId: string;
|
||||
description: string;
|
||||
image: string;
|
||||
usePuppeteer: boolean;
|
||||
return {
|
||||
title: sanitizeHtml(sum.split('\n')[0], { allowedTags: [], allowedAttributes: {} }),
|
||||
description: `<img src="${$item.find('.preview_w img').attr('data-src')}" /><br />${sum.replaceAll('\n', '<br>')}`,
|
||||
link: `${baseUrl}${coverLink}`,
|
||||
guid: shortcode,
|
||||
pubDate: parseRelativeDate($item.find('.time .txt').text()),
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
let profileTitle;
|
||||
let endpoint;
|
||||
if (type === 'tagged') {
|
||||
profileTitle = `${profile.name} (@${id}) tagged posts - Picnob`;
|
||||
endpoint = 'tagged';
|
||||
} else {
|
||||
profileTitle = `${profile.name} (@${id}) public posts - Picnob`;
|
||||
endpoint = 'posts';
|
||||
}
|
||||
const profileTitle = type === 'tagged' ? `${profile.name} (@${id}) tagged posts - Picnob` : `${profile.name} (@${id}) public posts - Picnob`;
|
||||
|
||||
const apiUrl = `${baseUrl}/api/${endpoint}`;
|
||||
|
||||
let responseData;
|
||||
try {
|
||||
if (profile.usePuppeteer) {
|
||||
responseData = await puppeteerGet(`${apiUrl}?userid=${profile.userId}`, browser);
|
||||
responseData = typeof responseData === 'string' ? JSON.parse(responseData) : responseData;
|
||||
} else {
|
||||
const data = await ofetch(apiUrl, {
|
||||
headers: {
|
||||
accept: 'application/json',
|
||||
},
|
||||
query: {
|
||||
userid: profile.userId,
|
||||
},
|
||||
});
|
||||
responseData = typeof data === 'string' ? JSON.parse(data) : data;
|
||||
}
|
||||
} catch {
|
||||
responseData = await puppeteerGet(`${apiUrl}?userid=${profile.userId}`, browser);
|
||||
responseData = typeof responseData === 'string' ? JSON.parse(responseData) : responseData;
|
||||
}
|
||||
|
||||
const posts = responseData?.posts;
|
||||
|
||||
if (!posts?.items?.length) {
|
||||
throw new Error('No posts found');
|
||||
}
|
||||
|
||||
if (type === 'tagged') {
|
||||
posts.items = posts.items.map((post, index) => {
|
||||
const taggedPost = responseData.tagged.items[index] || {};
|
||||
return { ...taggedPost, ...post };
|
||||
});
|
||||
}
|
||||
|
||||
const list = await Promise.all(
|
||||
posts.items.map(async (item) => {
|
||||
const { shortcode, sum, sum_pure, type, time } = item;
|
||||
|
||||
const link = `${baseUrl}/post/${shortcode}/`;
|
||||
if (type === 'img_multi') {
|
||||
item.images = await cache.tryGet(link, async () => {
|
||||
let html;
|
||||
if (profile.usePuppeteer) {
|
||||
html = await puppeteerGet(link, browser);
|
||||
} else {
|
||||
const data = await ofetch(link);
|
||||
html = data;
|
||||
for (const item of profile.items) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const newDescription = (await cache.tryGet(`picnob:user:${id}:${item.guid}`, async () => {
|
||||
try {
|
||||
let html;
|
||||
try {
|
||||
const data = await ofetch(item.link);
|
||||
html = data;
|
||||
} catch {
|
||||
html = await puppeteerGet(item.link, browser);
|
||||
}
|
||||
const $ = load(html);
|
||||
if ($('.video_img').length > 0) {
|
||||
return `<video src="${$('.video_img a').attr('href')}" poster="${$('.video_img img').attr('data-src')}"></video><br />${$('.sum_full').text()}`;
|
||||
} else {
|
||||
let description = '';
|
||||
for (const slide of $('.swiper-slide').toArray()) {
|
||||
const $slide = $(slide);
|
||||
description += `<img src="${$slide.find('.pic img').attr('data-src')}" /><br />`;
|
||||
}
|
||||
const $ = load(html);
|
||||
return [
|
||||
...new Set(
|
||||
$('.post_slide a')
|
||||
.toArray()
|
||||
.map((a: any) => {
|
||||
a = $(a);
|
||||
return {
|
||||
ori: a.attr('href'),
|
||||
url: a.find('img').attr('data-src'),
|
||||
isVideo: !!a.find('.icon_play').length,
|
||||
};
|
||||
})
|
||||
),
|
||||
];
|
||||
});
|
||||
description += $('.sum_full').text();
|
||||
return description;
|
||||
}
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
|
||||
return {
|
||||
title: sanitizeHtml(sum.split('\n')[0], { allowedTags: [], allowedAttributes: {} }) || sum_pure,
|
||||
description: art(path.join(__dirname, 'templates/desc.art'), {
|
||||
item: {
|
||||
...item,
|
||||
// Fix linebreaks
|
||||
sum: sum.replaceAll('\n', '<br>'),
|
||||
},
|
||||
}),
|
||||
link,
|
||||
guid: shortcode,
|
||||
pubDate: parseDate(time, 'X'),
|
||||
};
|
||||
})
|
||||
);
|
||||
})) as string;
|
||||
if (newDescription) {
|
||||
item.description = newDescription;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
title: profileTitle,
|
||||
description: profile.description,
|
||||
link: profileUrl,
|
||||
image: profile.image,
|
||||
item: list,
|
||||
item: profile.items,
|
||||
};
|
||||
} catch (error) {
|
||||
await browser.close();
|
||||
|
||||
Reference in New Issue
Block a user