From 42bd6fa2cfd1376d39b206b310efc56a3e234806 Mon Sep 17 00:00:00 2001 From: Linghao Zhang Date: Thu, 2 Apr 2020 11:05:59 +0800 Subject: [PATCH] Use promise instead --- docs/en/programming.md | 74 +++++++++++++++---------------- lib/routes/github/contributors.js | 36 ++++++++++----- 2 files changed, 61 insertions(+), 49 deletions(-) diff --git a/docs/en/programming.md b/docs/en/programming.md index 48bb5e65f..4436f7506 100644 --- a/docs/en/programming.md +++ b/docs/en/programming.md @@ -1,5 +1,5 @@ --- -pageClass: routes +pageClass: RouteEns --- # Programming @@ -19,52 +19,52 @@ GitHub provides some official RSS feeds: ### User Repo - + ### Trending - + ### Repo Issues - + ### Repo Pull Requests - + ### User Followers - + ### Repo Stars - + ### Repo Branches - + ### Files Commits - + -| 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` - + ### Search Result - + | Sort options | sort | | ---------------- | --------- | @@ -75,29 +75,29 @@ GitHub provides some official RSS feeds: ### User Starred Repositories - + ### Repo Contributors - + ## GitLab ### Explore - + | Trending | Most stars | All | | -------- | ---------- | --- | | trending | starred | all | - + ## Hacker News ### Section - + 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 | - + ## Hex-Rays ### Hex-Rays News - - + + ## Kaggle ### Discussion - + | hot | recent | new | top | active | | ------- | --------------- | --------------- | ---------- | ------------- | | Hotness | Recent Comments | Recently Posted | Most Votes | Most Comments | - + ### Competitions - + | çİş | featured | research | recruitment | gettingStarted | masters | playground | analytics | | -------------- | -------- | -------- | ----------- | --------------- | ------- | ---------- | --------- | | All Categories | Featured | Research | Recruitment | Getting started | Masters | Playground | Analytics | - + ## LeetCode ### Articles - + ### Submission - + ## Linux Patchwork ### Patch Comments - + ## LWN.net ### Security alerts - + | Distribution | Identification | | :--------------- | :----------------- | @@ -187,28 +187,28 @@ Website: https://news.ycombinator.com/ | SUSE | `SUSE` | | Ubuntu | `Ubuntu` | - + ## project-zero issues ### issues - + ## Scala ### Scala Blog - + ## Visual Studio Code Marketplace ### Visual Studio Code Plugins Marketplace - + | Featured | Trending Weekly | Trending Monthly | Trending Daily | Most Popular | Recently Added | | -------- | --------------- | ---------------- | -------------- | ------------ | -------------- | | featured | trending | trending_m | trending_d | popular | new | - + diff --git a/lib/routes/github/contributors.js b/lib/routes/github/contributors.js index 071b730ef..6d84bf7d2 100644 --- a/lib/routes/github/contributors.js +++ b/lib/routes/github/contributors.js @@ -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 = `

Anonymous contributor

Name: ${item.name}

E-mail: ${item.email}

Contributions: ${item.contributions}

`;