Initial commit

This commit is contained in:
Isaac Shoebottom 2024-07-31 19:31:46 -03:00
commit 29e3a425a3
5 changed files with 1008 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Ignore downloaded files
downloads/
# Fallback in case files are not in the downloads folder
*.7z
# Ignore the build folder
__pycache__/
# Ignore environment files
env.py

105
download.py Normal file
View File

@ -0,0 +1,105 @@
import json
import os
import time
from urllib.request import Request, urlopen, build_opener
BASE_URL = "https://api.nexusmods.com"
API_VERSION = "v1"
API_URL = f"{BASE_URL}/{API_VERSION}/games"
from env import API_KEY
# Docs: https://app.swaggerhub.com/apis-docs/NexusMods/nexus-mods_public_api_params_in_form_data/1.0
xEdits = {
"TES4Edit": {
"name": "TES4Edit",
"game": "oblivion",
"id": 11536,
},
"FO3Edit": {
"name": "FO3Edit",
"game": "fallout3",
"id": 637,
},
"FNVEdit": {
"name": "FNVEdit",
"game": "newvegas",
"id": 34703,
},
"TES5Edit": {
"name": "TES5Edit",
"game": "skyrim",
"id": 25859,
},
"EnderalEdit": {
"name": "EnderalEdit",
"game": "enderal",
"id": 23,
},
"SSEEdit": {
"name": "SSEEdit",
"game": "skyrimspecialedition",
"id": 164,
},
"EnderalSEEdit": {
"name": "EnderalSEEdit",
"game": "enderalspecialedition",
"id": 78,
},
"FO4Edit": {
"name": "FO4Edit",
"game": "fallout4",
"id": 2737,
},
"FO76Edit": {
"name": "FO76Edit",
"game": "fallout76",
"id": 30,
}
}
def get_main_file_id(edit):
request = Request(f"{API_URL}/{edit['game']}/mods/{edit['id']}/files.json", headers={"apikey": API_KEY})
response = urlopen(request)
data = json.loads(response.read())
files_array = data["files"]
for file in files_array:
if file["is_primary"]:
edit["version"] = file["version"] # Set version on edit
edit["file_id"] = file["file_id"] # Set file_id on edit
return file["file_id"] # Return file_id for use in download_link
print("No main file found for ", edit["name"])
def get_download_link(edit):
file_id = get_main_file_id(edit)
request = Request(f"{API_URL}/{edit['game']}/mods/{edit['id']}/files/{file_id}/download_link.json", headers={"apikey": API_KEY})
response = urlopen(request)
data = json.loads(response.read())
for cdn in data:
if cdn["short_name"] == "Nexus CDN":
return cdn["URI"]
print("No download link found for ", edit["name"])
def download_file(edit):
download_link = get_download_link(edit)
# Rewrite spaces to %20
download_link = download_link.replace(" ", "%20")
opener = build_opener()
print("Download link: ", download_link)
opener.addheaders = [("apikey", API_KEY), ("User-Agent", "Mozilla/5.0")]
filename = f"{edit['name']} {edit['version']}.7z"
path = f"./downloads/{edit['name']}/{filename}"
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
# If file already exists, skip download
if os.path.exists(path):
print(f"File {filename} already exists, skipping download...")
return
with opener.open(download_link) as response:
with open(path, "wb") as file:
file.write(response.read())
for edit in xEdits:
print(f"Downloading {edit}...")
download_file(xEdits[edit])
# Wait 1 second between downloads, to prevent rate limiting
time.sleep(1)

1
env.example.py Normal file
View File

@ -0,0 +1 @@
API_KEY = ""

View File

@ -0,0 +1,32 @@
[
{
"name": "Chicago (Premium)",
"short_name": "Chicago",
"URI": "Redacted"
},
{
"name": "Nexus Global Content Delivery Network",
"short_name": "Nexus CDN",
"URI": "Redacted"
},
{
"name": "Amsterdam (Premium)",
"short_name": "Amsterdam",
"URI": "Redacted"
},
{
"name": "Prague (Premium)",
"short_name": "Prague",
"URI": "Redacted"
},
{
"name": "Los Angeles (Premium)",
"short_name": "Los Angeles",
"URI": "Redacted"
},
{
"name": "Miami (Premium)",
"short_name": "Miami",
"URI": "Redacted"
}
]

862
get_files_example.json Normal file

File diff suppressed because one or more lines are too long