2021-12-14 22:03:03 -06:00
|
|
|
from os import error
|
2021-12-14 18:09:16 -06:00
|
|
|
import requests
|
|
|
|
debug = False
|
|
|
|
if debug == True:
|
|
|
|
import logging
|
|
|
|
from requests import api
|
|
|
|
logging.basicConfig()
|
|
|
|
logging.getLogger().setLevel(logging.DEBUG)
|
|
|
|
requests_log = logging.getLogger("requests.packages.urllib3")
|
|
|
|
requests_log.setLevel(logging.DEBUG)
|
|
|
|
requests_log.propagate = True
|
|
|
|
timeoutTime = 1
|
|
|
|
base_api_url = "https://papermc.io/api/v2"
|
2021-12-14 22:03:03 -06:00
|
|
|
def failCheck(response):
|
|
|
|
print("Status Code is: "+str(response.status_code))
|
|
|
|
if response.status_code != 200:
|
|
|
|
raise error
|
2021-12-14 18:09:16 -06:00
|
|
|
# This function returns the versions from a project from the Paper API.
|
|
|
|
def getVersions(project):
|
2021-12-14 20:02:16 -06:00
|
|
|
print("Calling getVersions()...")
|
2021-12-14 18:09:16 -06:00
|
|
|
response = requests.get(base_api_url+"/projects/"+project, timeout=timeoutTime)
|
2021-12-14 22:03:03 -06:00
|
|
|
failCheck(response)
|
2021-12-14 18:09:16 -06:00
|
|
|
api_response = response.json()
|
|
|
|
return api_response['versions']
|
|
|
|
|
|
|
|
def getLatest(project):
|
2021-12-14 20:02:16 -06:00
|
|
|
print("Calling getLatest()...")
|
2021-12-14 18:09:16 -06:00
|
|
|
versions = getVersions(project)
|
|
|
|
return versions[-1]
|
|
|
|
|
|
|
|
def getBuildNumber(project, version):
|
2021-12-14 20:02:16 -06:00
|
|
|
print("Calling getBuildNumber()...")
|
2021-12-14 18:09:16 -06:00
|
|
|
response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version, timeout=timeoutTime)
|
2021-12-14 22:03:03 -06:00
|
|
|
failCheck(response)
|
2021-12-14 18:09:16 -06:00
|
|
|
api_response = response.json()
|
|
|
|
return api_response['builds']
|
|
|
|
|
2021-12-14 20:02:16 -06:00
|
|
|
def getBuildInfo(project, version):
|
|
|
|
print("Calling getBuildInfo()...")
|
|
|
|
buildID1 = getBuildNumber(project, version)
|
|
|
|
buildID = str(buildID1[-1])
|
|
|
|
response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version+"/builds/"+buildID, timeout=timeoutTime)
|
2021-12-14 22:03:03 -06:00
|
|
|
failCheck(response)
|
2021-12-14 20:02:16 -06:00
|
|
|
api_response = response.json()
|
|
|
|
return api_response
|
2021-12-14 18:09:16 -06:00
|
|
|
|
2021-12-14 20:02:16 -06:00
|
|
|
def getJarInfo(project, version):
|
|
|
|
print("Calling getJarInfo()...")
|
|
|
|
buildInfo = getBuildInfo(project, version)
|
|
|
|
workingDict = buildInfo["downloads"]
|
|
|
|
workingDict2 = workingDict["application"]
|
|
|
|
return workingDict2
|
2021-12-14 18:09:16 -06:00
|
|
|
|
2021-12-14 20:02:16 -06:00
|
|
|
def downloadVersion(project, version):
|
|
|
|
print("Calling downloadVersion()...")
|
|
|
|
jarName = getJarInfo(project, version)
|
|
|
|
buildID1 = getBuildNumber(project, version)
|
|
|
|
buildID = str(buildID1[-1])
|
|
|
|
response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version+"/builds/"+buildID+"/downloads/"+jarName["name"], stream=True, timeout=timeoutTime)
|
2021-12-14 22:03:03 -06:00
|
|
|
failCheck(response)
|
2021-12-14 20:02:16 -06:00
|
|
|
return response
|
2021-12-14 22:03:03 -06:00
|
|
|
|