Skip to content

Instantly share code, notes, and snippets.

@npyoung
Created March 16, 2020 05:08
Show Gist options
  • Save npyoung/d692640138f016a91bc18585e2703150 to your computer and use it in GitHub Desktop.
Save npyoung/d692640138f016a91bc18585e2703150 to your computer and use it in GitHub Desktop.
Install a KSP mod from github
#!/usr/bin/env python
import argparse as ap
import re
import requests
import json
from tempfile import TemporaryFile
from zipfile import ZipFile
def main(project_path, ksp_path):
pattern = re.compile(r"https:\/\/github.com\/(\w+)\/(\w+)")
match = pattern.match(project_path)
user, repo = match.groups()
response = requests.get(f"https://api.github.com/repos/{user:s}/{repo:s}/releases/latest")
response_json = json.loads(response.content)
zip_url = response_json["assets"][0]["browser_download_url"]
zip_response = requests.get(zip_url)
with TemporaryFile() as tf:
tf.write(zip_response.content)
with ZipFile(tf) as zip_file:
zip_file.extractall(ksp_path)
if __name__ == '__main__':
parser = ap.ArgumentParser()
parser.add_argument('project_path')
parser.add_argument('ksp_path')
args = parser.parse_args()
main(**vars(args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment