Skip to content

Commit f797b37

Browse files
committed
feat: refresh curseforge categories
1 parent 461296f commit f797b37

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

models/database/curseforge.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,24 @@ class Fingerprint(Model):
206206
model_config = {
207207
"collection": "curseforge_fingerprints",
208208
}
209+
210+
211+
class Category(Model):
212+
id: int = Field(primary_field=True, index=True)
213+
gameId: int
214+
name: str
215+
slug: str
216+
url: str
217+
iconUrl: str
218+
dateModified: str
219+
isClass: Optional[bool] = None
220+
classId: Optional[int] = None
221+
parentCategoryId: Optional[int] = None
222+
displayIndex: int
223+
224+
sync_at: datetime = Field(default_factory=datetime.utcnow)
225+
226+
model_config = {
227+
"collection": "curseforge_categories",
228+
"title": "CurseForge Category",
229+
}

sync/curseforge.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from odmantic import query
33
import time
44

5-
from models.database.curseforge import File, Mod, Pagination, Fingerprint
5+
from models.database.curseforge import File, Mod, Pagination, Fingerprint, Category
66
from models.database.file_cdn import File as FileCDN
77
from models import ProjectDetail
88
from utils.network import request
@@ -191,3 +191,15 @@ def fetch_mutil_fingerprints(fingerprints: List[int]):
191191
except Exception as e:
192192
log.error(f"Failed to fetch mutil fingerprints info: {e}")
193193
return []
194+
195+
def sync_categories() -> List[dict]:
196+
try:
197+
res = request(f"{API}/v1/categories", headers=HEADERS).json()["data"]
198+
models = []
199+
for category in res:
200+
models.append(Category(**category))
201+
submit_models(models=res)
202+
return res
203+
except Exception as e:
204+
log.error(f"Failed to sync categories: {e}")
205+
return []

sync/tasks.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
StatisticsNotification,
1010
)
1111
from config import Config
12-
from sync.curseforge import sync_mod, sync_mod_all_files
13-
from sync.modrinth import sync_project, sync_project_all_version
12+
from sync.curseforge import sync_mod, sync_categories
13+
from sync.modrinth import sync_project
1414
from sync.check import (
1515
# fetch_all_curseforge_data,
1616
# fetch_all_modrinth_data,
@@ -366,9 +366,15 @@ def sync_modrinth_queue() -> bool:
366366
return True
367367

368368

369+
def refresh_curseforge_categories() -> bool:
370+
log.info("Start fetching curseforge categories.")
371+
result = sync_categories()
372+
log.info(f"CurseForge categories sync finished, total categories: {len(result)}")
373+
return True
374+
369375
def send_statistics_to_telegram() -> bool:
370376
log.info("Start fetching statistics to telegram.")
371377
message = StatisticsNotification.send_to_telegram()
372378
log.info("Statistics message sent to telegram.")
373379
# log.info(f"Statistics message: {message}")
374-
return True
380+
return True

tests/test_sync.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from sync.modrinth import sync_project
2-
from sync.curseforge import sync_mod
2+
from sync.curseforge import sync_mod, sync_categories
33
from models import ProjectDetail
4+
from models.database.curseforge import Category
45

56
project_id = "OpqpD8K2"
67
modId = 1052133
@@ -11,4 +12,9 @@ def test_sync_project():
1112

1213
def test_sync_mod():
1314
result = sync_mod(modId)
14-
assert isinstance(result, ProjectDetail)
15+
assert isinstance(result, ProjectDetail)
16+
17+
def test_sync_categories():
18+
result = sync_categories()
19+
assert isinstance(result, list)
20+
assert all(isinstance(item, Category) for item in result)

tests/test_tasks.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
sync_curseforge_queue,
44
refresh_modrinth_with_modify_date,
55
refresh_curseforge_with_modify_date,
6+
refresh_curseforge_categories,
67
# send_statistics_to_telegram
78
)
89

@@ -16,4 +17,7 @@ def test_refresh_modrinth_with_modify_date():
1617
assert refresh_modrinth_with_modify_date()
1718

1819
def test_refresh_curseforge_with_modify_date():
19-
assert refresh_curseforge_with_modify_date()
20+
assert refresh_curseforge_with_modify_date()
21+
22+
def test_refresh_curseforge_categories():
23+
assert refresh_curseforge_categories()

0 commit comments

Comments
 (0)