Created
July 8, 2020 10:18
-
-
Save grimpy/0472e584be453c023248f25b5cef2507 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import link_header | |
import argparse | |
def main(user, token, repofilter): | |
session = requests.Session() | |
session.auth = (user, token) | |
def get_next(resp): | |
for link in link_header.parse(resp.headers['Link']).links: | |
if link.rel == "next": | |
return link.href | |
def process_response(resp): | |
for subscription in resp.json(): | |
full = subscription["full_name"] | |
if not repofilter or full.startswith(repofilter): | |
url = subscription['subscription_url'] | |
print(f"Unsubscribing from {full}") | |
unsubresp = session.delete(url) | |
unsubresp.raise_for_status() | |
else: | |
print(f"Keeping subscription for {full}") | |
return get_next(resp) | |
resp = session.get("https://api.github.com/user/subscriptions") | |
resp.raise_for_status() | |
while True: | |
nexturl = process_response(resp) | |
if nexturl: | |
resp = session.get(nexturl) | |
resp.raise_for_status() | |
else: | |
break | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--username") | |
parser.add_argument("--token") | |
parser.add_argument("--filter") | |
options = parser.parse_args() | |
main(options.username, options.token, options.filter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment