forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Henry Wang <[email protected]>
- Loading branch information
1 parent
5f299e9
commit 905b54f
Showing
4 changed files
with
186 additions
and
1 deletion.
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
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
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,130 @@ | ||
const got = require('@/utils/got'); | ||
const cheerio = require('cheerio'); | ||
const url = require('url'); | ||
const iconv = require('iconv-lite'); | ||
|
||
module.exports = async (ctx) => { | ||
const category = ctx.params.category; | ||
const lang = ctx.params.language; | ||
|
||
let index_url; | ||
let character_set; | ||
switch (lang) { | ||
case 'zh_tw': | ||
index_url = 'http://www.soomal.com/doc/index201000_0001_00.htm'; | ||
character_set = 'big5'; | ||
break; | ||
case 'en': | ||
index_url = 'http://eng.soomal.com/edoc/index101000_0001_00.htm'; | ||
character_set = 'ISO-8859-5'; | ||
break; | ||
default: | ||
index_url = 'http://www.soomal.com/doc/index101000_0001_00.htm'; | ||
character_set = 'gbk'; | ||
break; | ||
} | ||
|
||
let category_list; | ||
let url_list; | ||
|
||
const category_url = await ctx.cache.tryGet([lang, category], async () => { | ||
const front_page = await got.get(index_url, { | ||
responseType: 'buffer', | ||
}); | ||
|
||
const front_data = iconv.decode(front_page.data, character_set); | ||
|
||
const $ = cheerio.load(front_data); | ||
if (lang !== 'en') { | ||
const container_list = $('div.container.cf').slice(0, 2); | ||
category_list = container_list | ||
.find('ul.ul-reset') | ||
.find('h3') | ||
.children() | ||
.get() | ||
.map((item) => $(item).text()); | ||
url_list = container_list | ||
.find('ul.ul-reset') | ||
.find('h3') | ||
.children() | ||
.get() | ||
.map((item) => $(item).attr('href')); | ||
} else { | ||
category_list = $('div.menu') | ||
.find('a') | ||
.get() | ||
.map((item) => $(item).text()); | ||
url_list = $('div.menu') | ||
.find('a') | ||
.get() | ||
.map((item) => $(item).attr('href')); | ||
} | ||
|
||
return url.resolve(index_url, url_list[category_list.indexOf(category)]); | ||
}); | ||
|
||
const response = await got.get(category_url, { | ||
responseType: 'buffer', | ||
}); | ||
|
||
const data = iconv.decode(response.data, character_set); | ||
const $ = cheerio.load(data); | ||
|
||
const list = $('div.item') | ||
.find('div.title') | ||
.children('a') | ||
.get() | ||
.map((item) => url.resolve(category_url, $(item).attr('href'))); | ||
|
||
const out = await Promise.all( | ||
list.map(async (link) => { | ||
const [title, author, pubDate, description] = await ctx.cache.tryGet(link, async () => { | ||
const response = await got.get(link, { | ||
responseType: 'buffer', | ||
}); | ||
|
||
const data = iconv.decode(response.data, character_set); | ||
const $ = cheerio.load(data); | ||
|
||
let [title, author, pubDate, description] = ''; | ||
if (lang !== 'en') { | ||
title = $('div.titlebox').find('div.title').text(); | ||
author = $('div.titlebox').find('div.info').children('a').text(); | ||
pubDate = Date.parse( | ||
$('div.titlebox') | ||
.find('div.info') | ||
.text() | ||
.match(/\d{4}([.\-/ ])\d{2}\1\d{2}\s\d{2}[:]\d{2}[:]\d{2}/)[0] | ||
); | ||
|
||
$('div.toolbar,div.grade').remove(); | ||
description = $('div.Doc').html(); | ||
} else { | ||
title = $('div.titlebox').find('div.title').text(); | ||
author = $('div.titlebox').find('div.infobox').children('a').text(); | ||
pubDate = new Date(); | ||
|
||
$('div.grade').remove(); | ||
description = $('div.textbox').html(); | ||
} | ||
|
||
return [title, author, pubDate, description]; | ||
}); | ||
|
||
const item = { | ||
title: title, | ||
description: description, | ||
pubDate: pubDate, | ||
link: link, | ||
author: author, | ||
}; | ||
return Promise.resolve(item); | ||
}) | ||
); | ||
|
||
ctx.state.data = { | ||
title: 'Soomal.com - ' + category, | ||
link: category_url, | ||
item: out, | ||
}; | ||
}; |