From c5206e53bfaefd7824c5e031ee3f954e46fadaef Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Fri, 26 Jan 2024 12:35:45 +0100 Subject: [PATCH 01/23] Groundwork refactoring to support cf Signed-off-by: TheBossMagnus --- src/ModpackChangelogger.py | 4 +++- src/compare_packs.py | 29 ++++------------------------- src/extract_pack_data.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 src/extract_pack_data.py diff --git a/src/ModpackChangelogger.py b/src/ModpackChangelogger.py index 749373b..74ac7a2 100644 --- a/src/ModpackChangelogger.py +++ b/src/ModpackChangelogger.py @@ -3,6 +3,7 @@ from compare_packs import compare_packs from config_handler import load_config from constants import VERSION +from extract_pack_data import mr_get_pack_data from get_json import get_json from out import markdown_out @@ -57,7 +58,8 @@ def main(old_path, new_path, config_path, changelog_file, debug=False): new_json = get_json(new_path) # Compare the packs - added, removed, updated = compare_packs(old_json, new_json, config) + old_ids, new_ids, old_info, new_info = mr_get_pack_data(old_json, new_json) + added, removed, updated = compare_packs(old_ids, new_ids, old_info, new_info, config) # Print in a md doc markdown_out(added, removed, updated, config, changelog_file) diff --git a/src/compare_packs.py b/src/compare_packs.py index 54f3a59..3cae63a 100644 --- a/src/compare_packs.py +++ b/src/compare_packs.py @@ -1,34 +1,13 @@ import asyncio import logging -import re from get_mod_names import get_mod_names -PATTERN = re.compile(r"(?<=data\/)[a-zA-Z0-9]{8}") -def get_dependency_info(json): - loader = next((key for key in json['dependencies'].keys() if key != 'minecraft'), "Unknown") - return { - 'mc_version': json['dependencies']['minecraft'], - 'loader': loader, - 'loader_version': json['dependencies'][loader] - } +def compare_packs(old_ids, new_ids, old_info, new_info, config): -def get_mod_urls(json): - return [download for url in json['files'] for download in url['downloads']] - -def extract_mod_ids(url_list): - return [PATTERN.search(str(url)).group(0) for url in url_list] - -def compare_packs(old_json, new_json, config): - old_info, new_info = get_dependency_info(old_json), get_dependency_info(new_json) - - new_urls, old_urls = set(get_mod_urls(new_json)), set(get_mod_urls(old_json)) - new_urls, old_urls = new_urls.difference(old_urls), old_urls.difference(new_urls) - - added_ids, removed_ids = set(extract_mod_ids(list(new_urls))), set(extract_mod_ids(list(old_urls))) - updated_ids = added_ids & removed_ids - added_ids -= updated_ids - removed_ids -= updated_ids + updated_ids = old_ids & new_ids + added_ids = new_ids - old_ids + removed_ids = old_ids - new_ids # Remove a category if disabled in config if not config['check']['added_mods']: diff --git a/src/extract_pack_data.py b/src/extract_pack_data.py new file mode 100644 index 0000000..22880e6 --- /dev/null +++ b/src/extract_pack_data.py @@ -0,0 +1,32 @@ +import re +import sys + +def mr_get_pack_data(old_json, new_json): + + def get_dependency_info(json): + loader = next((key for key in json['dependencies'].keys() if key != 'minecraft'), "Unknown") + return { + 'mc_version': json['dependencies']['minecraft'], + 'loader': loader, + 'loader_version': json['dependencies'][loader] + } + + def get_mod_urls(json): + return [download for url in json['files'] for download in url['downloads']] + + def extract_mod_ids(url_list): + PATTERN = re.compile(r"(?<=data\/)[a-zA-Z0-9]{8}") + return [PATTERN.search(str(url)).group(0) for url in url_list] + + old_info, new_info = get_dependency_info(old_json), get_dependency_info(new_json) + new_urls, old_urls = set(get_mod_urls(new_json)), set(get_mod_urls(old_json)) + # remove urls that are in both packs (not added nor removed nor updated) + new_urls -= old_urls + old_urls -= new_urls + + old_ids, new_ids = set(extract_mod_ids(new_urls)), set(extract_mod_ids(old_urls)) + return old_ids, new_ids, old_info, new_info + +def cf_get_pack_data(): + print("Not yet implemented") + sys.exit(1) \ No newline at end of file From 776352ffefeadbd09ebe42c0121c98635536ca52 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Fri, 26 Jan 2024 12:54:56 +0100 Subject: [PATCH 02/23] refactors Signed-off-by: TheBossMagnus --- src/ModpackChangelogger.py | 4 +++- src/compare_packs.py | 23 ++++++++--------------- src/extract_pack_data.py | 6 ++++-- src/get_json.py | 1 - src/get_mod_names.py | 4 ++-- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/ModpackChangelogger.py b/src/ModpackChangelogger.py index 74ac7a2..319ccf4 100644 --- a/src/ModpackChangelogger.py +++ b/src/ModpackChangelogger.py @@ -18,7 +18,7 @@ def setup_logging(debug): # Clear the log file with open('log.txt', 'w', encoding="utf-8") as f: f.write('') - + file_handler = logging.FileHandler('log.txt', encoding='utf-8') file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%H:%M')) @@ -60,6 +60,8 @@ def main(old_path, new_path, config_path, changelog_file, debug=False): # Compare the packs old_ids, new_ids, old_info, new_info = mr_get_pack_data(old_json, new_json) added, removed, updated = compare_packs(old_ids, new_ids, old_info, new_info, config) + logger.debug("Added mods: %s\nRemoved mods%s\nUpdated mods\n", added, removed, updated) + # Print in a md doc markdown_out(added, removed, updated, config, changelog_file) diff --git a/src/compare_packs.py b/src/compare_packs.py index 3cae63a..363c3c0 100644 --- a/src/compare_packs.py +++ b/src/compare_packs.py @@ -2,29 +2,24 @@ import logging from get_mod_names import get_mod_names - def compare_packs(old_ids, new_ids, old_info, new_info, config): - updated_ids = old_ids & new_ids added_ids = new_ids - old_ids removed_ids = old_ids - new_ids # Remove a category if disabled in config - if not config['check']['added_mods']: - added_ids = set() - if not config['check']['removed_mods']: - removed_ids = set() - if not config['check']['updated_mods']: - updated_ids = set() + added_ids = added_ids if config['check']['added_mods'] else set() + removed_ids = removed_ids if config['check']['removed_mods'] else set() + updated_ids = updated_ids if config['check']['updated_mods'] else set() added_mods, removed_mods, updated_mods = asyncio.run(get_mod_names(added_ids, removed_ids, updated_ids)) - added_mods = sorted(mod for mod in added_mods if mod is not None) - removed_mods = sorted(mod for mod in removed_mods if mod is not None) - updated_mods = sorted(mod for mod in updated_mods if mod is not None) + added_mods = sorted(mod for mod in added_mods if mod) + removed_mods = sorted(mod for mod in removed_mods if mod) + updated_mods = sorted(mod for mod in updated_mods if mod) if config['check']['loader']: - if old_info['loader'] != new_info['loader']: + if old_info['loader'] != new_info['loader']: added_mods.append(f"{new_info['loader']} (mod loader)") removed_mods.append(f"{old_info['loader']} (mod loader)") logging.debug("Loader change detected: %s, new loader: %s", old_info['loader'], new_info['loader']) @@ -36,6 +31,4 @@ def compare_packs(old_ids, new_ids, old_info, new_info, config): updated_mods.append(f"Minecraft version {new_info['mc_version']}") logging.debug("Minecraft version change detected: %s, new version: %s", old_info['mc_version'], new_info['mc_version']) - logging.debug("Added mods: %a\nRemoved mods: %a\nUpdated mods: %a", added_mods, removed_mods, updated_mods) - - return added_mods, removed_mods, updated_mods \ No newline at end of file + return added_mods, removed_mods, updated_mods diff --git a/src/extract_pack_data.py b/src/extract_pack_data.py index 22880e6..605503b 100644 --- a/src/extract_pack_data.py +++ b/src/extract_pack_data.py @@ -1,6 +1,9 @@ import re import sys +# Move the pattern outside of the function +PATTERN = re.compile(r"(?<=data\/)[a-zA-Z0-9]{8}") + def mr_get_pack_data(old_json, new_json): def get_dependency_info(json): @@ -15,7 +18,6 @@ def get_mod_urls(json): return [download for url in json['files'] for download in url['downloads']] def extract_mod_ids(url_list): - PATTERN = re.compile(r"(?<=data\/)[a-zA-Z0-9]{8}") return [PATTERN.search(str(url)).group(0) for url in url_list] old_info, new_info = get_dependency_info(old_json), get_dependency_info(new_json) @@ -29,4 +31,4 @@ def extract_mod_ids(url_list): def cf_get_pack_data(): print("Not yet implemented") - sys.exit(1) \ No newline at end of file + sys.exit(0) diff --git a/src/get_json.py b/src/get_json.py index dd2a10e..a1586dd 100644 --- a/src/get_json.py +++ b/src/get_json.py @@ -5,7 +5,6 @@ import sys from zipfile import ZipFile - def get_json(path): # Ensure the file is a modpack file if not path.endswith('.mrpack'): diff --git a/src/get_mod_names.py b/src/get_mod_names.py index e85f372..9ec5004 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -27,7 +27,7 @@ async def request_from_api(session, ids): except aiohttp.ClientConnectionError as e: logging.warning("Failed to connect to %s: %s", URL, e) except asyncio.TimeoutError: - logging.warning("The request %s timed out ", URL) + logging.warning("The request %s timed out", URL) except aiohttp.ClientResponseError as e: logging.warning("Server responded with an error for %s: %s", URL, e) except aiohttp.ClientPayloadError as e: @@ -35,4 +35,4 @@ async def request_from_api(session, ids): except aiohttp.ClientError as e: logging.warning("An unexpected error occurred: %s", e) - return names + return names \ No newline at end of file From 35eae1a2a10a4244ec41fb6eb76842d39921df31 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Tue, 30 Jan 2024 10:02:55 +0100 Subject: [PATCH 03/23] detect modpack type Signed-off-by: TheBossMagnus --- src/constants.py | 2 ++ src/get_json.py | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/constants.py b/src/constants.py index d5485bc..a438675 100644 --- a/src/constants.py +++ b/src/constants.py @@ -1,5 +1,7 @@ # This file contains some hardcoded values, do not edit it directly if you don't know what you are doing. +# Mod ecosistem used (modrinth or curseforge) +Modpacks_Format = None # Version number VERSION = "0.2.0-beta2" # Default config diff --git a/src/get_json.py b/src/get_json.py index a1586dd..d507250 100644 --- a/src/get_json.py +++ b/src/get_json.py @@ -4,16 +4,29 @@ import shutil import sys from zipfile import ZipFile +import constants def get_json(path): - # Ensure the file is a modpack file - if not path.endswith('.mrpack'): - logging.error('ERROR: Input file is not a modpack') - sys.exit(1) if not os.path.exists(path): logging.error('ERROR: The file %s does not exist', path) sys.exit(1) + if path.endswith('.mrpack'): + if constants.Modpacks_Format == 'curseforge': + logging.error('ERROR: Using Modrinth and a Curseforge modpack together is not supported') + sys.exit(1) + constants.Modpacks_Format = 'modrinth' + logging.debug('Using Modrinth format modpack') + elif path.endswith('.zip'): + if constants.Modpacks_Format == 'modrinth': + logging.error('ERROR: Using Modrinth and a Curseforge modpack together is not supported') + sys.exit(1) + logging.debug('Using CurseForge format modpack') + constants.Modpacks_Format = 'curseforge' + else: + logging.error('ERROR: Input modpack is not in a supported format') + sys.exit(1) + # Create a temporary directory temp_dir = os.path.join(os.environ.get('TEMP'), 'ModpackChangelogger') os.makedirs(temp_dir, exist_ok=True) From a6e3246e1792b66d7ddb6057ab36f846592ba998 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Wed, 31 Jan 2024 12:48:16 +0100 Subject: [PATCH 04/23] Split the get modpack data between mr and cf Signed-off-by: TheBossMagnus --- src/ModpackChangelogger.py | 14 +++++++++----- src/extract_pack_data.py | 3 +-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ModpackChangelogger.py b/src/ModpackChangelogger.py index 319ccf4..87f6fe3 100644 --- a/src/ModpackChangelogger.py +++ b/src/ModpackChangelogger.py @@ -1,9 +1,9 @@ import argparse import logging +import constants from compare_packs import compare_packs from config_handler import load_config -from constants import VERSION -from extract_pack_data import mr_get_pack_data +from extract_pack_data import mr_get_pack_data, cf_get_pack_data from get_json import get_json from out import markdown_out @@ -27,7 +27,7 @@ def setup_logging(debug): logging.basicConfig(level=logging.INFO, handlers=[console_handler]) logger = logging.getLogger(__name__) - logger.debug("Version: %s", VERSION) + logger.debug("Version: %s", constants.VERSION) def parse_arguments(): @@ -57,8 +57,12 @@ def main(old_path, new_path, config_path, changelog_file, debug=False): old_json = get_json(old_path) new_json = get_json(new_path) + if constants.Modpacks_Format == 'modrinth': + old_ids, new_ids, old_info, new_info = mr_get_pack_data(old_json, new_json) + else: + old_ids, new_ids, old_info, new_info = cf_get_pack_data(old_json, new_json) + # Compare the packs - old_ids, new_ids, old_info, new_info = mr_get_pack_data(old_json, new_json) added, removed, updated = compare_packs(old_ids, new_ids, old_info, new_info, config) logger.debug("Added mods: %s\nRemoved mods%s\nUpdated mods\n", added, removed, updated) @@ -68,5 +72,5 @@ def main(old_path, new_path, config_path, changelog_file, debug=False): if __name__ == "__main__": args = parse_arguments() if args.version: - print(f"ModpackChangelogger {VERSION}") + print(f"ModpackChangelogger {constants.VERSION}") main(args.old, args.new, args.config, args.file, args.debug) diff --git a/src/extract_pack_data.py b/src/extract_pack_data.py index 605503b..17cbb64 100644 --- a/src/extract_pack_data.py +++ b/src/extract_pack_data.py @@ -1,10 +1,9 @@ import re import sys -# Move the pattern outside of the function -PATTERN = re.compile(r"(?<=data\/)[a-zA-Z0-9]{8}") def mr_get_pack_data(old_json, new_json): + PATTERN = re.compile(r"(?<=data\/)[a-zA-Z0-9]{8}") def get_dependency_info(json): loader = next((key for key in json['dependencies'].keys() if key != 'minecraft'), "Unknown") From 085388c3269bab79d5ee938b484e02a3ad492307 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Wed, 31 Jan 2024 12:52:17 +0100 Subject: [PATCH 05/23] fix wrongly formatted crash message Signed-off-by: TheBossMagnus --- src/ModpackChangelogger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModpackChangelogger.py b/src/ModpackChangelogger.py index 87f6fe3..884daea 100644 --- a/src/ModpackChangelogger.py +++ b/src/ModpackChangelogger.py @@ -64,7 +64,7 @@ def main(old_path, new_path, config_path, changelog_file, debug=False): # Compare the packs added, removed, updated = compare_packs(old_ids, new_ids, old_info, new_info, config) - logger.debug("Added mods: %s\nRemoved mods%s\nUpdated mods\n", added, removed, updated) + logger.debug("Added mods: %s\nRemoved mods:%s\nUpdated mods:%s\n", added, removed, updated) # Print in a md doc markdown_out(added, removed, updated, config, changelog_file) From e5868be4de7ec85f42ca85a79268c1e65c6aa40c Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Wed, 31 Jan 2024 13:02:28 +0100 Subject: [PATCH 06/23] fix wip funct args Signed-off-by: TheBossMagnus --- src/extract_pack_data.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/extract_pack_data.py b/src/extract_pack_data.py index 17cbb64..f77c6be 100644 --- a/src/extract_pack_data.py +++ b/src/extract_pack_data.py @@ -28,6 +28,7 @@ def extract_mod_ids(url_list): old_ids, new_ids = set(extract_mod_ids(new_urls)), set(extract_mod_ids(old_urls)) return old_ids, new_ids, old_info, new_info -def cf_get_pack_data(): +def cf_get_pack_data(old_json, new_json): print("Not yet implemented") sys.exit(0) + return None, None, None, None From 167aec839eeb2340d7431aaf7b74b80b06372cf3 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Wed, 31 Jan 2024 18:11:15 +0100 Subject: [PATCH 07/23] add test pack for cf --- tests/new.zip | Bin 0 -> 788 bytes tests/old.zip | Bin 0 -> 786 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/new.zip create mode 100644 tests/old.zip diff --git a/tests/new.zip b/tests/new.zip new file mode 100644 index 0000000000000000000000000000000000000000..00cb374b4d91e983a98f0d1c59f30a4f36a3fb4a GIT binary patch literal 788 zcmWIWW@Zs#zy`R0g85~sMMaq@sm1yMV09b}92iP@fg-tyd6{Xc#U*-K#rb)2CLHwp zY#`9`zOrlT`M=7y3on^H|KG&2y^H(R!Byr73WYazOx2oF?tXL0LO}%;ziIpLN}hdF z&7_cZ|3PDps4FKkgMj*ybM0+v^EdfrprlXSmpW&$|%PDED#x z&1FVckE%Q^^xn6vL`>Di>d}$vy6>D0`ybhC`zTsgSe{o{F#Gde*6X(pcSq#hJs&9g z`Pj8Jw-Uv#?%0)~9Bdg>-Q(ZLX7MkY>wHCr`F~f=NuE#RKW=`$^l6fz@$8t@-p*kL z=C@1JJ|)Qs?31-!xI6FO`nqqrW=F$nU(a4!Jo*34@O0_5U&HUmtebji{}W$^Z#6Rt zoRr@Gun3>O#NUpIf#LuE0B?4VmmUa4+@IALg63@RB))mco|C{|{foY3NA8%&#pHp%7Om_P` zv#sX7?9U!&IQOAnQ=m}O|I^{u%lmx~Pwf-5-=oMAKUwq3e9w#DoH*7!i|Xi&UbQ-T zmVsE=O!vRtVt$VIfxd$H@r=v8JF9>`PzQ=mBBlb3Uz`#$ZD@wC5Z>jNE*EGb(5W9R2hMV}3>MJ{@hs%Smq zb&dRMi+I*U+Ybufdj0hAyXP^Y>0T?>@ARCKw(-=zIjd9WJ^N{1E38^N>(4xC2AQA9 ze>gkmJUzd27fbR7K?a8Z{{y_)IreC^CvY(`Fa$C&FyIbE9#A0Wr{rV;!!V;HH)qKN z$F61riPrDT#qATOZvJ@u1&`GdMnTrtDRBqeGJ+OOe8PX}_VlWzOYiPI+k5=`$Bd)j z#kaeCn3%DCnef$)=pXDizS~Z{$Zl$BzdB?WtJ(WJucU@O8ugKGuF0}lmv)pI8L!m0 z_1yLGRd36?n~j@`+?K^0p#Qb(FaRl$e{*G tU Date: Wed, 31 Jan 2024 18:15:51 +0100 Subject: [PATCH 08/23] fix cf having a different filename Signed-off-by: TheBossMagnus --- src/get_json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/get_json.py b/src/get_json.py index d507250..d7c3d36 100644 --- a/src/get_json.py +++ b/src/get_json.py @@ -38,7 +38,7 @@ def get_json(path): logging.debug('Extracted %s to %s', path, temp_dir) # Parse the json file - json_path = os.path.join(temp_dir, 'modrinth.index.json') + json_path = os.path.join(temp_dir, 'modrinth.index.json' if constants.Modpacks_Format == 'modrinth' else 'manifest.json') with open(json_path, 'r', encoding="utf-8") as json_file: logging.debug('Parsed %s', json_path) return json.load(json_file) From 044e39016d0bc2167e790dc0a1816f51cade2164 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Wed, 31 Jan 2024 18:33:48 +0100 Subject: [PATCH 09/23] basic cf modpack parsing support Signed-off-by: TheBossMagnus --- src/extract_pack_data.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/extract_pack_data.py b/src/extract_pack_data.py index f77c6be..d466a12 100644 --- a/src/extract_pack_data.py +++ b/src/extract_pack_data.py @@ -29,6 +29,37 @@ def extract_mod_ids(url_list): return old_ids, new_ids, old_info, new_info def cf_get_pack_data(old_json, new_json): - print("Not yet implemented") + print("Curseforge support Not yet fully implemented") + + def get_dependency_info(json): + loader_string = json['minecraft']['modLoaders'][0]['id'] + return { + 'mc_version': json['minecraft']['version'], + 'loader': loader_string.split('-')[0], + 'loader_version': loader_string.split('-')[1] + } + + + def analyze_json_files(old_json, new_json): + + old_file_ids = {item['fileID']: item['projectID'] for item in old_json['files']} + new_file_ids = {item['fileID']: item['projectID'] for item in new_json['files']} + + unique_old_project_ids = set() + unique_new_project_ids = set() + + for file_id, project_id in old_file_ids.items(): + if file_id not in new_file_ids: + unique_old_project_ids.add(project_id) + + for file_id, project_id in new_file_ids.items(): + if file_id not in old_file_ids: + unique_new_project_ids.add(project_id) + + return unique_old_project_ids, unique_new_project_ids + + old_info, new_info = get_dependency_info(old_json), get_dependency_info(new_json) + old_ids, new_ids = analyze_json_files(old_json, new_json) + print(old_ids, new_ids) sys.exit(0) - return None, None, None, None + return old_ids, new_ids, old_info, new_info From e57e19b00882434d2c94f8a162881ce23fb93fc4 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Thu, 1 Feb 2024 11:31:06 +0100 Subject: [PATCH 10/23] Refactor cf modpack parsing Signed-off-by: TheBossMagnus --- src/extract_pack_data.py | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/extract_pack_data.py b/src/extract_pack_data.py index d466a12..0947e98 100644 --- a/src/extract_pack_data.py +++ b/src/extract_pack_data.py @@ -29,8 +29,6 @@ def extract_mod_ids(url_list): return old_ids, new_ids, old_info, new_info def cf_get_pack_data(old_json, new_json): - print("Curseforge support Not yet fully implemented") - def get_dependency_info(json): loader_string = json['minecraft']['modLoaders'][0]['id'] return { @@ -38,28 +36,18 @@ def get_dependency_info(json): 'loader': loader_string.split('-')[0], 'loader_version': loader_string.split('-')[1] } - - - def analyze_json_files(old_json, new_json): - - old_file_ids = {item['fileID']: item['projectID'] for item in old_json['files']} - new_file_ids = {item['fileID']: item['projectID'] for item in new_json['files']} - unique_old_project_ids = set() - unique_new_project_ids = set() + def get_mod_ids(json): + # Extracts the file and project IDs from the JSON + return {item['fileID']: item['projectID'] for item in json['files']} - for file_id, project_id in old_file_ids.items(): - if file_id not in new_file_ids: - unique_old_project_ids.add(project_id) + old_file_ids = get_mod_ids(old_json) + new_file_ids = get_mod_ids(new_json) - for file_id, project_id in new_file_ids.items(): - if file_id not in old_file_ids: - unique_new_project_ids.add(project_id) + # To reference a file cf has a mod id (the project) and a file id (the version) + # with this code we can get mod that have been changed between the two packs, so with unquie file ids + old_ids = {old_file_ids[file_id] for file_id in old_file_ids if file_id not in new_file_ids} + new_ids = {new_file_ids[file_id] for file_id in new_file_ids if file_id not in old_file_ids} - return unique_old_project_ids, unique_new_project_ids - old_info, new_info = get_dependency_info(old_json), get_dependency_info(new_json) - old_ids, new_ids = analyze_json_files(old_json, new_json) - print(old_ids, new_ids) - sys.exit(0) return old_ids, new_ids, old_info, new_info From fcdd3108a9572dc2b09d221999ebf96661d6163f Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Thu, 1 Feb 2024 11:40:27 +0100 Subject: [PATCH 11/23] unused import Signed-off-by: TheBossMagnus --- src/extract_pack_data.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/extract_pack_data.py b/src/extract_pack_data.py index 0947e98..a224091 100644 --- a/src/extract_pack_data.py +++ b/src/extract_pack_data.py @@ -1,6 +1,4 @@ import re -import sys - def mr_get_pack_data(old_json, new_json): PATTERN = re.compile(r"(?<=data\/)[a-zA-Z0-9]{8}") From c482e3dc6bebf2c2e415113819f932b5c41ac383 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Thu, 1 Feb 2024 11:41:48 +0100 Subject: [PATCH 12/23] fix comment Signed-off-by: TheBossMagnus --- src/extract_pack_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extract_pack_data.py b/src/extract_pack_data.py index a224091..1a7b94f 100644 --- a/src/extract_pack_data.py +++ b/src/extract_pack_data.py @@ -42,7 +42,7 @@ def get_mod_ids(json): old_file_ids = get_mod_ids(old_json) new_file_ids = get_mod_ids(new_json) - # To reference a file cf has a mod id (the project) and a file id (the version) + # To reference a file cf has a project id (the mod) and a file id (the version) # with this code we can get mod that have been changed between the two packs, so with unquie file ids old_ids = {old_file_ids[file_id] for file_id in old_file_ids if file_id not in new_file_ids} new_ids = {new_file_ids[file_id] for file_id in new_file_ids if file_id not in old_file_ids} From d929600608dd6db8c843cdcbaa0183b169237503 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Fri, 2 Feb 2024 11:02:36 +0100 Subject: [PATCH 13/23] Unfinshed not working cf requests Signed-off-by: TheBossMagnus --- src/constants.py | 12 +++++++-- src/get_mod_names.py | 61 ++++++++++++++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/constants.py b/src/constants.py index c6bf2cc..03060a9 100644 --- a/src/constants.py +++ b/src/constants.py @@ -18,5 +18,13 @@ }, } # Networking -HEADERS = {'User-Agent':f"TheBossMagnus/ModpackChangelogger/{VERSION} (thebossmagnus@proton.me)"} -MODRINTH_API_URL = "https://api.modrinth.com/v2" +MR_HEADERS = {'User-Agent':f"TheBossMagnus/ModpackChangelogger/{VERSION} (thebossmagnus@proton.me)"} +MR_API_URL = "https://api.modrinth.com/v2" + +# DO NOT USE THIS KEY FOR YOUR OWN PROJECT/FORKS +# You can get your own key at https://docs.curseforge.com +CF_KEY = '$2a$10$GiT8VjJE8VJpcK68Wlz6aeJ5CPAZcRuTBcGuys8XtX5hGC87sIgku' +CF_HEADERS = {'User-Agent': f"TheBossMagnus/ModpackChangelogger/{VERSION} (thebossmagnus@proton.me)", + 'x-api-key': f"{CF_KEY}"} +CF_API_URL = "https://api.curseforge.com" + diff --git a/src/get_mod_names.py b/src/get_mod_names.py index 9ec5004..d1e9e8b 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -2,7 +2,8 @@ import json import logging import aiohttp -from constants import MODRINTH_API_URL, HEADERS +from constants import MR_API_URL, MR_HEADERS, CF_HEADERS, CF_API_URL +import constants async def get_mod_names(added_ids, removed_ids, updated_ids): async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=15)) as session: @@ -16,23 +17,49 @@ async def get_mod_names(added_ids, removed_ids, updated_ids): async def request_from_api(session, ids): # Convert the set of ids to a json array ids_list = json.dumps(list(ids)) - URL = f"{MODRINTH_API_URL}/projects?ids={ids_list}" names = [] + if constants.Modpacks_Format == "modrinth": + URL = f"{MR_API_URL}/projects?ids={ids_list}" - try: - async with session.get(URL, headers=HEADERS) as response: - response.raise_for_status() - data = await response.json() - names = [project.get('title') for project in data] - except aiohttp.ClientConnectionError as e: - logging.warning("Failed to connect to %s: %s", URL, e) - except asyncio.TimeoutError: - logging.warning("The request %s timed out", URL) - except aiohttp.ClientResponseError as e: - logging.warning("Server responded with an error for %s: %s", URL, e) - except aiohttp.ClientPayloadError as e: - logging.warning("Failed to read response from %s: %s", URL, e) - except aiohttp.ClientError as e: - logging.warning("An unexpected error occurred: %s", e) + try: + async with session.get(URL, headers=CF_HEADERS) as response: + response.raise_for_status() + data = await response.json() + names = [project.get('title') for project in data] + except aiohttp.ClientConnectionError as e: + logging.warning("Failed to connect to %s: %s", URL, e) + except asyncio.TimeoutError: + logging.warning("The request %s timed out", URL) + except aiohttp.ClientResponseError as e: + logging.warning("Server responded with an error for %s: %s", URL, e) + except aiohttp.ClientPayloadError as e: + logging.warning("Failed to read response from %s: %s", URL, e) + except aiohttp.ClientError as e: + logging.warning("An unexpected error occurred: %s", e) + + elif constants.Modpacks_Format == "curseforge": + URL = f"{CF_API_URL}/v1/mods" + headers = { + 'x-api-key': '$2a$10$GiT8VjJE8VJpcK68Wlz6aeJ5CPAZcRuTBcGuys8XtX5hGC87sIgku', + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + data = {'modIds': ids_list} + + try: + async with session.post(URL, headers=headers, json=data) as response: + response.raise_for_status() + data = await response.json() + names = [project.get('name') for project in data] + except aiohttp.ClientConnectionError as e: + logging.warning("Failed to connect to %s: %s", URL, e) + except asyncio.TimeoutError: + logging.warning("The request %s timed out", URL) + except aiohttp.ClientResponseError as e: + logging.warning("Server responded with an error for %s: %s", URL, e) + except aiohttp.ClientPayloadError as e: + logging.warning("Failed to read response from %s: %s", URL, e) + except aiohttp.ClientError as e: + logging.warning("An unexpected error occurred: %s", e) return names \ No newline at end of file From 9a95d63d5412df41748fec1e3ca837905cb0c78d Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Fri, 2 Feb 2024 11:12:11 +0100 Subject: [PATCH 14/23] fix ssl Signed-off-by: TheBossMagnus --- src/get_mod_names.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/get_mod_names.py b/src/get_mod_names.py index d1e9e8b..b68ea0e 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -44,10 +44,10 @@ async def request_from_api(session, ids): 'Content-Type': 'application/json', 'Accept': 'application/json' } - data = {'modIds': ids_list} + data = {'modIds': [348521, 238222]} try: - async with session.post(URL, headers=headers, json=data) as response: + async with session.post(URL, headers=headers, json=data, ssl=False) as response: response.raise_for_status() data = await response.json() names = [project.get('name') for project in data] From ec99a5ce68e392e2df45b8fb74f13f30df487902 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Sun, 4 Feb 2024 21:53:49 +0100 Subject: [PATCH 15/23] fix added mod wrong detections Signed-off-by: TheBossMagnus --- src/extract_pack_data.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/extract_pack_data.py b/src/extract_pack_data.py index d466a12..af7dd5d 100644 --- a/src/extract_pack_data.py +++ b/src/extract_pack_data.py @@ -22,9 +22,10 @@ def extract_mod_ids(url_list): old_info, new_info = get_dependency_info(old_json), get_dependency_info(new_json) new_urls, old_urls = set(get_mod_urls(new_json)), set(get_mod_urls(old_json)) # remove urls that are in both packs (not added nor removed nor updated) - new_urls -= old_urls - old_urls -= new_urls - + common_urls = new_urls & old_urls + new_urls -= common_urls + old_urls -= common_urls + old_ids, new_ids = set(extract_mod_ids(new_urls)), set(extract_mod_ids(old_urls)) return old_ids, new_ids, old_info, new_info From e2c868f34f787acbc3f989d19e6285ba0efa738d Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Sun, 11 Feb 2024 22:15:33 +0100 Subject: [PATCH 16/23] Finish draft of cf support (this is quite rough and needs some major cleanups/improvements) --- src/constants.py | 11 +++++++---- src/get_mod_names.py | 13 +++---------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/constants.py b/src/constants.py index 03060a9..0873411 100644 --- a/src/constants.py +++ b/src/constants.py @@ -22,9 +22,12 @@ MR_API_URL = "https://api.modrinth.com/v2" # DO NOT USE THIS KEY FOR YOUR OWN PROJECT/FORKS -# You can get your own key at https://docs.curseforge.com CF_KEY = '$2a$10$GiT8VjJE8VJpcK68Wlz6aeJ5CPAZcRuTBcGuys8XtX5hGC87sIgku' -CF_HEADERS = {'User-Agent': f"TheBossMagnus/ModpackChangelogger/{VERSION} (thebossmagnus@proton.me)", - 'x-api-key': f"{CF_KEY}"} -CF_API_URL = "https://api.curseforge.com" +# You can get your own key at https://docs.curseforge.com +CF_HEADERS = { + 'x-api-key': CF_KEY, + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } +CF_API_URL = "https://api.curseforge.com" diff --git a/src/get_mod_names.py b/src/get_mod_names.py index b68ea0e..76a3f25 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -39,18 +39,11 @@ async def request_from_api(session, ids): elif constants.Modpacks_Format == "curseforge": URL = f"{CF_API_URL}/v1/mods" - headers = { - 'x-api-key': '$2a$10$GiT8VjJE8VJpcK68Wlz6aeJ5CPAZcRuTBcGuys8XtX5hGC87sIgku', - 'Content-Type': 'application/json', - 'Accept': 'application/json' - } - data = {'modIds': [348521, 238222]} try: - async with session.post(URL, headers=headers, json=data, ssl=False) as response: - response.raise_for_status() - data = await response.json() - names = [project.get('name') for project in data] + async with session.post(URL, headers=CF_HEADERS, json={'modIds': list(ids)}, ssl=False) as response: + response = await response.json() + names = [project['name'] for project in response['data']] except aiohttp.ClientConnectionError as e: logging.warning("Failed to connect to %s: %s", URL, e) except asyncio.TimeoutError: From a175e82ae59af2d59aade00d9c1b61b72a694791 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Sun, 11 Feb 2024 22:19:57 +0100 Subject: [PATCH 17/23] Fix mr requests having cf headers Signed-off-by: TheBossMagnus --- src/get_mod_names.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/get_mod_names.py b/src/get_mod_names.py index 76a3f25..39486a9 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -22,7 +22,7 @@ async def request_from_api(session, ids): URL = f"{MR_API_URL}/projects?ids={ids_list}" try: - async with session.get(URL, headers=CF_HEADERS) as response: + async with session.get(URL, headers=MR_HEADERS) as response: response.raise_for_status() data = await response.json() names = [project.get('title') for project in data] From a632944278478ae0afd42769dc13a5966c101909 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Sun, 11 Feb 2024 22:29:34 +0100 Subject: [PATCH 18/23] split the 2 request sistems Signed-off-by: TheBossMagnus --- src/get_mod_names.py | 94 ++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 43 deletions(-) diff --git a/src/get_mod_names.py b/src/get_mod_names.py index 39486a9..39b36cf 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -7,52 +7,60 @@ async def get_mod_names(added_ids, removed_ids, updated_ids): async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=15)) as session: - added_names, removed_names, updated_names = await asyncio.gather( - request_from_api(session, added_ids), - request_from_api(session, removed_ids), - request_from_api(session, updated_ids) + if constants.Modpacks_Format == "modrinth": + added_names, removed_names, updated_names = await asyncio.gather( + request_from_mr_api(session, added_ids), + request_from_mr_api(session, removed_ids), + request_from_mr_api(session, updated_ids) + ) + else: + added_names, removed_names, updated_names = await asyncio.gather( + request_from_cf_api(session, added_ids), + request_from_cf_api(session, removed_ids), + request_from_cf_api(session, updated_ids) ) + return added_names, removed_names, updated_names -async def request_from_api(session, ids): - # Convert the set of ids to a json array - ids_list = json.dumps(list(ids)) +async def request_from_mr_api(session, ids): + names = [] + URL = f"{MR_API_URL}/projects?ids={json.dumps(list(ids))}" + + try: + async with session.get(URL, headers=MR_HEADERS) as response: + response.raise_for_status() + data = await response.json() + names = [project.get('title') for project in data] + except aiohttp.ClientConnectionError as e: + logging.warning("Failed to connect to %s: %s", URL, e) + except asyncio.TimeoutError: + logging.warning("The request %s timed out", URL) + except aiohttp.ClientResponseError as e: + logging.warning("Server responded with an error for %s: %s", URL, e) + except aiohttp.ClientPayloadError as e: + logging.warning("Failed to read response from %s: %s", URL, e) + except aiohttp.ClientError as e: + logging.warning("An unexpected error occurred: %s", e) + + return names + +async def request_from_cf_api(session, ids): names = [] - if constants.Modpacks_Format == "modrinth": - URL = f"{MR_API_URL}/projects?ids={ids_list}" - - try: - async with session.get(URL, headers=MR_HEADERS) as response: - response.raise_for_status() - data = await response.json() - names = [project.get('title') for project in data] - except aiohttp.ClientConnectionError as e: - logging.warning("Failed to connect to %s: %s", URL, e) - except asyncio.TimeoutError: - logging.warning("The request %s timed out", URL) - except aiohttp.ClientResponseError as e: - logging.warning("Server responded with an error for %s: %s", URL, e) - except aiohttp.ClientPayloadError as e: - logging.warning("Failed to read response from %s: %s", URL, e) - except aiohttp.ClientError as e: - logging.warning("An unexpected error occurred: %s", e) - - elif constants.Modpacks_Format == "curseforge": - URL = f"{CF_API_URL}/v1/mods" - - try: - async with session.post(URL, headers=CF_HEADERS, json={'modIds': list(ids)}, ssl=False) as response: - response = await response.json() - names = [project['name'] for project in response['data']] - except aiohttp.ClientConnectionError as e: - logging.warning("Failed to connect to %s: %s", URL, e) - except asyncio.TimeoutError: - logging.warning("The request %s timed out", URL) - except aiohttp.ClientResponseError as e: - logging.warning("Server responded with an error for %s: %s", URL, e) - except aiohttp.ClientPayloadError as e: - logging.warning("Failed to read response from %s: %s", URL, e) - except aiohttp.ClientError as e: - logging.warning("An unexpected error occurred: %s", e) + URL = f"{CF_API_URL}/v1/mods" + + try: + async with session.post(URL, headers=CF_HEADERS, json={'modIds': list(ids)}, ssl=False) as response: + response = await response.json() + names = [project['name'] for project in response['data']] + except aiohttp.ClientConnectionError as e: + logging.warning("Failed to connect to %s: %s", URL, e) + except asyncio.TimeoutError: + logging.warning("The request %s timed out", URL) + except aiohttp.ClientResponseError as e: + logging.warning("Server responded with an error for %s: %s", URL, e) + except aiohttp.ClientPayloadError as e: + logging.warning("Failed to read response from %s: %s", URL, e) + except aiohttp.ClientError as e: + logging.warning("An unexpected error occurred: %s", e) return names \ No newline at end of file From e64848ec6ba2af50b8e0a08b10138a8a937e19cf Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Sun, 11 Feb 2024 22:36:16 +0100 Subject: [PATCH 19/23] Use a map to slect the correct function Signed-off-by: TheBossMagnus --- src/get_mod_names.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/get_mod_names.py b/src/get_mod_names.py index 39b36cf..dfbb4f4 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -7,17 +7,15 @@ async def get_mod_names(added_ids, removed_ids, updated_ids): async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=15)) as session: - if constants.Modpacks_Format == "modrinth": - added_names, removed_names, updated_names = await asyncio.gather( - request_from_mr_api(session, added_ids), - request_from_mr_api(session, removed_ids), - request_from_mr_api(session, updated_ids) - ) - else: - added_names, removed_names, updated_names = await asyncio.gather( - request_from_cf_api(session, added_ids), - request_from_cf_api(session, removed_ids), - request_from_cf_api(session, updated_ids) + api_function = { + "modrinth": request_from_mr_api, + "curseforge": request_from_cf_api + }.get(constants.Modpacks_Format, request_from_cf_api) + + added_names, removed_names, updated_names = await asyncio.gather( + api_function(session, added_ids), + api_function(session, removed_ids), + api_function(session, updated_ids) ) return added_names, removed_names, updated_names From a83a4132ee3507b931d4f91ef762ba0439e2dcbd Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Sun, 11 Feb 2024 22:42:32 +0100 Subject: [PATCH 20/23] refactors Signed-off-by: TheBossMagnus --- src/get_mod_names.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/get_mod_names.py b/src/get_mod_names.py index dfbb4f4..0ea8b4e 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -35,8 +35,6 @@ async def request_from_mr_api(session, ids): logging.warning("The request %s timed out", URL) except aiohttp.ClientResponseError as e: logging.warning("Server responded with an error for %s: %s", URL, e) - except aiohttp.ClientPayloadError as e: - logging.warning("Failed to read response from %s: %s", URL, e) except aiohttp.ClientError as e: logging.warning("An unexpected error occurred: %s", e) @@ -47,17 +45,15 @@ async def request_from_cf_api(session, ids): URL = f"{CF_API_URL}/v1/mods" try: - async with session.post(URL, headers=CF_HEADERS, json={'modIds': list(ids)}, ssl=False) as response: - response = await response.json() - names = [project['name'] for project in response['data']] + async with session.post(URL, headers=CF_HEADERS, json={'modIds': list(ids)}, ssl=False) as response: + response = await response.json() + names = [project['name'] for project in response['data']] except aiohttp.ClientConnectionError as e: logging.warning("Failed to connect to %s: %s", URL, e) except asyncio.TimeoutError: logging.warning("The request %s timed out", URL) except aiohttp.ClientResponseError as e: logging.warning("Server responded with an error for %s: %s", URL, e) - except aiohttp.ClientPayloadError as e: - logging.warning("Failed to read response from %s: %s", URL, e) except aiohttp.ClientError as e: logging.warning("An unexpected error occurred: %s", e) From 8ffeb1884ee2dc95207dcc9a5cc0c4713791cc39 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Sun, 11 Feb 2024 22:45:16 +0100 Subject: [PATCH 21/23] Specify witch server gave the error Signed-off-by: TheBossMagnus --- src/get_mod_names.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/get_mod_names.py b/src/get_mod_names.py index 0ea8b4e..e24f2e9 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -36,7 +36,7 @@ async def request_from_mr_api(session, ids): except aiohttp.ClientResponseError as e: logging.warning("Server responded with an error for %s: %s", URL, e) except aiohttp.ClientError as e: - logging.warning("An unexpected error occurred: %s", e) + logging.warning("An unexpected error occurred connecting to Modrinth: %s", e) return names @@ -55,6 +55,6 @@ async def request_from_cf_api(session, ids): except aiohttp.ClientResponseError as e: logging.warning("Server responded with an error for %s: %s", URL, e) except aiohttp.ClientError as e: - logging.warning("An unexpected error occurred: %s", e) + logging.warning("An unexpected error occurred connecting to CurseForge: %s", e) return names \ No newline at end of file From 0490bb336840c6566529d3ef4aa4488732a623cd Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Wed, 14 Feb 2024 11:22:33 +0100 Subject: [PATCH 22/23] Merge the error handling for the 2 apis Signed-off-by: TheBossMagnus --- src/get_mod_names.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/get_mod_names.py b/src/get_mod_names.py index e24f2e9..2fc5ad9 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -6,7 +6,7 @@ import constants async def get_mod_names(added_ids, removed_ids, updated_ids): - async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=15)) as session: + async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=0.1)) as session: api_function = { "modrinth": request_from_mr_api, "curseforge": request_from_cf_api @@ -29,14 +29,8 @@ async def request_from_mr_api(session, ids): response.raise_for_status() data = await response.json() names = [project.get('title') for project in data] - except aiohttp.ClientConnectionError as e: - logging.warning("Failed to connect to %s: %s", URL, e) - except asyncio.TimeoutError: - logging.warning("The request %s timed out", URL) - except aiohttp.ClientResponseError as e: - logging.warning("Server responded with an error for %s: %s", URL, e) - except aiohttp.ClientError as e: - logging.warning("An unexpected error occurred connecting to Modrinth: %s", e) + except (aiohttp.ClientConnectionError, asyncio.TimeoutError, aiohttp.ClientResponseError) as e: + handle_request_errors(e, URL) return names @@ -48,13 +42,17 @@ async def request_from_cf_api(session, ids): async with session.post(URL, headers=CF_HEADERS, json={'modIds': list(ids)}, ssl=False) as response: response = await response.json() names = [project['name'] for project in response['data']] - except aiohttp.ClientConnectionError as e: + except (aiohttp.ClientConnectionError, asyncio.TimeoutError, aiohttp.ClientResponseError) as e: + handle_request_errors(e, URL) + + return names + +def handle_request_errors(e, URL): + if isinstance(e, aiohttp.ClientConnectionError): logging.warning("Failed to connect to %s: %s", URL, e) - except asyncio.TimeoutError: + elif isinstance(e, asyncio.TimeoutError): logging.warning("The request %s timed out", URL) - except aiohttp.ClientResponseError as e: + elif isinstance(e, aiohttp.ClientResponseError): logging.warning("Server responded with an error for %s: %s", URL, e) - except aiohttp.ClientError as e: - logging.warning("An unexpected error occurred connecting to CurseForge: %s", e) - - return names \ No newline at end of file + else: + logging.warning("An unexpected error occurred: %s", e) From 0191787ba27233bf84425468bb5e08f24c1839d2 Mon Sep 17 00:00:00 2001 From: TheBossMagnus Date: Wed, 14 Feb 2024 11:23:23 +0100 Subject: [PATCH 23/23] Move arcoded url parts in constants.py Signed-off-by: TheBossMagnus --- src/constants.py | 4 ++-- src/get_mod_names.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/constants.py b/src/constants.py index 0873411..58df990 100644 --- a/src/constants.py +++ b/src/constants.py @@ -19,7 +19,7 @@ } # Networking MR_HEADERS = {'User-Agent':f"TheBossMagnus/ModpackChangelogger/{VERSION} (thebossmagnus@proton.me)"} -MR_API_URL = "https://api.modrinth.com/v2" +MR_API_URL = "https://api.modrinth.com/v2/projects?ids=" # DO NOT USE THIS KEY FOR YOUR OWN PROJECT/FORKS CF_KEY = '$2a$10$GiT8VjJE8VJpcK68Wlz6aeJ5CPAZcRuTBcGuys8XtX5hGC87sIgku' @@ -30,4 +30,4 @@ 'Content-Type': 'application/json', 'Accept': 'application/json' } -CF_API_URL = "https://api.curseforge.com" +CF_API_URL = "https://api.curseforge.com/v1/mods" diff --git a/src/get_mod_names.py b/src/get_mod_names.py index 2fc5ad9..a7b0673 100644 --- a/src/get_mod_names.py +++ b/src/get_mod_names.py @@ -22,7 +22,7 @@ async def get_mod_names(added_ids, removed_ids, updated_ids): async def request_from_mr_api(session, ids): names = [] - URL = f"{MR_API_URL}/projects?ids={json.dumps(list(ids))}" + URL = f"{MR_API_URL}{json.dumps(list(ids))}" try: async with session.get(URL, headers=MR_HEADERS) as response: @@ -36,7 +36,7 @@ async def request_from_mr_api(session, ids): async def request_from_cf_api(session, ids): names = [] - URL = f"{CF_API_URL}/v1/mods" + URL = f"{CF_API_URL}" try: async with session.post(URL, headers=CF_HEADERS, json={'modIds': list(ids)}, ssl=False) as response: