Skip to content

Instantly share code, notes, and snippets.

@jpstroop
Last active August 22, 2021 12:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpstroop/6ed0c98f6a960f69f8142b56064cdb84 to your computer and use it in GitHub Desktop.
Save jpstroop/6ed0c98f6a960f69f8142b56064cdb84 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:
USER = environ['GITHUB_USER']
PASSWORD = environ['GITHUB_PASSWORD']
#
WHITELIST = (
'IIIF/api',
)
ACCEPT = { 'accept' : 'application/vnd.github.v3+json' }
def list_watched(user, pw):
url = 'https://api.github.com/user/subscriptions'
auth = (user, pw)
params = { 'per_page' : 100 }
r = get(url, auth=auth, headers=ACCEPT, params=params)
return map(lambda d: d['url'], r.json())
def unwatch(user, pw, 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, auth=(user, pw), headers=ACCEPT)
return { 'status' : r.status_code, 'repo' : repo_url }
def unwatch_all(user, pw):
result = [ unwatch(user, pw, repo) for repo in list_watched(user, pw) ]
return dumps(result, indent=4)
print(unwatch_all(USER, PASSWORD))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment