Skip to content

Commit 639d638

Browse files
committed
feat: Modrinth 增加项目存活状态返回并实现项目删除
1 parent 23dda1f commit 639d638

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

mcim_sync/checker/modrinth.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union, List, Set
1+
from typing import Union, List, Set, Tuple
22
import datetime
33
import time
44

@@ -28,18 +28,27 @@
2828
MODRINTH_DELAY: Union[float, int] = config.modrinth_delay
2929

3030

31-
def check_modrinth_data_updated(projects: List[Project]) -> Set[str]:
31+
def check_modrinth_data_updated_and_alive(projects: List[Project]) -> tuple[set[str], set[str]]:
3232
project_info = {
3333
project.id: {"sync_date": project.updated, "versions": project.versions}
3434
for project in projects
3535
}
3636
expired_project_ids: Set[str] = set()
37-
info = fetch_mutil_projects_info(project_ids=[project.id for project in projects])
37+
db_project_ids = [project.id for project in projects]
38+
alive_project_ids = []
39+
40+
info = fetch_mutil_projects_info(project_ids=db_project_ids)
41+
3842
if info is not None:
3943
with ModelSubmitter() as submitter:
4044
for project in info:
41-
submitter.add(Project(**project))
4245
project_id = project["id"]
46+
47+
# mark as alive
48+
alive_project_ids.append(project_id)
49+
50+
submitter.add(Project(**project))
51+
4352
sync_date: datetime.datetime = project_info[project_id][
4453
"sync_date"
4554
].replace(tzinfo=None)
@@ -61,7 +70,10 @@ def check_modrinth_data_updated(projects: List[Project]) -> Set[str]:
6170
f"Project {project_id} is updated {sync_date.isoformat(timespec='seconds')} -> {updated_date.isoformat(timespec='seconds')}!"
6271
)
6372

64-
return expired_project_ids
73+
# check if project is not alive
74+
not_alive_project_ids = set(db_project_ids) - set(alive_project_ids)
75+
76+
return expired_project_ids, not_alive_project_ids
6577

6678

6779
# check modrinth_project_ids queue

mcim_sync/cleaner/modrinth.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Union, List, Set
2+
3+
from mcim_sync.database.mongodb import sync_mongo_engine, raw_mongo_client
4+
from mcim_sync.utils.loger import log
5+
from mcim_sync.config import Config
6+
7+
config = Config.load()
8+
9+
def remove_project(project_id: str):
10+
project_result = raw_mongo_client["modrinth_projects"].delete_one({"_id": project_id})
11+
version_result = raw_mongo_client["modrinth_versions"].delete_many({"project_id": project_id})
12+
hash_result = raw_mongo_client["modrinth_hashes"].delete_many({"project_id": project_id})
13+
log.debug(f"Remove project {project_id}, {version_result.deleted_count} versions, {hash_result.deleted_count} hashes")
14+
return project_result, version_result, hash_result
15+
16+
def remove_projects(project_ids: List[str]):
17+
result = []
18+
for project_id in project_ids:
19+
singal_result = remove_project(project_id)
20+
if singal_result[0].deleted_count == 0 and singal_result[1].deleted_count == 0 and singal_result[2].deleted_count == 0:
21+
log.debug(f"Can't remove project {project_id}, not found")
22+
continue
23+
result.append({
24+
"project_id": project_id,
25+
"version_count": singal_result[1].deleted_count,
26+
"hash_count": singal_result[2].deleted_count
27+
})
28+
log.debug(f"Remove project {project_id}, {singal_result[1].deleted_count} versions, {singal_result[2].deleted_count} hashes")
29+
return result

mcim_sync/fetcher/modrinth.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from mcim_sync.utils.loger import log
88
from mcim_sync.config import Config
99
from mcim_sync.models.database.modrinth import Project
10-
from mcim_sync.checker.modrinth import check_modrinth_data_updated
10+
from mcim_sync.checker.modrinth import check_modrinth_data_updated_and_alive
1111

1212
config = Config.load()
1313

@@ -70,8 +70,9 @@ def fetch_modrinth_data_by_sync_at():
7070

7171

7272

73-
def fetch_expired_modrinth_data() -> List[str]:
73+
def fetch_expired_and_removed_modrinth_data() -> tuple[List[str], List[str]]:
7474
expired_project_ids = set()
75+
removed_project_ids = set()
7576
skip = 0
7677
while True:
7778
projects_result: List[Project] = list(
@@ -80,9 +81,10 @@ def fetch_expired_modrinth_data() -> List[str]:
8081
if not projects_result:
8182
break
8283
skip += MODRINTH_LIMIT_SIZE
83-
check_expired_result = check_modrinth_data_updated(projects_result)
84+
check_expired_result, not_alive_result = check_modrinth_data_updated_and_alive(projects_result)
8485
expired_project_ids.update(check_expired_result)
85-
log.debug(f"Matched {len(check_expired_result)} expired projects")
86+
removed_project_ids.update(not_alive_result)
87+
log.debug(f"Matched {len(check_expired_result)} expired projects, {len(not_alive_result)} removed projects")
8688
time.sleep(MODRINTH_DELAY)
8789
log.debug(f"Delay {MODRINTH_DELAY} seconds")
88-
return list(expired_project_ids)
90+
return list(expired_project_ids), list(removed_project_ids)

mcim_sync/tasks/modrinth.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
check_modrinth_hashes_available,
2222
check_newest_search_result,
2323
)
24-
from mcim_sync.fetcher.modrinth import fetch_expired_modrinth_data
24+
from mcim_sync.cleaner.modrinth import remove_projects
25+
from mcim_sync.fetcher.modrinth import fetch_expired_and_removed_modrinth_data
2526
from mcim_sync.queues.modrinth import clear_modrinth_all_queues
2627
from mcim_sync.tasks import create_tasks_pool, modrinth_pause_event
2728

@@ -34,11 +35,18 @@ def refresh_modrinth_with_modify_date() -> bool:
3435
log.info("Start fetching expired Modrinth data.")
3536

3637
if config.sync_modrinth:
37-
modrinth_expired_data = fetch_expired_modrinth_data()
38+
modrinth_expired_data, modrinth_removed_data = fetch_expired_and_removed_modrinth_data()
3839

39-
log.info(f"Modrinth expired data fetched: {len(modrinth_expired_data)}")
40-
log.info("Start syncing Modrinth expired data...")
40+
log.info(f"Modrinth expired data fetched: {len(modrinth_expired_data)}, removed data: {len(modrinth_removed_data)}")
41+
42+
# 删除已经源删除的 modrinth 数据
43+
if modrinth_removed_data:
44+
log.info(f"Start removing modrinth data: {len(modrinth_removed_data)}")
45+
remove_projects(modrinth_removed_data)
46+
log.info(f"Removed {len(modrinth_removed_data)} modrinth data.")
4147

48+
# 刷新过期的 modrinth 数据
49+
log.info("Start syncing Modrinth expired data...")
4250
modrinth_pause_event.set()
4351
modrinth_pool, modrinth_futures = create_tasks_pool(
4452
sync_project, # 需要 ProjectDetail 返回值

0 commit comments

Comments
 (0)