minecraft_version.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. 获取 MC 版本信息
  3. """
  4. from homepagebuilder.interfaces import Logger
  5. from homepagebuilder.core.config import config
  6. from typing import TYPE_CHECKING
  7. import requests
  8. import json
  9. import re
  10. if TYPE_CHECKING:
  11. from homepagebuilder.core.types import Context
  12. LAUNCHER_MANIFSET_URL = 'https://piston-meta.mojang.com/mc/game/version_manifest.json'
  13. MANIFSET = None
  14. VERSIONS_DICT = None
  15. FULL_VERSIONS = None
  16. LATEST_VERSIONS = None
  17. logger = Logger("VersionScript")
  18. def fetch_manifset():
  19. logger.info("正在加载 MC 版本列表")
  20. try:
  21. response = requests.get(LAUNCHER_MANIFSET_URL,timeout=10000)
  22. return json.loads(response.content)
  23. except Exception as e:
  24. logger.warning("加载 MC 版本列表失败")
  25. logger.exception(e)
  26. return {}
  27. def get_manifset():
  28. global MANIFSET
  29. if not MANIFSET:
  30. MANIFSET = fetch_manifset()
  31. return MANIFSET
  32. def init_version_dict():
  33. global LATEST_VERSIONS,FULL_VERSIONS,VERSIONS_DICT
  34. VERSIONS_DICT = {}
  35. LATEST_VERSIONS = {'snapshot':None,'release':None}
  36. manifset = get_manifset()
  37. LATEST_VERSIONS.update(manifset['latest'])
  38. FULL_VERSIONS = manifset.get('versions')
  39. for version in FULL_VERSIONS:
  40. VERSIONS_DICT[version['id']] = version
  41. if get_version_type(LATEST_VERSIONS["snapshot"]) == 'release':
  42. LATEST_VERSIONS["snapshot"] = None
  43. logger.info(f'最新快照版:{LATEST_VERSIONS.get("snapshot")}; 最新正式版:{LATEST_VERSIONS["release"]}')
  44. def get_latest(version_type = 'all',version_rank = -1):
  45. global LATEST_VERSIONS
  46. if not LATEST_VERSIONS:
  47. init_version_dict()
  48. version_rank = int(version_rank)
  49. if version_rank <= 0:
  50. if(version_type == 'all'):
  51. if latest_snapshot := LATEST_VERSIONS.get('snapshot'):
  52. return latest_snapshot
  53. else:
  54. return LATEST_VERSIONS['release']
  55. return LATEST_VERSIONS.get(version_type)
  56. count = version_rank
  57. for version in FULL_VERSIONS:
  58. if version['type'] == version_type:
  59. count -= 1
  60. if count == 0:
  61. return version['id']
  62. return ''
  63. def get_server_jar(version_id):
  64. global VERSIONS_DICT
  65. if not (ver_info := VERSIONS_DICT.get(version_id)):
  66. return None
  67. response = requests.get(ver_info['url'],timeout=10000)
  68. return json.loads(response.content)['downloads']['server']['url']
  69. SNAPSHOT_PATTERN = re.compile(r'^[0-9]{2}[w|W][0-9]{2}[A-Fa-f]$')
  70. PRE_RELEASE_PATTERN = re.compile(r'^.*-pre[0-9]+$')
  71. RELEASE_CANDIDATE_PATTERN = re.compile(r'^.*-rc[0-9]+$')
  72. RELEASE_PATTERN = re.compile(r'^1\.[0-9]+(\.[0-9]+)?$')
  73. def get_version_type(version_id):
  74. '''获取版本类型'''
  75. if re.match(SNAPSHOT_PATTERN,version_id):
  76. return 'snapshot'
  77. if re.match(PRE_RELEASE_PATTERN,version_id):
  78. return 'pre_release'
  79. if re.match(RELEASE_CANDIDATE_PATTERN,version_id):
  80. return 'release_candidate'
  81. if re.match(RELEASE_PATTERN,version_id):
  82. return 'release'
  83. return 'other'
  84. def init(context:'Context', *args, **kwargs):
  85. global MANIFSET
  86. if config('NewsHomepage.MCV.UseReloadCache', config('NewsHomepage.Debug')):
  87. if mcv_manifset_cache := context.builder.get_data('mcv_manifset_cache'):
  88. MANIFSET = mcv_manifset_cache
  89. else:
  90. context.builder.set_data('mcv_manifset_cache', get_manifset())