The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

66 lines
2.0 KiB

  1. #!/usr/bin/env python3
  2. import logging
  3. import re
  4. import requests
  5. import sys
  6. GIT_URLS_OPTION = '--git-urls'
  7. GITGUD_COMPLETE_ITEMS_OPTION = '--gitgud-complete-items'
  8. MODES = (GIT_URLS_OPTION, GITGUD_COMPLETE_ITEMS_OPTION)
  9. mode = None
  10. users = sys.argv[1:]
  11. if users and users[0] in MODES:
  12. mode = users[0]
  13. users = users[1:]
  14. assert users and (mode is None or mode in MODES) and not users[0].startswith('--'), f'Usage: github-list-repos [{" | ".join(MODES)}] USER [USER...]'
  15. def get(url):
  16. while True:
  17. logging.info(f'Fetching {url}')
  18. r = requests.get(url, headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0'})
  19. if r.status_code == 429:
  20. logging.warning(f'Got 429, sleeping and retrying')
  21. time.sleep(5)
  22. else:
  23. break
  24. return r
  25. def p(repoName):
  26. if mode is None:
  27. print(f'https://github.com/{repoName}')
  28. elif mode == GIT_URLS_OPTION:
  29. print(f'https://github.com/{repoName}.git')
  30. print(f'https://github.com/{repoName}.wiki.git')
  31. elif mode == GITGUD_COMPLETE_ITEMS_OPTION:
  32. print(f'web:complete:{repoName}')
  33. for user in users:
  34. r = get(f'https://github.com/{user}')
  35. if '<div id="org-repositories"' in r.text:
  36. # Organisation, ?page=2 pagination
  37. page = 1
  38. while True:
  39. for m in re.finditer(r'<a itemprop="name codeRepository"\s(?:[^>]*\s)?data-hovercard-url="/([^/>"]+/[^/>"]+)/hovercard"', r.text):
  40. p(m.group(1))
  41. if '<a class="next_page"' not in r.text:
  42. # End of pagination
  43. break
  44. page += 1
  45. r = get(f'https://github.com/{user}?page={page}')
  46. else:
  47. # User, ?tab=repositories + cursor pagination
  48. r = get(f'https://github.com/{user}?tab=repositories')
  49. while True:
  50. for m in re.finditer(r'<a href="/([^/>"]+/[^/>"]+)" itemprop="name codeRepository"(\s[^>]*)?>', r.text):
  51. p(m.group(1))
  52. if not (m := re.search(r'<a\s(?:[^>]*\s)?href="https://github\.com/[^/?"]+\?after=([^&]+)&amp;tab=repositories"(?:\s[^>]*)?>', r.text)):
  53. # End of pagination
  54. break
  55. r = get(f'https://github.com/{user}?after={m.group(1)}&tab=repositories')