Github API number of watch over time

Viewed 87

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.

3 Answers

EDIT - as per your first comment and extra requirements: Unfortunately I do not think it is possible to get watchers over time, you can however get the stargazers over time, an example using GitHub's GraphQL API is below. 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
                }}
                stargazers(first: 100) {{
                  totalCount
                  edges {{
                    starredAt
                  }}
                }}
              }}
            }}
            '''

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": 1594
      },
      "stargazers": {
        "totalCount": 53952,
        "edges": [
          {
            "starredAt": "2016-08-15T18:39:55Z"
          },
          {
            "starredAt": "2016-08-15T19:14:32Z"
          }
        ]
      }
    }
  }
}

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
    }
    stargazers(first:100) {
      totalCount
      edges {
        starredAt
      }
    }
  }
}

Note you should now be careful for pagination (see first:... parameter).

I guess for privacy reasons the date when a user added a repository to watch is not public. The only way that I have found to get that date is using the user scope instead of repo scope, using the following call:

https://api.github.com/users/your_username/subscriptions

Remember you have to be authenticated using a private token

To get the number of watchers you can do this:

#get number of watchers
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']

#get all the watchers:
url = f"https://api.github.com/repos/octocat/hello-world/subscribers?simple=yes&per_page={watchers}"
response_watcher = requests.get(url, auth=(username, GITHUB_TOKEN))
response_watcher.raise_for_status()
response_watcher = response.json()
for watcher in response_watcher:
    user_url = watcher['url']
    watcher_name = watcher['login']
    response_user = requests.get(user_url, auth=(username, GITHUB_TOKEN))
    response_user.raise_for_status()
    response_dict_user = response.json()
    created_at= response_dict_user['created_at']
    print(f"watcher name: {watcher_name}, created_at: {created_at}")
Related