Marcus Ottosson
Marcus Ottosson

Reputation: 3301

Get GitHub Gist Stargazer Count

I can get the repository stargazer count with this.

curl https://api.github.com/repos/mottosso/be/stargazers

But how can I get the stargazers of a Gist?

For example

curl https://api.github.com/gists/mottosso/9b25f547d5364f050494/stargazers

Upvotes: 6

Views: 1170

Answers (2)

KEINOS
KEINOS

Reputation: 109

Finally, I found a way to get the number of stars, or "stargazers", in the gist from the GitHub API without having to scrape the HTML. Hope this will be useful to someone. 🤞

tl; dr

Use GraphQL API rather than RESTful API

  • Example using gh(GitHub-CLI) command.
#!/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
  • Example using cURL command.
#!/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}}}}

ts; dr

#!/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

Since I am new to GraphQL, the new question is "how to define a particular gist"... found!

Upvotes: 2

VonC
VonC

Reputation: 1327394

That doesn't seem to be directly available on a gist.
You can check if a gist is starred, but a starred gists like richardcornish/file-git-notes doesn't include stars in its description.

See https://api.github.com/gists/4676556

That would leave you with scrapping the https://gist.github.com/richardcornish/4676556/stars page in order to get the '36' (total number of stars for that gist).

And the xpath for that counter isn't an obvious one

//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']

Upvotes: 4

Related Questions