Created
September 30, 2018 19:18
-
-
Save TylerHendrickson/b476194fa93af0ead36783c09c035fa6 to your computer and use it in GitHub Desktop.
Bulk-unwatch GitHub repositories using regular expressions
This file contains hidden or 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 re | |
| import sys | |
| try: | |
| from github import Github | |
| except ImportError: | |
| print('You must install PyGithub in order to use this script!') | |
| print('Example: `pip install pygithub`') | |
| print('More info: https://github.com/PyGithub/PyGithub') | |
| sys.exit(1) | |
| def get_user(): | |
| token = input('Enter token: ') | |
| github = Github(token) | |
| user = github.get_user() | |
| return user | |
| def get_match_expression(): | |
| expression = input('Enter regular expression: ') | |
| regex = re.compile(expression) | |
| return regex | |
| def get_matching_watched_repos(user, regex): | |
| matches = [repo for repo in user.get_watched() if regex.match(repo.full_name)] | |
| if not matches: | |
| print('No repositories matched!') | |
| sys.exit(0) | |
| return matches | |
| def unsubscribe(user, repositories): | |
| print(f'Matched the following {len(repositories)} repositories:') | |
| for repo in repositories: | |
| print(f'\t- {repo.full_name}') | |
| continue_unwatching = input('Unsubscribe from matched repos? [y/n]: ') == 'y' | |
| if not continue_unwatching: | |
| print('Exiting without unsubscribing') | |
| sys.exit(0) | |
| for repo in repositories: | |
| user.remove_from_watched(repo) | |
| def main(): | |
| user = get_user() | |
| regex = get_match_expression() | |
| repositories = get_matching_watched_repos(user, regex) | |
| unsubscribe(user, repositories) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment