Skip to content

Commit

Permalink
Merge branch 'cf-support' of https://github.com/TheBossMagnus/Modpack…
Browse files Browse the repository at this point in the history
…Changelogger into cf-support
  • Loading branch information
TheBossMagnus committed Feb 4, 2024
2 parents ec99a5c + 9a95d63 commit 3f0b295
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 43 deletions.
16 changes: 16 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changleog
All notable changes to this project will be documented in this file. Betas won't be included.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0](https://github.com/TheBossMagnus/ModpackChangelogger/releases/tag/0.2.0) - 2024-01-30
### Added
* Adde the option to print changelog to console (just do -f console)
### Fixed
* Fixed a crash when running as a module
### Changed
* Improve reliability and performance of web requests
* Various refactorings and improvements

## [0.1.0](https://github.com/TheBossMagnus/ModpackChangelogger/releases/tag/0.1.0) - 2023-12-24
First Release
14 changes: 11 additions & 3 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Mod ecosistem used (modrinth or curseforge)
Modpacks_Format = None
# Version number
VERSION = "0.2.0-beta2"
VERSION = "0.2.0"
# Default config
DEFAULT_CONFIG = {
'check': {
Expand All @@ -18,5 +18,13 @@
},
}
# Networking
HEADERS = {'User-Agent':f"TheBossMagnus/ModpackChangelogger/{VERSION} ([email protected])"}
MODRINTH_API_URL = "https://api.modrinth.com/v2"
MR_HEADERS = {'User-Agent':f"TheBossMagnus/ModpackChangelogger/{VERSION} ([email protected])"}
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} ([email protected])",
'x-api-key': f"{CF_KEY}"}
CF_API_URL = "https://api.curseforge.com"

32 changes: 9 additions & 23 deletions src/extract_pack_data.py
Original file line number Diff line number Diff line change
@@ -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}")
Expand Down Expand Up @@ -30,37 +28,25 @@ 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 {
'mc_version': json['minecraft']['version'],
'loader': loader_string.split('-')[0],
'loader_version': loader_string.split('-')[1]
}


def analyze_json_files(old_json, new_json):
def get_mod_ids(json):
# Extracts the file and project IDs from the JSON
return {item['fileID']: item['projectID'] for item in json['files']}

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']}
old_file_ids = get_mod_ids(old_json)
new_file_ids = get_mod_ids(new_json)

unique_old_project_ids = set()
unique_new_project_ids = set()
# 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}

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 old_ids, new_ids, old_info, new_info
61 changes: 44 additions & 17 deletions src/get_mod_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 5 in src/get_mod_names.py

View check run for this annotation

Codeac.io / Codeac Code Quality

unused-import

Unused MR_HEADERS imported from constants
import constants

async def get_mod_names(added_ids, removed_ids, updated_ids):
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=15)) as session:
Expand All @@ -16,23 +17,49 @@ async def get_mod_names(added_ids, removed_ids, updated_ids):
async def request_from_api(session, ids):

Check warning on line 17 in src/get_mod_names.py

View check run for this annotation

Codeac.io / Codeac Code Quality

too-complex

'request_from_api' is too complex. The McCabe rating is 15
# 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"

Check warning on line 41 in src/get_mod_names.py

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 13 lines is too similar to src/get_mod_names.py:53
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]
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

0 comments on commit 3f0b295

Please sign in to comment.