Use promise instead

This commit is contained in:
Linghao Zhang
2020-04-02 11:05:59 +08:00
parent c4a13afcab
commit 42bd6fa2cf
2 changed files with 61 additions and 49 deletions
+37 -37
View File
@@ -1,5 +1,5 @@
---
pageClass: routes
pageClass: RouteEns
---
# Programming
@@ -19,52 +19,52 @@ GitHub provides some official RSS feeds:
### User Repo
<Route author="dragon-yuan" path="/github/repos/:user" example="/github/repos/DIYgod" :paramsDesc="['GitHub username']" radar="1" />
<RouteEn author="dragon-yuan" path="/github/repos/:user" example="/github/repos/DIYgod" :paramsDesc="['GitHub username']" radar="1" />
### Trending
<Route path="/github/trending/:since/:language?" example="/github/trending/daily/javascript" :paramsDesc="['time frame, available in [Trending page](https://github.com/trending/javascript?since=monthly) \'s URL, possible values are: daily, weekly or monthly', 'the feed language, available in [Trending page](https://github.com/trending/javascript?since=monthly) \'s URL']" radar="1" />
<RouteEn path="/github/trending/:since/:language?" example="/github/trending/daily/javascript" :paramsDesc="['time frame, available in [Trending page](https://github.com/trending/javascript?since=monthly) \'s URL, possible values are: daily, weekly or monthly', 'the feed language, available in [Trending page](https://github.com/trending/javascript?since=monthly) \'s URL']" radar="1" />
### Repo Issues
<Route author="HenryQW AndreyMZ" path="/github/issue/:user/:repo/:state?/:labels?" example="/github/issue/DIYgod/RSSHub/all/bug" :paramsDesc="['GitHub username', 'GitHub repo name', 'the state of the issues. Can be either `open`, `closed`, or `all`. Default: `open`.', 'a list of comma separated label names']" radar="1" />
<RouteEn author="HenryQW AndreyMZ" path="/github/issue/:user/:repo/:state?/:labels?" example="/github/issue/DIYgod/RSSHub/all/bug" :paramsDesc="['GitHub username', 'GitHub repo name', 'the state of the issues. Can be either `open`, `closed`, or `all`. Default: `open`.', 'a list of comma separated label names']" radar="1" />
### Repo Pull Requests
<Route author="hashman" example="/github/pull/DIYgod/RSSHub" path="/github/pull/:user/:repo" :paramsDesc="['User name', 'Repo name']" radar="1"/>
<RouteEn author="hashman" example="/github/pull/DIYgod/RSSHub" path="/github/pull/:user/:repo" :paramsDesc="['User name', 'Repo name']" radar="1"/>
### User Followers
<Route author="HenryQW" path="/github/user/follower/:user" example="/github/user/followers/HenryQW" :paramsDesc="['GitHub username']" radar="1" />
<RouteEn author="HenryQW" path="/github/user/follower/:user" example="/github/user/followers/HenryQW" :paramsDesc="['GitHub username']" radar="1" />
### Repo Stars
<Route author="HenryQW" path="/github/stars/:user/:repo" example="/github/stars/DIYGod/RSSHub" :paramsDesc="['GitHub username', 'GitHub repo name']" radar="1" />
<RouteEn author="HenryQW" path="/github/stars/:user/:repo" example="/github/stars/DIYGod/RSSHub" :paramsDesc="['GitHub username', 'GitHub repo name']" radar="1" />
### Repo Branches
<Route author="max-arnold" example="/github/branches/DIYgod/RSSHub" path="/github/branches/:user/:repo" :paramsDesc="['User name', 'Repo name']" radar="1"/>
<RouteEn author="max-arnold" example="/github/branches/DIYgod/RSSHub" path="/github/branches/:user/:repo" :paramsDesc="['User name', 'Repo name']" radar="1"/>
### Files Commits
<Route author="zengxs" example="/github/file/DIYgod/RSSHub/master/lib/router.js" path="/github/file/:user/:repo/:branch/:filepath+" :paramsDesc="['User name', 'Repo name', 'Branch name', 'File path']" radar="1">
<RouteEn author="zengxs" example="/github/file/DIYgod/RSSHub/master/lib/RouteEnr.js" path="/github/file/:user/:repo/:branch/:filepath+" :paramsDesc="['User name', 'Repo name', 'Branch name', 'File path']" radar="1">
| User name | Repo name | Branch name | File path |
| --------- | --------- | ----------- | --------------- |
| `DIYgod` | `RSSHub` | `master` | `lib/router.js` |
| User name | Repo name | Branch name | File path |
| --------- | --------- | ----------- | ----------------- |
| `DIYgod` | `RSSHub` | `master` | `lib/RouteEnr.js` |
> - If there are special characters such as `/` in the **branch name**, they need to be encoded with urlencode, usually `/` needs to be replaced with `%2f`
> - If there are special characters in the **file path**, you need to use urlencode to encode them, but the file path can be recognized normally `/` characters
> - If the **file path** ends with `.rss`, `.atom`, `.json`, you need to replace the `.` in the suffix with `%2e`
> > Reeder will make an error when subscribing to `% 2erss` or similar suffixes. At this time, add`.rss` after the route to subscribe
> > Reeder will make an error when subscribing to `% 2erss` or similar suffixes. At this time, add`.rss` after the RouteEn to subscribe
> >
> > Such as: replace `https://rsshub.app/github/file/DIYgod/RSSHub/master/lib/router%2ejs` to `https://rsshub.app/github/file/DIYgod/RSSHub/master/lib/router%2ejs.rss`
> > Such as: replace `https://rsshub.app/github/file/DIYgod/RSSHub/master/lib/RouteEnr%2ejs` to `https://rsshub.app/github/file/DIYgod/RSSHub/master/lib/RouteEnr%2ejs.rss`
</Route>
</RouteEn>
### Search Result
<Route author="LogicJake" example="/github/search/RSSHub/bestmatch/desc" path="/github/search/:query/:sort?/:order?" :paramsDesc="['search keyword', 'Sort options (default to bestmatch)','Sort order, desc and asc (desc descending by default)']"/>
<RouteEn author="LogicJake" example="/github/search/RSSHub/bestmatch/desc" path="/github/search/:query/:sort?/:order?" :paramsDesc="['search keyword', 'Sort options (default to bestmatch)','Sort order, desc and asc (desc descending by default)']"/>
| Sort options | sort |
| ---------------- | --------- |
@@ -75,29 +75,29 @@ GitHub provides some official RSS feeds:
### User Starred Repositories
<Route author="LanceZhu" example="/github/starred_repos/DIYgod" path="/github/starred_repos/:user" :paramsDesc="['User name']" radar="1"/>
<RouteEn author="LanceZhu" example="/github/starred_repos/DIYgod" path="/github/starred_repos/:user" :paramsDesc="['User name']" radar="1"/>
### Repo Contributors
<Route author="zoenglinghou" example="/github/contributors/DIYgod/RSSHub" path="/github/contributors/:user/:repo/:order?/:anon?" :paramsDesc="['User name','Repo name','Sort order by commit numbers, desc and asc (descending by default)','Show anonymous users. Defaults to no, use any values for yes.']" radar="1"/>
<RouteEn author="zoenglinghou" example="/github/contributors/DIYgod/RSSHub" path="/github/contributors/:user/:repo/:order?/:anon?" :paramsDesc="['User name','Repo name','Sort order by commit numbers, desc and asc (descending by default)','Show anonymous users. Defaults to no, use any values for yes.']" radar="1"/>
## GitLab
### Explore
<Route author="imlonghao" example="/gitlab/explore/trending" path="/gitlab/explore/:type" :paramsDesc="['type']">
<RouteEn author="imlonghao" example="/gitlab/explore/trending" path="/gitlab/explore/:type" :paramsDesc="['type']">
| Trending | Most stars | All |
| -------- | ---------- | --- |
| trending | starred | all |
</Route>
</RouteEn>
## Hacker News
### Section
<Route author="cf020031308" example="/hackernews/best/comments" path="/hackernews/:section/:type?" :paramsDesc="['Section', 'Link type']">
<RouteEn author="cf020031308" example="/hackernews/best/comments" path="/hackernews/:section/:type?" :paramsDesc="['Section', 'Link type']">
Website: https://news.ycombinator.com/
@@ -118,58 +118,58 @@ Website: https://news.ycombinator.com/
| story | Deault, link to shared address |
| comments | Link to Hacker News address |
</Route>
</RouteEn>
## Hex-Rays
### Hex-Rays News
<Route author="hellodword" example="/hex-rays/news" path="/hex-rays/news">
</Route>
<RouteEn author="hellodword" example="/hex-rays/news" path="/hex-rays/news">
</RouteEn>
## Kaggle
### Discussion
<Route author="LogicJake" example="/kaggle/discussion/387811/active" path="/kaggle/discussion/:forumId/:sort?" :paramsDesc="['Forum ID, open web request, search forumId; fill in all to subscribe to the whole site discussion forum', 'See the table below for sorting methods, default to hot']">
<RouteEn author="LogicJake" example="/kaggle/discussion/387811/active" path="/kaggle/discussion/:forumId/:sort?" :paramsDesc="['Forum ID, open web request, search forumId; fill in all to subscribe to the whole site discussion forum', 'See the table below for sorting methods, default to hot']">
| hot | recent | new | top | active |
| ------- | --------------- | --------------- | ---------- | ------------- |
| Hotness | Recent Comments | Recently Posted | Most Votes | Most Comments |
</Route>
</RouteEn>
### Competitions
<Route author="LogicJake" example="/kaggle/competitions" path="/kaggle/competitions/:category?" :paramsDesc="['category, default to all']">
<RouteEn author="LogicJake" example="/kaggle/competitions" path="/kaggle/competitions/:category?" :paramsDesc="['category, default to all']">
| 空 | featured | research | recruitment | gettingStarted | masters | playground | analytics |
| -------------- | -------- | -------- | ----------- | --------------- | ------- | ---------- | --------- |
| All Categories | Featured | Research | Recruitment | Getting started | Masters | Playground | Analytics |
</Route>
</RouteEn>
## LeetCode
### Articles
<Route author="LogicJake" example="/leetcode/articles" path="/leetcode/articles"/>
<RouteEn author="LogicJake" example="/leetcode/articles" path="/leetcode/articles"/>
### Submission
<Route author="NathanDai" example="/leetcode/submission/us/nathandai" path="/leetcode/submission/:country/:user" :paramsDesc="['country, Chines(cn) and US(us)', 'Username, available at the URL of the LeetCode user homepage']"/>
<RouteEn author="NathanDai" example="/leetcode/submission/us/nathandai" path="/leetcode/submission/:country/:user" :paramsDesc="['country, Chines(cn) and US(us)', 'Username, available at the URL of the LeetCode user homepage']"/>
## Linux Patchwork
### Patch Comments
<Route author="ysc3839" example="/patchwork.kernel.org/comments/10723629" path="/patchwork.kernel.org/comments/:id" :paramsDesc="['Patch ID']"/>
<RouteEn author="ysc3839" example="/patchwork.kernel.org/comments/10723629" path="/patchwork.kernel.org/comments/:id" :paramsDesc="['Patch ID']"/>
## LWN.net
### Security alerts
<Route author="zengxs" example="/lwn/alerts/CentOS" path="/lwn/alerts/:distributor" :paramsDesc="['Distribution identification']">
<RouteEn author="zengxs" example="/lwn/alerts/CentOS" path="/lwn/alerts/:distributor" :paramsDesc="['Distribution identification']">
| Distribution | Identification |
| :--------------- | :----------------- |
@@ -187,28 +187,28 @@ Website: https://news.ycombinator.com/
| SUSE | `SUSE` |
| Ubuntu | `Ubuntu` |
</Route>
</RouteEn>
## project-zero issues
### issues
<Route author="hellodword" example="/project-zero-issues" path="/project-zero-issues" />
<RouteEn author="hellodword" example="/project-zero-issues" path="/project-zero-issues" />
## Scala
### Scala Blog
<Route author="fengkx" example="/scala/blog/posts" path="/scala/blog/:part?" :paramsDesc="['part parmater can be found in the url of blog, defaults to All']"/>
<RouteEn author="fengkx" example="/scala/blog/posts" path="/scala/blog/:part?" :paramsDesc="['part parmater can be found in the url of blog, defaults to All']"/>
## Visual Studio Code Marketplace
### Visual Studio Code Plugins Marketplace
<Route author="SeanChao" example="/vscode/marketplace" path="/vscode/marketplace/:category?" :paramsDesc="['Category']">
<RouteEn author="SeanChao" example="/vscode/marketplace" path="/vscode/marketplace/:category?" :paramsDesc="['Category']">
| Featured | Trending Weekly | Trending Monthly | Trending Daily | Most Popular | Recently Added |
| -------- | --------------- | ---------------- | -------------- | ------------ | -------------- |
| featured | trending | trending_m | trending_d | popular | new |
</Route>
</RouteEn>
+24 -12
View File
@@ -2,19 +2,18 @@ const got = require('@/utils/got');
const config = require('@/config').value;
module.exports = async (ctx) => {
const user = ctx.params.user;
const repo = ctx.params.repo;
const order = ctx.params.order;
const anon = ctx.params.anon;
const { user, repo, order, anon } = ctx.params;
const host = `https://github.com/${user}/${repo}`;
const url = `https://api.github.com/repos/${user}/${repo}/contributors?` + (anon ? 'anon=1' : '');
// Use token if available
const headers = {};
if (config.github && config.github.access_token) {
headers.Authorization = `token ${config.github.access_token}`;
}
// First page
const response = await got({
method: 'get',
url,
@@ -23,40 +22,53 @@ module.exports = async (ctx) => {
let data = response.data;
try {
// Get total page number
const last_page_link = response.headers.link.split(',').find(function(elem) {
return elem.includes('"last"');
});
const url_base = last_page_link.match(/<(.*)page=\d*/)[1];
const page_count = Number(last_page_link.match(/page=(\d*)/)[1]);
for (let page = 2; page <= page_count; page++) {
// eslint-disable-next-line no-await-in-loop
const generate_array = (n) => [...Array(n - 1)].map((_, index) => index + 2);
const page_array = generate_array(page_count);
// Get everypage
const tasks = page_array.map(async (page) => {
const response = await got({
method: 'get',
url: `${url_base}page=${page}`,
headers,
});
data = data.concat(response.data);
}
});
await Promise.all(tasks);
} catch (err) {
// If only one page
// Other errors
if (!(err instanceof TypeError)) {
throw err;
}
}
let title;
let description;
let link;
let guid;
// Sort by commits
data.sort((a, b) => a.contributions - b.contributions);
if (order !== 'asc') {
data.reverse();
}
let title, description, link, guid;
const items = [];
let index = 0;
// Generate RSS
data.forEach(function(item) {
const time = new Date();
time.setMinutes(time.getMinutes() + (order === 'asc' ? -data.length + index : -index));
time.setMinutes(time.getMinutes() - index);
index++;
// Distiguished between acounts
if (item.type === 'Anonymous') {
title = `Contributor: ${item.name}`;
description = `<p>Anonymous contributor</p><p>Name: ${item.name}</p><p>E-mail: ${item.email}</p><p>Contributions: ${item.contributions}</p>`;