Home > Software design >  Github API number of watch over time
Github API number of watch over time

Time:02-13

I am trying to fetch the number of watches over time using Python, I have tried "https://api.github.com/repos/octocat/hello-world/subscription" that is listed on the Github API webpage, but seems no longer work, I am a bit confused. I am just trying to get "created_at" under subscription. Any suggestions? Thank you.

CodePudding user response:

To get the number of watchers you can do this:

url = f"https://api.github.com/repos/{git_owner_repository}/{git_repository_name}"
response = requests.get(url, auth=(username, GITHUB_TOKEN))
response.raise_for_status()
response_dict = response.json()
watchers = response_dict['watchers_count']

subscriptions is per user.

CodePudding user response:

I would suggest using GitHub's GraphQL API. To do so you can use the following:

import requests

MY_TOKEN = my_token
REPO_NAME = repo_name
REPO_OWNER = repo_owner

query = f'''{{
              repository(name: "{REPO_NAME}", owner: "{REPO_OWNER}") {{
                watchers {{
                  totalCount
                }}
              }}
            }}
            '''

headers = {"Authorization": f"token {MY_TOKEN}"}

request = requests.post("https://api.github.com/graphql", json={"query": query}, headers=headers)

print(request.json())

Which will output a result like the following:

{'data': {'repository': {'watchers': {'totalCount': 1593}}}}

You can easily try out GitHub's GraphQL API in the explorer they provide (one click to run a query). Just use the following query as an example (and replace the repo name and owner as you wish):

{
  repository(name: "repo_name", owner: "repo_owner") {
    watchers {
      totalCount
    }
  }
}
  • Related