check_mcv.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import json
  2. import requests
  3. import re
  4. import os
  5. LAUNCHER_MANIFSET_URL = 'https://piston-meta.mojang.com/mc/game/version_manifest.json'
  6. SNAPSHOT_PATTERN = re.compile(r'^[0-9]{2}[w|W][0-9]{2}[A-Fa-f]$')
  7. PRE_RELEASE_PATTERN = re.compile(r'^.*-pre[0-9]+$')
  8. RELEASE_CANDIDATE_PATTERN = re.compile(r'^.*-rc[0-9]+$')
  9. RELEASE_PATTERN = re.compile(r'^1\.[0-9]+(\.[0-9]+)?$')
  10. INIT_MARKDOWN = '---\nversion-image-link: https://exapmle.com\nnot_finished: true\n---\n'
  11. def get_version_type(vid:str):
  12. if re.match(SNAPSHOT_PATTERN,vid):
  13. return 'Snapshot'
  14. if re.match(PRE_RELEASE_PATTERN,vid):
  15. return 'Pre-Release'
  16. if re.match(RELEASE_CANDIDATE_PATTERN,vid):
  17. return 'Release-Candidate'
  18. if re.match(RELEASE_PATTERN,vid):
  19. return 'Release'
  20. return ''
  21. def update_library(version_libloc:str,ver_type:str,ver_id:str):
  22. filepath = f'{version_libloc}{ver_type}{os.sep}{ver_id}.md'
  23. if os.path.exists(filepath):
  24. print(f'- {ver_id:} 已存在于版本库,无需更改')
  25. else:
  26. with open(filepath, "w") as file:
  27. file.write(INIT_MARKDOWN)
  28. try:
  29. response = requests.get(LAUNCHER_MANIFSET_URL)
  30. version_mainfest = json.loads(response.content)
  31. except Exception as e:
  32. print(f'获取版本列表失败:{e.with_traceback()}')
  33. exit(1)
  34. latest_snapshot_id = version_mainfest['latest']['snapshot']
  35. latest_release_id = version_mainfest['latest']['release']
  36. latest_snapshot_type = get_version_type(latest_snapshot_id)
  37. latest_release_type = get_version_type(latest_release_id)
  38. print(f'● 最新 {latest_release_type} 版: {latest_release_id}')
  39. print(f'● 最新 {latest_snapshot_type} 版: {latest_snapshot_id}')
  40. dir_location = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  41. version_lib_location = f'{dir_location}{os.sep}Libraries{os.sep}Versions{os.sep}'
  42. update_library(version_lib_location,latest_release_type,latest_release_id)
  43. update_library(version_lib_location,latest_snapshot_type,latest_snapshot_id)