-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): add 木木博客频道 & 标签 & 专题 & 搜索 (#13170)
* feature(route): add 木木博客频道 & 标签 & 专题 & 搜索 * fix typo
- Loading branch information
Showing
8 changed files
with
231 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const got = require('@/utils/got'); | ||
const cheerio = require('cheerio'); | ||
const { parseDate } = require('@/utils/parse-date'); | ||
|
||
module.exports = async (ctx) => { | ||
const { params } = ctx.params; | ||
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 20; | ||
|
||
const rootUrl = 'https://www.liulinblog.com'; | ||
const currentUrl = params ? new URL(params, rootUrl).href : rootUrl; | ||
|
||
const { data: response } = await got(currentUrl); | ||
|
||
const $ = cheerio.load(response); | ||
|
||
let items = $('div.scroll') | ||
.first() | ||
.find('article') | ||
.slice(0, limit) | ||
.toArray() | ||
.map((item) => { | ||
item = $(item); | ||
|
||
const a = item.find('h2.entry-title a'); | ||
|
||
return { | ||
title: a.prop('title'), | ||
link: a.prop('href'), | ||
description: item.find('div.entry-excerpt').html(), | ||
author: item | ||
.find('span.meta-author a') | ||
.toArray() | ||
.map((a) => $(a).prop('title')) | ||
.join(' / '), | ||
category: item | ||
.find('span.meta-category-dot a[rel="category"]') | ||
.toArray() | ||
.map((c) => $(c).text()), | ||
guid: `liulinblog-${item.prop('id')}`, | ||
pubDate: parseDate(item.find('span.meta-date time').prop('datetime')), | ||
comments: item.find('span.meta-comment').text() ? parseInt(item.find('span.meta-comment').text().trim(), 10) : 0, | ||
}; | ||
}); | ||
|
||
items = await Promise.all( | ||
items.map((item) => | ||
ctx.cache.tryGet(item.link, async () => { | ||
const { data: detailResponse } = await got(item.link); | ||
|
||
const content = cheerio.load(detailResponse); | ||
|
||
content('div[role="alert"]').remove(); | ||
|
||
item.title = content('meta[property="og:title"]').prop('content'); | ||
item.description = content('div.entry-content').html(); | ||
item.author = content('div.entry-meta') | ||
.first() | ||
.find('span.meta-author a') | ||
.toArray() | ||
.map((a) => content(a).prop('title')) | ||
.join(' / '); | ||
item.category = content('div.entry-meta') | ||
.first() | ||
.find('span.meta-category a[rel="category"]') | ||
.toArray() | ||
.map((c) => content(c).text()); | ||
item.guid = `liulinblog-${content('article').first().prop('id')}`; | ||
item.pubDate = parseDate(content('span.meta-date time').first().prop('datetime')); | ||
item.comments = content('h3.comments-title').text() | ||
? parseInt( | ||
content('h3.comments-title') | ||
.text() | ||
.match(/\((\d+)\)/), | ||
10 | ||
) | ||
: 0; | ||
|
||
return item; | ||
}) | ||
) | ||
); | ||
|
||
const icon = $('link[rel="icon"]').prop('href'); | ||
const title = $('img.logo').prop('alt'); | ||
|
||
ctx.state.data = { | ||
item: items, | ||
title: `${title} - ${params ? $('h1.term-title').text().split('搜索到')[0] : '最新'}`, | ||
link: currentUrl, | ||
description: $('meta[name="description"]').prop('content'), | ||
language: 'zh-cn', | ||
image: $('img.logo').prop('src'), | ||
icon, | ||
logo: icon, | ||
subtitle: $('p.term-description').text(), | ||
author: title, | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
module.exports = { | ||
'/kuaixun': ['Fatpandac'], | ||
'/itnews/:channel?': ['Fatpandac'], | ||
'/itnews/:channel?': ['Fatpandac', 'nczitzk'], | ||
'/kuaixun': ['Fatpandac', 'nczitzk'], | ||
'/search/:keyword': ['nczitzk'], | ||
'/series/:id': ['nczitzk'], | ||
'/tag/:id': ['nczitzk'], | ||
'/:channel?': ['nczitzk'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
module.exports = function (router) { | ||
router.get('/kuaixun', require('./kuaixun')); | ||
router.get('/itnews/:channel?', require('./itnews')); | ||
module.exports = (router) => { | ||
router.get('/itnews/:channel', (ctx) => { | ||
const { channel } = ctx.params; | ||
const redirectTo = `/liulinblog/${channel}`; | ||
ctx.redirect(redirectTo); | ||
}); | ||
router.get('/:params*', require('./')); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters