feat(route): add GitHub Pulse (#13247)

* feat(route): add GitHub Pulse

* chore: apply suggested changes

---------

Co-authored-by: James Chen-Smith <james@chen-smith.net>
This commit is contained in:
James Chen-Smith
2023-09-12 00:28:55 +08:00
committed by GitHub
co-authored by James Chen-Smith
parent e8523c0768
commit cc34dfb9fb
6 changed files with 123 additions and 0 deletions
+1
View File
@@ -7,6 +7,7 @@ module.exports = {
'/issue/:user/:repo/:state?/:labels?': ['HenryQW', 'AndreyMZ'],
'/notifications': ['zhzy0077'],
'/pull/:user/:repo/:state?': ['hashman', 'TonyRL'],
'/pulse/:user/:repo/:period?': ['jameschensmith'],
'/repos/:user': ['DIYgod'],
'/search/:query/:sort?/:order?': ['LogicJake'],
'/starred_repos/:user': ['LanceZhu'],
+87
View File
@@ -0,0 +1,87 @@
const cheerio = require('cheerio');
const path = require('path');
const got = require('@/utils/got');
const md5 = require('@/utils/md5');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
module.exports = async (ctx) => {
const { user, repo, period } = ctx.params;
const periods = ['daily', 'halfweekly', 'weekly', 'monthly'];
const pulsePeriod = periods.includes(period) ? period : periods[2];
const link = `https://github.com/${user}/${repo}/pulse/${pulsePeriod}`;
const { data: pulsePage } = await got(link);
const $ = cheerio.load(pulsePage);
const $mainSections = $('main .Layout-main').children();
const $subheading = $mainSections.eq(0);
const [periodFrom, periodTo] = $subheading.find('h2').text().split('');
const $overview = $mainSections.eq(1);
const overviewItems = $overview
.find('ul ul li')
.map((_, el) => $(el).text())
.toArray();
const $commitActivity = $mainSections.eq(2);
let commitActivity;
const $contributionData = $commitActivity.find('.js-pulse-contribution-data');
if ($contributionData.length) {
const summaryUrl = $contributionData.attr('data-pulse-diffstat-summary-url');
commitActivity = (await got(`https://github.com${summaryUrl}`)).data;
} else {
commitActivity = $commitActivity.text();
}
const $githubActivity = $mainSections.eq(3);
let githubActivity;
const $sections = $githubActivity.find('h3');
if ($sections.length) {
githubActivity = $sections
.map((_, section) => {
const $section = $(section);
const $sectionSiblings = $section.nextUntil('h3');
const $paragraph = $section.nextUntil('ul');
const $list = $sectionSiblings.last();
return {
heading: $section.text(),
paragraph: $paragraph.length > 0 ? $paragraph.text() : undefined,
items: $list
.children()
.map((_, item) => {
const $item = $(item);
const $link = $item.find('a');
const $details = $item.find('p');
const $relativeTime = $details.find('relative-time');
$relativeTime.replaceWith($relativeTime.attr('datetime'));
return {
link: { text: $link.text(), url: $link.attr('href') },
details: $details.text(),
};
})
.toArray(),
};
})
.toArray();
}
ctx.state.data = {
title: `${user}/${repo} ${pulsePeriod} Pulse`,
link,
item: [
{
guid: md5(`${user}${repo}${period}${periodFrom}${periodTo}`),
title: `${periodFrom} - ${periodTo}`,
description: art(path.join(__dirname, 'templates/pulse-description.art'), {
overviewItems,
commitActivity,
githubActivity,
}),
pubDate: parseDate(periodTo),
},
],
};
};
+6
View File
@@ -44,6 +44,12 @@ module.exports = {
source: ['/:user/:repo/pulls', '/:user/:repo/pulls/:id', '/:user/:repo'],
target: '/github/pull/:user/:repo',
},
{
title: 'Pulse',
docs: 'https://docs.rsshub.app/routes/programming#github',
source: ['/:user/:repo/pulse', '/:user/:repo/pulse/:period'],
target: '/github/pulse/:user/:repo/:period?',
},
{
title: '用户仓库',
docs: 'https://docs.rsshub.app/routes/programming#github',
+1
View File
@@ -8,6 +8,7 @@ module.exports = function (router) {
router.get('/issue/:user/:repo/:state?/:labels?', require('./issue'));
router.get('/notifications', require('./notifications'));
router.get('/pull/:user/:repo/:state?/:labels?', require('./pulls'));
router.get('/pulse/:user/:repo/:period?', require('./pulse'));
router.get('/repos/:user', require('./repos'));
router.get('/search/:query/:sort?/:order?', require('./search'));
router.get('/starred_repos/:user', require('./starred_repos'));
@@ -0,0 +1,24 @@
<h2>Overview</h2>
<ul>
{{each overviewItems item}}
<li>{{item}}</li>
{{/each}}
</ul>
{{@ commitActivity}}
{{each githubActivity activity}}
<h2>{{activity.heading}}</h2>
{{if activity.paragraph}}
<p>{{activity.paragraph}}</p>
{{/if}}
<ul>
{{each activity.items item}}
<li>
<a href="{{item.link.url}}">{{item.link.text}}</a>
<p>{{item.details}}</p>
</li>
{{/each}}
</ul>
{{/each}}
+4
View File
@@ -316,6 +316,10 @@ For instance, the `/github/topics/framework/l=php&o=desc&s=stars` route will gen
<Route author="hashman TonyRL" example="/github/pull/DIYgod/RSSHub" path="/github/pull/:user/:repo/:state?/:labels?" paramsDesc={['User name', 'Repo name', 'the state of pull requests. Can be either `open`, `closed`, or `all`. Default: `open`.', 'a list of comma separated label names']} radar="1" rssbud="1"/>
### Repo Pulse {#github-repo-pulse}
<Route author="jameschensmith" example="/github/pulse/DIYgod/RSSHub" path="/github/pulse/:user/:repo/:period?" paramsDesc={['User name', 'Repo name', 'Time frame, selected from a repository\'s Pulse/Insights page. Possible values are: `daily`, `halfweekly`, `weekly`, or `monthly`. Default: `weekly`. If your RSS client supports it, consider aligning the polling frequency of the feed to the period.']} radar="1" rssbud="1"/>
### User Followers {#github-user-followers}
<Route author="HenryQW" path="/github/user/followers/:user" example="/github/user/followers/HenryQW" paramsDesc={['GitHub username']} radar="1" rssbud="1"/>