Skip to content

Instantly share code, notes, and snippets.

@jminuscula
Forked from jpstroop/unwatch_all.py
Last active March 11, 2021 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jminuscula/a39f2169a2dc8edfc672d6f68930f01b to your computer and use it in GitHub Desktop.
Save jminuscula/a39f2169a2dc8edfc672d6f68930f01b to your computer and use it in GitHub Desktop.
Unwatch all Github repositories.
#!/usr/bin/env python
#
# Unwatch from all Github repositories. Note that it will only work with up to
# 100 repos at a time (pagination is not implemented), so you may need to run
# more than once.
#
# Depends:
# requests : http://docs.python-requests.org/en/master/
#
# Output (to stdout):
# [
# {
# "status": 204,
# "repo": "https://api.github.com/repos/thatcher/openseadragon"
# },
# {
# "status": "SKIPPED - in whitelist",
# "repo": "https://api.github.com/repos/jpstroop/URIs-to-EAD"
# },
# {
# "status": 204,
# "repo": "https://api.github.com/repos/pulibrary/lapidus-project"
# },
# // ...
# ]
#
# See:
# * https://developer.github.com/v3/activity/watching/#list-repositories-being-watched
# * https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription
#
from requests import get
from requests import delete
from json import dumps
from os import environ
# Set these:
TOKEN = environ['GITHUB_TOKEN'] # notification permissions
HEADERS = {
'Accept' : 'application/vnd.github.v3+json'
'Authentication': f'Bearer {TOKEN}',
}
WHITELIST = (
'IIIF/api',
)
def list_watched(user, pw):
url = 'https://api.github.com/user/subscriptions'
auth = (user, pw)
params = { 'per_page' : 100 }
r = get(url, headers=HEADERS, params=params)
return map(lambda d: d['url'], r.json())
def unwatch(repo_url):
print(repo_url)
if any([repo_url.endswith(repo) for repo in WHITELIST]):
return { 'status' : 'SKIPPED - in whitelist', 'repo' : repo_url }
url = '{0}/subscription'.format(repo_url)
r = delete(url, headers=HEADERS)
return { 'status' : r.status_code, 'repo' : repo_url }
def unwatch_all():
result = [ unwatch(repo) for repo in list_watched() ]
return dumps(result, indent=4)
print(unwatch_all())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment