我正在尝试从 github 为任何用户提取以下信息。
github-api中是否有公开的方式/api ,我们可以直接获取这些信息?
2019 年的答案,使用GitHub API V4。
先去GitHub申请token:https ://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line 。第 7 步,范围仅选择read:user
卷曲
curl -H "Authorization: bearer token" -X POST -d '{"query":"query {\n user(login: \"MeiK2333\") {\n name\n contributionsCollection {\n contributionCalendar {\n colors\n totalContributions\n weeks {\n contributionDays {\n color\n contributionCount\n date\n weekday\n }\n firstDay\n }\n }\n }\n }\n}"}' https://api.github.com/graphql
JavaScript
async function getContributions(token, username) {
const headers = {
'Authorization': `bearer ${token}`,
}
const body = {
"query": `query {
user(login: "${username}") {
name
contributionsCollection {
contributionCalendar {
colors
totalContributions
weeks {
contributionDays {
color
contributionCount
date
weekday
}
firstDay
}
}
}
}
}`
}
const response = await fetch('https://api.github.com/graphql', { method: 'POST', body: JSON.stringify(body), headers: headers })
const data = await response.json()
return data
}
const data = await getContributions('token', 'MeiK2333')
console.log(data)
是的,您可以使用新的 graphql API 轻松做到这一点
查看资源管理器:https ://developer.github.com/v4/explorer/
在那里您可以看到作为用户边缘的贡献集合。您可以获得重建日历所需的所有信息。
我已经包含了一个完整的示例,资源管理器文档可以进一步指导您。
专门回答您的问题,这query.user.contributionsCollection.contributionsCalendar.totalContributions
就是您要寻找的
继续将以下内容复制/粘贴到资源管理器中,您将看到我去年的贡献历史
query {
user(login: "qhenkart") {
email
createdAt
contributionsCollection(from: "2019-09-28T23:05:23Z", to: "2020-09-28T23:05:23Z") {
contributionCalendar {
totalContributions
weeks {
contributionDays {
weekday
date
contributionCount
color
}
}
months {
name
year
firstDay
totalWeeks
}
}
}
}
}
You can use the github events api for that:
const got = require('got')
async function getEvents(username) {
const events = []
let page = 1
do {
const url = `https://api.github.com/users/${username}/events?page=${page}`
var { body } = await got(url, {
json: true
})
page++
events.push(...body)
} while(!body.length)
return events
}
(async () => {
const events = await getEvents('handtrix')
console.log('Overall Events', events.length)
console.log('PullRequests', events.filter(event => event.type === 'PullRequestEvent').length)
console.log('Forks', events.filter(event => event.type === 'ForkEvent').length)
console.log('Issues', events.filter(event => event.type === 'IssuesEvent').length)
console.log('Reviews', events.filter(event => event.type === 'PullRequestReviewEvent').length)
})()
async function getEvents(username) {
const events = []
let page = 1
do {
const url = `https://api.github.com/users/${username}/events?page=${page}`
var body = await fetch(url).then(res => res.json())
page++
events.push(...body)
} while(!body.length)
return events
}
(async () => {
const events = await getEvents('handtrix')
console.log('Overall Events', events.length)
console.log('PullRequests', events.filter(event => event.type === 'PullRequestEvent').length)
console.log('Forks', events.filter(event => event.type === 'ForkEvent').length)
console.log('Issues', events.filter(event => event.type === 'IssuesEvent').length)
console.log('Reviews', events.filter(event => event.type === 'PullRequestReviewEvent').length)
})()
https://github.com/users/<USER>/contributions
您可以使用to
URL 参数获取 svg 日历,例如:
https://github.com/users/bertrandmartel/contributions?to=2016-12-31
您可以使用基本的 xml 解析器来汇总来自 svg 的所有贡献。
2016 年curl和xmlstarlet的示例:
curl -s "https://github.com/users/bertrandmartel/contributions?to=2016-12-31" | \
xmlstarlet sel -t -v "sum(//svg/g/g/rect/@data-count)"
我相信您可以在 Code Climate 的 Velocity git 分析中看到某个时间范围内的贡献计数以及其他个人贡献分析,您可以在此处请求访问:https ://go.codeclimate.com/velocity-free-for-teams
您可以使用此函数提取去年(客户)的贡献:
function getContributions(){
const svgGraph = document.getElementsByClassName('js-calendar-graph')[0];
const daysRects = svgGraph.getElementsByClassName('day');
const days = [];
for (let d of daysRects){
days.push({
date: d.getAttribute('data-date'),
count: d.getAttribute('data-count')
});
}
return days;
}
我还编写了一个小节点模块,可以“提取”贡献
@simonwep/github-contributions
也许这会对你有所帮助(即使我迟到了 4 年)
如果你更喜欢一个npm
包,你可以试试这个。
https://github.com/SammyRobensParadise/github-contributions-counter#readme
您可以获得所有时间的捐款或每年的捐款。