Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
fix: 🚑️ move api to api.curseforge.com (fix #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jun 26, 2022
1 parent ec1d9af commit 047ab1c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 43 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

感谢 [安逸菌](http://anyijun.com/)[Cyl18](https://github.com/Cyl18)[安逸汉化组](https://github.com/ShaBaiTianCN) 在本软件开发过程中提供的建议和帮助。

### 使用的 API 的文档

[Curse API](https://gist.github.com/crapStone/9a423f7e97e64a301e88a2f6a0f3e4d9)

[CurseforgeAPI/apiary.apib](https://github.com/Gaz492/CurseforgeAPI/blob/master/apiary.apib)

## 许可

Copyright © 2022 Andy Zhang and contributors
Expand Down
8 changes: 4 additions & 4 deletions utils/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class WINDOW:

class SEARCH:
VERSIONS = ['', '1.10.2', '1.12.2', '1.16.5', '1.18.2']
SORT = {
'Name': 3,
'Popularity': 1,
'Total Downloads': 5
SORTING = {
'Name': [4, 'asc'],
'Popularity': [2, 'desc'],
'Total Downloads': [6, 'desc']
}
13 changes: 9 additions & 4 deletions utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,20 @@ def get_download_urls(self, update: Callable):
result = []
i = 0
for r in self.thread_pool.map(
lambda file: Requester.download_url(
file["projectID"],
file["fileID"]
lambda file: Requester.get_mod_file(
file['projectID'],
file['fileID']
), files
):
i += 1
update()
self.logger.info(f'获取模组下载链接({i}/{count})')
result.append(r.text)
data = r.json()['data']
result.append('https://edge.forgecdn.net/files/{}/{}/{}'.format(
int(data['id'] / 1000),
data['id'] % 1000,
data['fileName']
))
return result

def download_mods(self, urls, update: Callable):
Expand Down
34 changes: 15 additions & 19 deletions utils/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from urllib.parse import quote, urlencode
from urllib.request import Request, urlopen

from typing import Any, Dict
from typing import Any, Dict, List


class Response:
Expand All @@ -29,8 +29,10 @@ def json(self):

class Requester:
HEADERS = {
'user-agent': ''
'Accept': 'application/json',
'x-api-key': '*'
}
BASE_URL = 'https://api.curseforge.com'

@classmethod
def get(cls, url: str, params: Dict[str, Any] = None) -> Response:
Expand All @@ -51,43 +53,39 @@ def get(cls, url: str, params: Dict[str, Any] = None) -> Response:
return Response(urlopen(request))

@classmethod
def download_url(cls, project_id, file_id):
return cls.get(
f'https://addons-ecs.forgesvc.net/api/v2/addon/'
f'{project_id}/file/{file_id}/download-url'
)
def get_mod_file(cls, project_id, file_id):
return cls.get(f'{cls.BASE_URL}/v1/mods/{project_id}/files/{file_id}')

@classmethod
def search_modpack(
cls,
game_version: str = None,
search_filter: str = None,
sort: int = None,
sorting: List = None,
index: int = 0
):
"""
Search modpacks.
:param game_version: Game version string.
:param sort: Sorting rule.
:param search_filter: Filter to search, a string.
:param sorting: Sorting rule, a list. First number is sort field and
second string is order.
:param index: Page index.
:return: Response.
"""
params = {
'gameId': 432,
'sectionId': 4471,
'classId': 4471,
'index': index
}
if game_version:
params['gameVersion'] = game_version
if search_filter:
params['searchFilter'] = search_filter
if sort:
params['sort'] = sort
return cls.get(
'https://addons-ecs.forgesvc.net/api/v2/addon/search',
params=params
)
if sorting:
params['sortField'] = sorting[0]
params['sortOrder'] = sorting[1]
return cls.get(f'{cls.BASE_URL}/v1/mods/search', params=params)

@classmethod
def files(cls, _id: int):
Expand All @@ -96,6 +94,4 @@ def files(cls, _id: int):
:param _id: Modpack ID.
:return: Response.
"""
return cls.get(
f'https://addons-ecs.forgesvc.net/api/v2/addon/{_id}/files'
)
return cls.get(f'{cls.BASE_URL}/v1/mods/{_id}/files')
2 changes: 1 addition & 1 deletion utils/window/frames/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def modpack_version(self):
return self.modpack_version_combobox.get()

def init(self):
self.sort_combobox['values'] = list(SEARCH.SORT.keys())
self.sort_combobox['values'] = list(SEARCH.SORTING.keys())
self.sort_combobox.current(1)
self.game_version_combobox['values'] = SEARCH.VERSIONS

Expand Down
26 changes: 17 additions & 9 deletions utils/window/frames/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ def __init__(self, master: 'Main'):
def update_list(self, append=False):
def request(index):
# Get filters
keyword = sort = game_version = None
keyword = sorting = game_version = None
if self.__main_window.search_frame.keyword:
keyword = self.__main_window.search_frame.keyword
if self.__main_window.filters_frame.sort:
sort = SEARCH.SORT[self.__main_window.filters_frame.sort]
sorting = SEARCH.SORTING[self.__main_window.filters_frame.sort]
if self.__main_window.filters_frame.game_version:
game_version = self.__main_window.filters_frame.game_version

return Requester.search_modpack(
game_version=game_version,
search_filter=keyword,
sort=sort,
sorting=sorting,
index=index
).json()

Expand All @@ -63,7 +63,7 @@ def run():
self.__search_index += 1

# Request and check result
result = request(self.__search_index)
result = request(self.__search_index)['data']
if len(result) == 0:
showwarning('提示', '共搜索到 0 个结果!')

Expand Down Expand Up @@ -98,7 +98,7 @@ def run():
self.__main_window.filters_frame.set_modpack_version([''])

# Get files
files = Requester.files(_id).json()
files = Requester.files(project_id).json()['data']

# Storage into data
self.__data[self.selected_index]['files'] = {}
Expand All @@ -119,8 +119,11 @@ def run():
if self.selected_index == old_index:
return
else:
_id = self.__data[self.selected_index].get('id')
Thread(target=run, name=f'Get Modpack Versions ({_id})').start()
project_id = self.__data[self.selected_index].get('id')
Thread(
target=run,
name=f'Get Modpack Files ({project_id})'
).start()

@property
def selected_index(self) -> int:
Expand All @@ -147,14 +150,19 @@ def selected_file_name(self) -> str:
def selected_download_url(self) -> str:
display_name = self.__main_window.filters_frame.modpack_version
if display_name != '' and self.selected_files is not None:
return self.selected_files[display_name]['downloadUrl']
selected_file_id = self.selected_files[display_name]['id']
return 'https://edge.forgecdn.net/files/{}/{}/{}'.format(
int(selected_file_id / 1000),
selected_file_id % 1000,
self.selected_file_name
)
return ''

@property
def selected_avatar_url(self) -> str:
display_name = self.__main_window.filters_frame.modpack_version
if display_name != '' and self.selected_files is not None:
return self.__data[self.selected_index]['attachments'][0]['url']
return self.__data[self.selected_index]['logo']['url']
return ''

def reselect(self):
Expand Down

0 comments on commit 047ab1c

Please sign in to comment.