Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): npm #9437

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/en/program-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ Refer to [#minecraft](/en/game.html#minecraft)

见 [#nintendo](/game.html#nintendo)

## NPM

### Package

<RouteEn author="Fatpandac" example="/npm/package/rsshub" path="/npm/package/:name" :paramsDesc="['Package name']"/>

## Nvidia Web Driver

### Changelog
Expand Down
6 changes: 6 additions & 0 deletions docs/program-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ pageClass: routes

见 [#nintendo](/game.html#nintendo)

## NPM

### 包

<Route author="Fatpandac" example="/npm/package/rsshub" path="/npm/package/:name" :paramsDesc="['包名']"/>

## Nvidia Web Driver

### 更新日志
Expand Down
3 changes: 3 additions & 0 deletions lib/v2/npm/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/package/:name': ['Fatpandac'],
};
45 changes: 45 additions & 0 deletions lib/v2/npm/package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const got = require('@/utils/got');
const { art } = require('@/utils/render');
const path = require('path');

module.exports = async (ctx) => {
const name = ctx.params.name;
const packageDownloadLastMonthAPI = `https://api.npmjs.org/downloads/point/last-month/${name}`; // 按月统计
const packageDownloadLastWeekAPI = `https://api.npmjs.org/downloads/point/last-week/${name}`; // 按周统计
const packageDownloadLastDayAPI = `https://api.npmjs.org/downloads/point/last-day/${name}`; // 按天统计
const packageVersionAPI = `https://registry.npmjs.org/${name}`; // 包基本信息

const downloadCountLastMonthRes = await got(packageDownloadLastMonthAPI).json();
const downloadCountLastWeekRes = await got(packageDownloadLastWeekAPI).json();
const downloadCountLastDayRes = await got(packageDownloadLastDayAPI).json();
const packageVersionRes = await got(packageVersionAPI).json();

const packageVersion = packageVersionRes.time;
const packageVersionList = [];
for (const key in packageVersion) {
packageVersionList.push({
version: key,
time: packageVersion[key],
});
}
packageVersionList.reverse();

ctx.state.data = {
title: `${name} - npm`,
link: `https://www.npmjs.com/package/${name}`,
description: `${name} - npm`,
item: [
{
title: `${name} - npm`,
description: art(path.join(__dirname, 'templates/package.art'), {
packageDownloadCountLastMonth: downloadCountLastMonthRes.downloads,
packageDownloadCountLastWeek: downloadCountLastWeekRes.downloads,
packageDownloadCountLastDay: downloadCountLastDayRes.downloads,
packageVersion: packageVersionList,
}),
link: `https://www.npmjs.com/package/${name}`,
guid: `https://www.npmjs.com/package/${name}${packageVersion.modified}`,
},
],
};
};
13 changes: 13 additions & 0 deletions lib/v2/npm/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
'npmjs.com': {
_name: 'npm',
'.': [
{
title: '包',
docs: 'https://docs.rsshub.app/program-update.html#npm',
source: ['/package/:name'],
target: '/npm/package/:name',
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/npm/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/package/:name', require('./package'));
};
10 changes: 10 additions & 0 deletions lib/v2/npm/templates/package.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h3>Download</h3>
<p>Last Day: {{packageDownloadCountLastDay}}</p>
<p>Last week: {{packageDownloadCountLastWeek}}</p>
<p>Last month: {{packageDownloadCountLastMonth}}</p>
<hr/>
<h3>Version</h3>
{{ each packageVersion}}
<p>{{$value.version}}: {{$value.time}}</p>
{{/each}}