Skip to content

Commit

Permalink
feat(route): add slowmist news (DIYgod#10044)
Browse files Browse the repository at this point in the history
* feat(route): add slowmist news

* feat(utils): remove useless ul for code section

* docs: add slowmist news route docs

* docs(quick-start): new router under /lib/v2

* test(utils): add test fixArticleContent for new wechat-mp code

* feat(router): update radar source of slowmist
  • Loading branch information
Atlasoin authored Jul 4, 2022
1 parent 8f8adfb commit bd80574
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 7 deletions.
6 changes: 3 additions & 3 deletions docs/joinus/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ sidebar: auto

### 添加脚本路由

[/lib/router.js](https://github.com/DIYgod/RSSHub/blob/master/lib/router.js) 里添加路由
[/lib/v2/routes/](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 中创建对应路由路径,并在 `/lib/v2/routes/:path/router.js` 中添加路由

### 编写脚本

[/lib/routes/](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 中的路由对应路径下创建新的 js 脚本:
[/lib/v2/routes/](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 中的路由对应路径下创建新的 js 脚本:

#### 获取源数据

Expand Down Expand Up @@ -493,7 +493,7 @@ ctx.state.data = {
### 编写规则
在 [/assets/radar-rules.js](https://github.com/DIYgod/RSSHub/blob/master/assets/radar-rules.js) 里添加规则
在 [/lib/v2/routes/](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 的对应路由下创建 `radar.js` 并添加规则
下面说明中会用到的简化的规则:
Expand Down
12 changes: 12 additions & 0 deletions docs/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -2666,6 +2666,18 @@ column 为 third 时可选的 category:

</Route>

## 慢雾科技

### 动态

<Route author="AtlasQuan" example="/slowmist/research" path="/slowmist/:type?" :paramsDesc="['分类,见下表,默认为公司新闻']">

| 公司新闻 | 漏洞披露 | 技术研究 |
| ---- | ---- | -------- |
| news | vul | research |

</Route>

## 梅花网

### 作品
Expand Down
10 changes: 8 additions & 2 deletions lib/utils/wechat-mp.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ const fixArticleContent = (html, skipImg = false) => {
}
});

// add breaks in code section
$('code').each((_, code) => {
$('<br>').insertAfter(code);
});

// clear line index tags in code section
$('.code-snippet__line-index').remove();

// fix single picture article
// example: https://mp.weixin.qq.com/s/4p5YmYuASiQSYFiy7KqydQ
$('script').each((_, script) => {
Expand Down Expand Up @@ -186,7 +194,6 @@ const fetchArticle = async (ctx, url, bypassHostCheck = false) => {
let summary = $('meta[name=description]').attr('content');
summary = summary !== title ? summary : '';
let description = fixArticleContent($('#js_content'));

// No article get or article is too short, try the original url
const originalUrl = detectOriginalArticleUrl($);
if (originalUrl) {
Expand All @@ -211,7 +218,6 @@ const fetchArticle = async (ctx, url, bypassHostCheck = false) => {

let mpName = $('.profile_nickname').first().text();
mpName = mpName && mpName.trim();

return { title, author, description, summary, pubDate, mpName, link: url };
});
};
Expand Down
13 changes: 13 additions & 0 deletions lib/v2/slowmist/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
'slowmist.com': {
_name: '慢雾科技 SLOWMIST',
'.': [
{
title: '动态',
docs: 'https://docs.rsshub.app/new-media.html#man-wu-ke-ji',
source: ['/zh/news.html'],
target: '/slowmist/:type?',
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/slowmist/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/:type?', require('./slowmist'));
};
39 changes: 39 additions & 0 deletions lib/v2/slowmist/slowmist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const baseUrl = 'https://www.slowmist.com';
const { finishArticleItem } = require('@/utils/wechat-mp');

module.exports = async (ctx) => {
let type = ctx.params.type;

let title = '慢雾科技 - ';
if (type === 'news') {
title += '公司新闻';
} else if (type === 'vul') {
title += '漏洞披露';
} else if (type === 'research') {
title += '技术研究';
} else {
type = 'news';
title += '公司新闻';
}

const url = `${baseUrl}/api/get_list?type=${type}`;

const response = await got(url);

let items = (response.data.data || []).map((item) => ({
title: item.title,
link: item.url,
description: item.desc,
pubDate: parseDate(item.date),
}));

items = await Promise.all(items.map((item) => finishArticleItem(ctx, item)));

ctx.state.data = {
title,
link: url,
item: items,
};
};
25 changes: 23 additions & 2 deletions test/utils/wechat-mp.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,28 @@ describe('wechat-mp', () => {
it('fixArticleContent', () => {
const divHeader = '<div class="rich_media_content " id="js_content">';
const divFooter = '</div>';

const codeSection =
'<section class="code-snippet__fix code-snippet__js">' +
'<ul class="code-snippet__line-index code-snippet__js">' +
'<li></li><li></li><li></li>' +
'</ul >' +
'<pre class="code-snippet__js">' +
'<code><span class="code-snippet_outer">Line1 {</span></code>' +
'<code><span class="code-snippet__keyword">Line2</span></code>' +
'<code><span class="code-snippet_outer">Line3 }</span></code>' +
'</pre></section>';
const expectedCodeSection =
'<p class="code-snippet__fix code-snippet__js">' +
'<pre class="code-snippet__js">' +
'<code><span class="code-snippet_outer">Line1 {</span></code>' +
'<br>' +
'<code><span class="code-snippet__keyword">Line2</span></code>' +
'<br>' +
'<code><span class="code-snippet_outer">Line3 }</span></code>' +
'<br>' +
'</pre></p>';
const htmlSection =
codeSection +
'<section>test</section>' +
'<section><p>test</p></section>' +
'<section><div>test</div></section>' +
Expand All @@ -34,7 +54,8 @@ describe('wechat-mp', () => {
'<p>test</p>' +
'<div><p>test</p></div>' +
'<script>const test = "test"</script>';
const expectedHtmlSection = '<p>test</p>' + '<div><p>test</p></div>' + '<div><div>test</div></div>' + '<div><div><p>test</p></div></div>' + '<div><div><p>test</p></div></div>' + '<p>test</p>' + '<div><p>test</p></div>';
const expectedHtmlSection =
expectedCodeSection + '<p>test</p>' + '<div><p>test</p></div>' + '<div><div>test</div></div>' + '<div><div><p>test</p></div></div>' + '<div><div><p>test</p></div></div>' + '<p>test</p>' + '<div><p>test</p></div>';
let $ = cheerio.load(divHeader + htmlSection + divFooter);
expect(fixArticleContent(htmlSection)).toBe(expectedHtmlSection);
expect(fixArticleContent($('div#js_content.rich_media_content'))).toBe(expectedHtmlSection);
Expand Down

0 comments on commit bd80574

Please sign in to comment.