commit e2f6a6e0a09112f40066bbb810ab028979f9cf7c Author: oitsjustjose Date: Tue Apr 18 10:33:30 2023 -0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..466e248 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +out/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b2bdf0e --- /dev/null +++ b/README.md @@ -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` diff --git a/download.py b/download.py new file mode 100644 index 0000000..dc18f59 --- /dev/null +++ b/download.py @@ -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") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a962192 Binary files /dev/null and b/requirements.txt differ diff --git a/upload.py b/upload.py new file mode 100644 index 0000000..b95d7a2 --- /dev/null +++ b/upload.py @@ -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")