From eca9f0f7dfadcf20056ea83379dd4d29d7194eec Mon Sep 17 00:00:00 2001 From: Stephen Greene Date: Mon, 19 Feb 2018 14:19:23 -0800 Subject: [PATCH] Add option to fetch followers/following JSON data --- README.rst | 24 ++++++++++++++---------- bin/github-backup | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/README.rst b/README.rst index 69dd9a6..861e616 100644 --- a/README.rst +++ b/README.rst @@ -28,12 +28,12 @@ Usage CLI Usage is as follows:: github-backup [-h] [-u USERNAME] [-p PASSWORD] [-t TOKEN] - [-o OUTPUT_DIRECTORY] [-i] [--starred] [--watched] - [--all] [--issues] [--issue-comments] [--issue-events] - [--pulls] [--pull-comments] [--pull-commits] [--labels] - [--hooks] [--milestones] [--repositories] [--bare] [--lfs] - [--wikis] [--skip-existing] [--all-starred] - [--gists] [--starred-gists] + [-o OUTPUT_DIRECTORY] [-i] [--starred] [--all-starred] + [--watched] [--followers] [--following] [--all] + [--issues] [--issue-comments] [--issue-events] [--pulls] + [--pull-comments] [--pull-commits] [--labels] [--hooks] + [--milestones] [--repositories] [--bare] [--lfs] + [--wikis] [--gists] [--starred-gists] [--skip-existing] [-L [LANGUAGES [LANGUAGES ...]]] [-N NAME_REGEX] [-H GITHUB_HOST] [-O] [-R REPOSITORY] [-P] [-F] [--prefer-ssh] [-v] @@ -54,12 +54,16 @@ CLI Usage is as follows:: password for basic auth. If a username is given but not a password, the password will be prompted for. -t TOKEN, --token TOKEN - personal access or OAuth token + personal access or OAuth token, or path to token + (file://...) -o OUTPUT_DIRECTORY, --output-directory OUTPUT_DIRECTORY directory at which to backup the repositories -i, --incremental incremental backup --starred include JSON output of starred repositories in backup + --all-starred include starred repositories in backup --watched include watched repositories in backup + --followers include JSON output of followers in backup + --following include JSON output of following users in backup --all include everything in backup --issues include issues in backup --issue-comments include issue comments in backup @@ -73,12 +77,12 @@ CLI Usage is as follows:: --milestones include milestones in backup --repositories include repository clone in backup --bare clone bare repositories - --lfs clone LFS repositories (requires Git LFS to be installed, https://git-lfs.github.com) + --lfs clone LFS repositories (requires Git LFS to be + installed, https://git-lfs.github.com) --wikis include wiki clone in backup - --skip-existing skip project if a backup directory exists - --all-starred include starred repositories in backup --gists include gists in backup --starred-gists include starred gists in backup + --skip-existing skip project if a backup directory exists -L [LANGUAGES [LANGUAGES ...]], --languages [LANGUAGES [LANGUAGES ...]] only allow these languages -N NAME_REGEX, --name-regex NAME_REGEX diff --git a/bin/github-backup b/bin/github-backup index d3ca674..59c1968 100755 --- a/bin/github-backup +++ b/bin/github-backup @@ -168,6 +168,14 @@ def parse_args(): action='store_true', dest='include_watched', help='include watched repositories in backup') + parser.add_argument('--followers', + action='store_true', + dest='include_followers', + help='include JSON output of followers in backup') + parser.add_argument('--following', + action='store_true', + dest='include_following', + help='include JSON output of following users in backup') parser.add_argument('--all', action='store_true', dest='include_everything', @@ -886,25 +894,41 @@ def backup_account(args, output_directory): account_cwd = os.path.join(output_directory, 'account') if args.include_starred or args.include_everything: - output_file = '{0}/starred.json'.format(account_cwd) - template = "https://{0}/users/{1}/starred" - template = template.format(get_github_api_host(args), args.user) + output_file = "{0}/starred.json".format(account_cwd) + template = "https://{0}/users/{1}/starred".format(get_github_api_host(args), args.user) _backup_data(args, - 'starred repositories', + "starred repositories", template, output_file, account_cwd) if args.include_watched or args.include_everything: - output_file = '{0}/watched.json'.format(account_cwd) - template = "https://{0}/users/{1}/subscriptions" - template = template.format(get_github_api_host(args), args.user) + output_file = "{0}/watched.json".format(account_cwd) + template = "https://{0}/users/{1}/subscriptions".format(get_github_api_host(args), args.user) _backup_data(args, - 'watched repositories', + "watched repositories", template, output_file, account_cwd) + if args.include_followers or args.include_everything: + output_file = "{0}/followers.json".format(account_cwd) + template = "https://{0}/users/{1}/followers".format(get_github_api_host(args), args.user) + _backup_data(args, + "followers", + template, + output_file, + account_cwd) + + if args.include_following or args.include_everything: + output_file = "{0}/following.json".format(account_cwd) + template = "https://{0}/users/{1}/following".format(get_github_api_host(args), args.user) + _backup_data(args, + "following", + template, + output_file, + account_cwd) + def _backup_data(args, name, template, output_file, output_directory): skip_existing = args.skip_existing