From ea27e35b905e378127f276b93d1aed5a274c3732 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Tue, 17 Jan 2023 18:09:22 +0000 Subject: [PATCH] Add optional username and fullname extraction --- github-list-repos | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/github-list-repos b/github-list-repos index 5a272a5..484b513 100755 --- a/github-list-repos +++ b/github-list-repos @@ -1,7 +1,9 @@ #!/usr/bin/env python3 +import html import logging import re import requests +import shlex import sys import time @@ -12,11 +14,15 @@ MODES = (GIT_URLS_OPTION, GITGUD_COMPLETE_ITEMS_OPTION) mode = None +name = False users = sys.argv[1:] +if users and users[0] == '--name': + name = True + users = users[1:] if users and users[0] in MODES: mode = users[0] users = users[1:] -assert users and (mode is None or mode in MODES) and not users[0].startswith('--'), f'Usage: github-list-repos [{" | ".join(MODES)}] USER [USER...]' +assert users and (mode is None or mode in MODES) and not users[0].startswith('--'), f'Usage: github-list-repos [--name] [{" | ".join(MODES)}] USER [USER...]' def get(url): @@ -45,6 +51,16 @@ for user in users: r = get(f'https://github.com/{user}') if '
', r.text) + if not musername: + print('Error: could not find profile:username meta tag', file = sys.stderr) + sys.exit(1) + mfullname = re.search(r']*\s)?class="(?:[^"]*\s)?h2(?:\s[^"]*)?"(?:\s[^>]*)?>(.*?)', r.text, flags = re.DOTALL) + if not mfullname: + print('Error: could not find name h1', file = sys.stderr) + sys.exit(1) + print(f'{shlex.quote(html.unescape(musername.group(1).strip()))} {shlex.quote(html.unescape(mfullname.group(1).strip()))}') r = get(f'https://github.com/orgs/{user}/repositories') page = 1 while True: @@ -57,6 +73,16 @@ for user in users: r = get(f'https://github.com/orgs/{user}/repositories?page={page}') else: # User, ?tab=repositories + cursor pagination + if name: + musername = re.search(r']*\s)?class="(?:[^"]*\s)?vcard-username(?:\s[^"]*)?"(?:\s[^>]*)?>(.*?)', r.text, flags = re.DOTALL) + if not musername: + print('Error: could not find vcard-username span', file = sys.stderr) + sys.exit(1) + if (m := re.search(r']*\s)?class="(?:[^"]*\s)?vcard-fullname(?:\s[^"]*)?"(?:\s[^>]*)?>(.*?)', r.text, flags = re.DOTALL)): + fullname = html.unescape(m.group(1).strip()) + else: + fullname = '' + print(f'{shlex.quote(html.unescape(musername.group(1).strip()))} {shlex.quote(fullname)}') r = get(f'https://github.com/{user}?tab=repositories') while True: for m in re.finditer(r'"]+)" itemprop="name codeRepository"(\s[^>]*)?>', r.text):