Skip to content

Commit

Permalink
feat(route): npm (#9437)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatpandac authored Apr 1, 2022
1 parent 457e8f8 commit bb3924c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
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}}

0 comments on commit bb3924c

Please sign in to comment.