From e2f6a6e0a09112f40066bbb810ab028979f9cf7c Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Tue, 18 Apr 2023 10:33:30 -0500 Subject: [PATCH] Initial commit --- .gitignore | 1 + README.md | 12 ++++++++ download.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | Bin 0 -> 3552 bytes upload.py | 69 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 155 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 download.py create mode 100644 requirements.txt create mode 100644 upload.py 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 0000000000000000000000000000000000000000..a9621920ff9777bea5399c5aef4a8713f4e63645 GIT binary patch literal 3552 zcmZvfUvC;$5XJ9vq<$1EFC=dA(3d_mY9&WeqbiRTf7X~_V8h}7{_t(jZw|u+)j~^b z&&-)KGw06T{QJ+iEXt-l==-U(r7yqeGc2QWQyM+D<)`wd*ypkA^y$I_`+GfaU$GC$ zR9Fk4t>w>eV?B@h9qe00{j6tn5!uUoF7L78@FBAq)a-rv9;mwuoThw`XL5W}wt3kK zrBk-d)kL=8DO)d}t?FHrS$GT;-$cH5XOE+pOvQU16|{l4Q_S|vtvToHOM35wxl+WY zJSzv;-v+lEb<rgq|G52CB zw5_s%wK#d)$_FnxHayL3Dtmg+^QOFuZ}>8?aqw-0G*D0Iz)F8(wl9$ZW?6@MdGg5% zg?N@m+$a{5ap2^H=#zCcm35_TM@=xbXkOr?&o=B#q)z)7RgPlTD9pYyF@4e}Goh(k zVOtKWvQ;iRwNZuOF+XdZ%wV~)--I*PRI=lAF=zV_ar9&@uZ6s<0N0*cXRbRvTlpoU zCQgwRG7p6h^!+RksvMMwex`<5{G~>5hUn>|*fkeu)*KcT&WR$1y4?{p+Nnm=0n>?c zpxT{0XL|UTh`Wf$+NsDf*xbR|nT4YIh^B)ur-!*otN>T|P3TOYIp65p9qkokZ+KeC zRPcxRtqU>3;N*>vJzpy7C^UTLVb85)KM=rwi9T*r={H48#dV+#!0{jp^)MwilIMIM z9G9w~D_`{UgQD=d*7-_EY&KN$Lmu>#@5G}!@bFf_n=QyG#{(-FeOgzV0BGnjXOkK6 z^5j;^g>nX}*ts|XoGAB>b8W>^`MoU6pe?_P;e&q5yw)T>E7={(v}^22k?c|wv=B3E z86S8j8+454HuglXZ%+vy_bRL7G^1tegZWxnecqhisCi0036lz!>Q{1RkNCb=%Zh^Q zHtR5Ltj^|L%ait(;|6~w;x+?_MASGf6YM_F|%!T4TS^YW`u z58-3wU2nf|i z)x>@^GJZajL zWZ8#xq=|vU$$8C&rmpY%>lsxfza7;*M(v&u^YfjFzTJ7B5_)9b?0Zy|Q-QB7>`3NH zHS88VJlWhDY^$o&=A!F;MEM=c%)oU=nM&T8t)}g}g7Zh$(0658&qu!Xk+Y4P1wK>* zD*tjrP^Z~@pK_zJGrbk9pgb#VQZQ22)_%@7-wv&T#Ps|ZCf9vf*C6-}Z?oEWxPVzC2#(z#!r`mM&>0Q)fJ)tvFT zUHtBkH8;As@FsY9r_wVXZ_%$YwPkFI=kq&robZk#TiTyr`m1pN#QayrcdDXUYMo literal 0 HcmV?d00001 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")