Initial commit

This commit is contained in:
oitsjustjose 2023-04-18 10:33:30 -05:00
commit e2f6a6e0a0
5 changed files with 155 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
out/

12
README.md Normal file
View file

@ -0,0 +1,12 @@
# Curse to Modrinth Migrator
This project only handles copying files from Curse to Modrinth - nothing else. It's pretty crude but great if you want to catch up your Modrinth mod with CurseForge's versions.
## Steps
1. Install Python 3.10 from [here](https://www.python.org/downloads/release/python-3100) - scroll down to files
2. Run `pip[3][.exe] install -r requirements.txt`
3. Open `download.py`, scroll all the way to the bottom and insert a `mod_archive_all` for each mod's slug you want to automatically fully archive. One has been included by default, but you can repeat `mod_archive_all("YOURMODSLUG")` as many times as you wish. This process takes a while.
4. Run `python[3][.exe] download.py`
5. For each mod you just downloaded, modify `upload.py` at the very top to include your newly created Modrinth project ID, and the folder with all files for the mod you just downloaded from Curseforge in step 4.
6. Run `python[3][.exe] upload.py`

73
download.py Normal file
View file

@ -0,0 +1,73 @@
"""**EH**"""
from typing import List
import os
from time import sleep
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
URL: str
def get_mods_for_page(driver: webdriver.Chrome, page: int) -> List[str]:
"""EH"""
driver.get(f"{URL}?page={page}")
root = driver.page_source
root = BeautifulSoup(root, features="html.parser")
file_links = root.body.find_all("a", attrs={"data-action": "file-link"})
return list(
map(
lambda x: f"https://legacy.curseforge.com/{x['href']}".replace(
"files", "download"
),
file_links,
)
)
def mod_archive_all(modid: str) -> None:
"""EH"""
global URL
URL = f"https://legacy.curseforge.com/minecraft/mc-mods/{modid}/files/all"
os.makedirs(f"./out/{modid}", exist_ok=True)
page: int = 1
page_num: int = 1
options = Options()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
options.add_experimental_option(
"prefs",
{
"download.default_directory": os.path.realpath(f"./out/{modid}/"),
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True,
},
)
driver = webdriver.Chrome(options=options)
driver.get(f"{URL}?page={page}")
root = driver.page_source
root = BeautifulSoup(root, features="html.parser")
page_els = root.body.find_all("a", attrs={"class": "pagination-item"})
if page_els: # mods with only 1 page won't have a pagination item
page_el = page_els[-1]
page_el = page_el.find("span").text
page_num = int(page_el)
all_urls = []
for page in range(page_num):
all_urls += get_mods_for_page(driver, page + 1)
for url in all_urls:
driver.get(url)
sleep(8)
if __name__ == "__main__":
mod_archive_all("YOUR_MODID")

BIN
requirements.txt Normal file

Binary file not shown.

69
upload.py Normal file
View file

@ -0,0 +1,69 @@
"""EH"""
import os
import json
import requests
MOD_OUTPUT_PATH = "./out/MODID" # CHANGE ME
MODRINTH_PROJECT_ID = "" # CHANGE ME
def main(api_key: str) -> None:
"""EH"""
for fpath in os.listdir(MOD_OUTPUT_PATH):
with open(f"{MOD_OUTPUT_PATH}/{fpath}", "rb") as modfile:
data = modfile.read()
parts = fpath.split("-")
if len(parts) != 3:
continue
version_title = " ".join(parts).replace(".jar", "")
# .x versions somehow were only ever for the .0, .1 and .2 game vers lol
if "x" in parts[1].lower():
root = parts[1].replace(".x", "").replace(".X", "")
game_versions = [root, f"{root}.1", f"{root}.2"]
else:
game_versions = [parts[1]]
version = parts[2].replace(".jar", "")
payload = json.dumps(
{
"name": version_title,
"version_number": f"{parts[1]}-{version}",
"changelog": "Migrated Automagically from CurseForge",
"dependencies": [],
"game_versions": game_versions,
"loaders": ["forge"],
"featured": False,
"requested_status": "listed",
"version_type": "release",
"project_id": MODRINTH_PROJECT_ID,
"primary_file": fpath,
"file_parts": [fpath],
}
)
response = requests.post(
"https://api.modrinth.com/v2/version",
headers={"Authorization": api_key},
files=[
("data", (None, payload, None)),
("files", (fpath, data, "application/octet-stream")),
],
)
if response.status_code == 200:
print(f"Successfully uploaded {fpath} - deleting")
os.unlink(f"{MOD_OUTPUT_PATH}/{fpath}")
else:
print(f"---------{fpath}---------")
print(response.text)
print(json.dumps(response.json(), indent=2))
if __name__ == "__main__":
if "github_token" in os.environ:
main(os.environ["github_token"])
else:
print("`github_token` is not part of your PATH, add it and run again")