我可以通过以下方式获取仓库的点赞数。
curl https://api.github.com/repos/mottosso/be/stargazers
但是我如何获取一个 Gist 的赞星数呢?
例如:
curl https://api.github.com/gists/mottosso/9b25f547d5364f050494/stargazers
我可以通过以下方式获取仓库的点赞数。
curl https://api.github.com/repos/mottosso/be/stargazers
但是我如何获取一个 Gist 的赞星数呢?
例如:
curl https://api.github.com/gists/mottosso/9b25f547d5364f050494/stargazers
这似乎在gist上不是直接可用的。
您可以检查一个gist是否被标星, 但是像richardcornish/file-git-notes这样的标星gist并不在其描述中包含星号。
请参见https://api.github.com/gists/4676556
这将使您需要从https://gist.github.com/richardcornish/4676556/stars页面中获取“36”(该gist的总星数)。
而且,该计数器的xpath并不明显。
//div[@id='js-flash-container']/
div[@class='container']/
div[@class='gist js-gist-container gist-with-sidebar with-full-navigation']/
div[@class='gist-sidebar clearfix']/
div[@class='sunken-menu vertical-right repo-nav js-repo-nav js-repository-container-pjax js-octicon-loaders']/
div[@class='sunken-menu-contents']/
ul[@class='sunken-menu-group']/
li[@class='tooltipped tooltipped-w'][3]/
a[@class='sunken-menu-item selected']/
span[@class='counter']
最终,我找到了一种方法,可以在不必爬取HTML的情况下从GitHub API中获取Gist中“星标者”(即收藏者)的数量。希望这对某人有用。
使用GraphQL API而不是RESTful API
gh
(GitHub-CLI)命令。#!/bin/bash
QUERY='
query {
viewer {
gist (name: "5b10b34f87955dfc86d310cd623a61d1" ) {
stargazerCount
}
}
}
'
TEMPLATE='{{.data.viewer.gist.stargazerCount}}'
# Login(`gh auth login`) is required
gh api graphql -f query="${QUERY}" --template="${TEMPLATE}"
# Output:
# 2
#!/bin/bash
QUERY='
{
"query": "query {
viewer {
gist (name: \"5b10b34f87955dfc86d310cd623a61d1\" ) {
stargazerCount
}
}
}"
}
'
QUERY="$(echo $QUERY | jq -c -M .)"
curl -s -H "Authorization: bearer ${GITHUB_TOKEN}" -X POST -d "${QUERY}" https://api.github.com/graphql
# Output:
# {"data":{"viewer":{"gist":{"stargazerCount":2}}}}
#!/bin/bash
# shellcheck disable=SC2016
# --------------------------------------------------------------
# Example of using the `gh api graphql` command to retrieve and
# format information of gists
# --------------------------------------------------------------
# List number of stars from the first 10 gists
QUERY1='
query {
viewer {
gists (first: 10, orderBy: {field: CREATED_AT, direction: DESC} ) {
nodes {
createdAt
description
name
pushedAt
stargazers (first: 100) {
totalCount
edges {
node {
id
}
}
}
updatedAt
}
}
}
}
'
TEMPLATE1='
{{- range $repo := .data.viewer.gists.nodes -}}
{{- printf "name: %s - stargazers: %v\n" $repo.name $repo.stargazers.totalCount -}}
{{- end -}}
'
# List the number of stars of a particular gist
QUERY2='
query {
viewer {
gist (name: "5b10b34f87955dfc86d310cd623a61d1" ) {
name
stargazerCount
}
}
}
'
TEMPLATE2='
{{- printf "name: %s - stargazers: %v\n" .data.viewer.gist.name .data.viewer.gist.stargazerCount -}}
'
gh api graphql -f query="${QUERY1}" --paginate --template="${TEMPLATE1}"
echo "----------------------------------"
gh api graphql -f query="${QUERY2}" --paginate --template="${TEMPLATE2}"
# Output:
# name: 7101f542be23e5048198e2a27c3cfda8 - stargazers: 0
# name: d5b9800c636dd78defa4f15894d54d29 - stargazers: 0
# name: e915aa8c01dd438e3ffd79b05f15a4ff - stargazers: 0
# name: 83e82d243b9b3d9ffbf370010ad658c3 - stargazers: 0
# name: 9cbad9941ad797b8d75123e40f7fd4c8 - stargazers: 0
# name: 76857bc6339515d7144e00f17adb1090 - stargazers: 1
# name: 5fd419de98e58b650ad279d6c1266179 - stargazers: 1
# name: cb4dbde222fb494405bb522692235456 - stargazers: 1
# name: f15d018ceb6d2a2103c581b41530ad49 - stargazers: 0
# name: a821517a85992fe329489db2ce32bd56 - stargazers: 0
# ----------------------------------
# name: 5b10b34f87955dfc86d310cd623a61d1 - stargazers: 2
由于我对GraphQL还很陌生,所以新的问题是“如何定义特定的要点”...找到了!