reposave.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. #
  3. # Requires git
  4. import requests
  5. import os
  6. import argparse
  7. from urllib.parse import urlparse
  8. import tarfile
  9. from zipfile import ZipFile
  10. parser = argparse.ArgumentParser(description='reposave.py Download stuff from git repos')
  11. parser.add_argument('-gh', '--github', type=str, metavar='USER', help='Download all of a user\'s repos from github')
  12. parser.add_argument('-gt', '--gitea', type=str, metavar='SERVER/USER', help='Download all of a user\'s repos from gitea')
  13. parser.add_argument('-r', '--releases', action="store_true", default=False, help='Fetch the newest release from github/gitea repo')
  14. args = parser.parse_args()
  15. if args.github:
  16. username = urlparse(args.github).path.replace("/","",1)
  17. api_url = "https://api.github.com"
  18. if args.releases:
  19. get_releases = True
  20. get_repos = False
  21. else:
  22. get_repos = True
  23. get_releases = False
  24. elif args.gitea and "/" in args.gitea:
  25. username = urlparse(args.gitea).path.replace("/","",1)
  26. api_url = f"{urlparse(args.gitea).scheme}://{urlparse(args.gitea).netloc}/api/v1"
  27. if args.releases:
  28. get_releases = True
  29. get_repos = False
  30. else:
  31. get_repos = True
  32. get_releases = True
  33. else:
  34. print("Please tell what gitea server is this from e.g. https://domain.name/USER")
  35. quit()
  36. if get_repos == True:
  37. data = requests.get(f"{api_url}/users/{username}/repos").json()
  38. print(username)
  39. os.mkdir(username)
  40. for repo in data:
  41. print(repo["name"])
  42. print(repo["description"])
  43. os.system(f"cd {username} && git clone {repo['clone_url']}")
  44. if args.releases:
  45. print(f"{api_url}/repos/{username}/releases")
  46. data = requests.get(f"{api_url}/repos/{username}/releases").json()
  47. assets=[]
  48. for asset in data[0]['assets']:
  49. assets.append(f"{asset['name']} - {asset['browser_download_url']}")
  50. assets = '\n'.join(assets)
  51. print(f"""Release Number: {data[0]['tag_name']}
  52. Release Name: {data[0]['name']}
  53. ---
  54. [0] Tarball: {data[0]['tarball_url']}
  55. [1] Zipball: {data[0]['zipball_url']}
  56. Assets:
  57. [2] {assets}""")
  58. try:
  59. choice = int(input("Download: "))
  60. except:
  61. print("Has to be an integer")
  62. def extract_tars(file_name, url):
  63. with open(file_name,"wb") as f:
  64. f.write(requests.get(url).content)
  65. print("Downloading Tar file...")
  66. my_tar = tarfile.open(file_name)
  67. print("Extracting Tar file...")
  68. my_tar.extractall(data[0]['name'])
  69. my_tar.close()
  70. print("Finished Extraction...")
  71. os.remove(file_name)
  72. print("Removed tar file")
  73. def extract_zips(file_name, url):
  74. print("Downloading Zip file...")
  75. with open(file_name,"wb") as f:
  76. f.write(requests.get(url).content)
  77. print("Extracting Zip file...")
  78. with ZipFile(file_name,"r") as zip:
  79. zip.extractall()
  80. print("Finished Extraction...")
  81. os.remove(file_name)
  82. print("Removed zip file")
  83. if choice == 0:
  84. extract_tars(data[0]['name']+".tar.gz", data[0]['tarball_url'])
  85. elif choice == 1:
  86. extract_zips(data[0]['name']+".zip", data[0]['zipball_url'])
  87. # Issue right now with this is it doesn't handle in case there are multiple assets. Future me problem :(
  88. elif choice == 2:
  89. if assets.split(" - ")[0].endswith(".zip"):
  90. extract_zips(assets.split(" - ")[0]+".zip", assets.split(" - ")[1])
  91. if assets.split(" - ")[0].endswith(".tar.gz"):
  92. extract_tars(assets.split(" - ")[0]+".tar.gz", assets.split(" - ")[1])