factorio-ansible/factorio-mod-downloader/mod-downloader.py
2022-04-08 00:08:34 +02:00

43 lines
No EOL
1.2 KiB
Python

import requests
import json
import sys
from os.path import exists
def login(username, password):
print("> Login for user "+username)
payload = {'username': username, 'password': password', 'api_version':'4'}
headers = {'content-type': 'application/json'}
r = requests.post("https://auth.factorio.com/api-login", data=payload)
return r.json()["token"]
def downloadRelease(releaseInfo, username, token):
downloadURL = "https://mods.factorio.com"+releaseInfo["download_url"]+"?username="+username+"&token="+token
r = requests.get(downloadURL)
with open(releaseInfo["file_name"], "wb") as f:
f.write(r.content)
def checkReleaseExistsOnHDD(releaseInfo):
file_exists = exists(releaseInfo["file_name"])
return file_exists
def getReleaseInfo(name, version):
print("> Get Release Info")
r = requests.get('https://mods.factorio.com/api/mods/'+name)
data = r.json()
if version == "latest":
return data["releases"][-1]
for r in data["releases"]:
if r["version"] == version:
return r
sys.exit(200)
r = getReleaseInfo(sys.argv[1], sys.argv[2])
if checkReleaseExistsOnHDD(r) == False:
token = login(sys.argv[3], sys.argv[4])
downloadRelease(r, sys.argv[3], token)