mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-02 18:18:06 +08:00
fix(route): fix GitHub route parameter conflict at trending.js and refactor to V2 (#8923)
* Fix(route): fix route parameter conflict at trending.js and change to V2 * Fix(route): modify the judgment condition of language parameter * Fix(docs): Update docs/programming.md Co-authored-by: Tony <TonyRL@users.noreply.github.com> * Fix(docs): Update docs/en/programming.md Co-authored-by: Tony <TonyRL@users.noreply.github.com> * fix(docs/route): add author and sort routes * Fix(route): Update docs/en/programming.md Co-authored-by: Tony <TonyRL@users.noreply.github.com> * Fix(route): sort routes Co-authored-by: Tony <TonyRL@users.noreply.github.com>
This commit is contained in:
96
lib/v2/github/contributors.js
Normal file
96
lib/v2/github/contributors.js
Normal file
@@ -0,0 +1,96 @@
|
||||
const got = require('@/utils/got');
|
||||
const config = require('@/config').value;
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
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,
|
||||
headers,
|
||||
});
|
||||
let data = response.data;
|
||||
|
||||
try {
|
||||
// Get total page number
|
||||
const last_page_link = response.headers.link.split(',').find((elem) => elem.includes('"last"'));
|
||||
const url_base = last_page_link.match(/<(.*)page=\d*/)[1];
|
||||
const page_count = Number(last_page_link.match(/page=(\d*)/)[1]);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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((item) => {
|
||||
const time = new Date();
|
||||
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>`;
|
||||
link = '';
|
||||
guid = `anon-${item.name}`;
|
||||
} else {
|
||||
title = `Contributor: ${item.login}`;
|
||||
description = `<img src="${item.avatar_url}"></img><p><a href="${item.html_url}">${item.login}</a></p><p>Contributions: ${item.contributions}</p>`;
|
||||
link = item.html_url;
|
||||
guid = item.id;
|
||||
}
|
||||
|
||||
items.push({
|
||||
title,
|
||||
description,
|
||||
guid,
|
||||
link,
|
||||
pubDate: time,
|
||||
});
|
||||
}),
|
||||
(ctx.state.data = {
|
||||
title: `${user}/${repo} Contributors`,
|
||||
link: `${host}/graphs/contributors`,
|
||||
description: `New contributors for ${user}/${repo}`,
|
||||
item: items,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user