check_mcv.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # https://piston-meta.mojang.com/mc/game/version_manifest.json
  2. import json
  3. import requests
  4. import re
  5. import os
  6. LAUNCHER_MANIFSET_URL = 'https://piston-meta.mojang.com/mc/game/version_manifest.json'
  7. SNAPSHOT_PATTERN = re.compile(r'^[0-9]{2}[w|W][0-9]{2}[A-Fa-f]$')
  8. PRE_RELEASE_PATTERN = re.compile(r'^.*-pre[0-9]+$')
  9. RELEASE_CANDIDATE_PATTERN = re.compile(r'^.*-rc[0-9]+$')
  10. RELEASE_PATTERN = re.compile(r'^1\.[0-9]+(\.[0-9]+)?$')
  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. if os.path.exists(f'{version_libloc}{ver_type}{os.sep}{ver_id}.md'):
  23. print(f'- {ver_id:} 已存在于版本库,无需更改')
  24. try:
  25. response = requests.get(LAUNCHER_MANIFSET_URL)
  26. version_mainfest = json.loads(response.content)
  27. except Exception as e:
  28. print(f'获取版本列表失败:{e.with_traceback()}')
  29. exit(1)
  30. latest_snapshot_id = version_mainfest['latest']['snapshot']
  31. latest_release_id = version_mainfest['latest']['release']
  32. latest_snapshot_type = get_version_type(latest_snapshot_id)
  33. latest_release_type = get_version_type(latest_release_id)
  34. print(f'● 最新 {latest_release_type} 版: {latest_release_id}')
  35. print(f'● 最新 {latest_snapshot_type} 版: {latest_snapshot_id}')
  36. dir_location = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  37. version_lib_location = f'{dir_location}{os.sep}Libraries{os.sep}Versions{os.sep}'
  38. update_library(version_lib_location,latest_release_type,latest_release_id)
  39. update_library(version_lib_location,latest_snapshot_type,latest_snapshot_id)