feat: load pics of searched items

This commit is contained in:
Troy Liu
2021-01-11 21:46:34 +08:00
parent 3e93529df1
commit 91c6e6dfc2
2 changed files with 39 additions and 19 deletions

View File

@@ -2,7 +2,7 @@ const { processFeed } = require('./utils');
module.exports = async (ctx) => {
const url = ctx.params.type ? `https://ja.porn-images-xxx.com/search/${ctx.params.type}/${encodeURIComponent(ctx.params.name)}/` : 'https://ja.porn-images-xxx.com/search/';
const items = await processFeed(url);
const items = await processFeed(ctx, url);
ctx.state.data = {
title: `${ctx.params.name || '新着画像一覧'} - エロ画像`,
link: url,

View File

@@ -1,26 +1,46 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
exports.processFeed = async function processFeed(link) {
const host = 'https://ja.porn-images-xxx.com/';
exports.processFeed = async function processFeed(ctx, link) {
const response = await got.get(link);
const $ = cheerio.load(response.body);
const list = $('ul#image-list li');
const list = $('ul#image-list li')
.map(function (index, item) {
item = $(item);
return {
title: item.find('.image-list-item-title').text(),
link: item.find('.image-list-item-title > a').attr('href'),
date: item.find('.image-list-item-regist-date > span').text(),
};
})
.get();
return await Promise.all(
list.map(async (info) => {
const title = info.title.trim();
const date = info.date;
const itemUrl = host + info.link;
return (
list &&
list
.map((index, item) => {
item = $(item);
return {
title: item.find('.image-list-item-title').text(),
link: item.find('.image-list-item-title > a').attr('href'),
description: item
.find('.image-list-item-image')
.html()
.replace(/\/p=\d+x\d+/, ''),
pubDate: new Date(item.find('.image-list-item-regist-date').text()),
};
})
.get()
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got.get(itemUrl);
const $ = cheerio.load(response.data);
const images = $('.icon-overlay > a > img');
const desc = cheerio.html(images);
const item = {
title: title,
link: itemUrl,
description: desc,
pubDate: new Date(date).toUTCString(),
};
if (desc) {
ctx.cache.set(itemUrl, JSON.stringify(item));
}
return Promise.resolve(item);
})
);
};