Skip to content

Instantly share code, notes, and snippets.

@zkitefly
Last active January 29, 2024 15:36
Show Gist options
  • Save zkitefly/9e789c934a8cc7847330674432babfd0 to your computer and use it in GitHub Desktop.
Save zkitefly/9e789c934a8cc7847330674432babfd0 to your computer and use it in GitHub Desktop.
version.py
# https://gist.github.com/zkitefly/9e789c934a8cc7847330674432babfd0
import json
import os
import requests
# 读取版本列表和版本清单文件的URL
# test url: https://cdn.crashmc.com/https://gist.github.com/zkitefly/9e789c934a8cc7847330674432babfd0/raw/66a580349159aa26a405eb02f1dcbf6a16efeb15/test-versions.txt
versions_txt_url = "https://raw.githubusercontent.com/HMCL-dev/HMCL/javafx/HMCLCore/src/main/resources/assets/game/versions.txt"
version_manifest_url = "https://launchermeta.mojang.com/mc/game/version_manifest.json"
local_versions_txt = "versions.txt"
# 下载文件并保存到本地
def download_file(url, filename):
response = requests.get(url)
if response.status_code == 200:
with open(filename, 'wb') as f:
f.write(response.content)
print(f"文件 {filename} 下载完成。")
else:
print(f"下载 {filename} 文件失败。")
# 读取版本清单文件中的版本id
def read_version_manifest():
response = requests.get(version_manifest_url)
if response.status_code == 200:
manifest_data = json.loads(response.text)
versions = [version['id'] for version in manifest_data['versions']]
return versions
else:
print("无法读取版本清单文件。")
return None
# 读取版本列表文件中的最后一行文本
def read_last_line_from_versions_txt(filename):
try:
with open(filename, 'r') as f:
lines = f.readlines()
if len(lines) > 0:
last_line = lines[-1].strip()
else:
last_line = None
return last_line
except FileNotFoundError:
print(f"{filename} 文件不存在。")
return None
# 检查是否有新的版本id,并更新版本列表文件
def check_and_update_versions(versions_txt, new_versions):
last_version = read_last_line_from_versions_txt(versions_txt)
if last_version is None:
return
if last_version not in new_versions:
print(f"找不到id: {last_version}")
return
new_ids = []
found_last_version = False
for version in new_versions:
if found_last_version:
new_ids.append(version)
if version == last_version:
found_last_version = True
if not new_ids:
print("没有新的id。")
return
with open(versions_txt, 'a') as f:
print("新增的id(已按照从下至上版本号大至小的排序): \n\n")
for new_id in new_ids:
# f.write(f"\n{new_id}")
print(f"{new_id}")
print("\n")
# print(f"版本列表已更新并保存到 {versions_txt}。")
# 删除版本列表文件中最后一行的换行符
def remove_last_line_break(filename):
try:
with open(filename, 'r') as f:
lines = f.readlines()
with open(filename, 'w') as f:
f.writelines(lines[:-1])
print(f"{filename} 文件中最后一行的换行符已删除。")
except FileNotFoundError:
print(f"{filename} 文件不存在。")
# 主函数
def main():
download_file(versions_txt_url, local_versions_txt)
new_versions = read_version_manifest()
if new_versions:
new_versions.reverse() # 将版本清单反转,确保从第一个开始读取
# remove_last_line_break(local_versions_txt)
check_and_update_versions(local_versions_txt, new_versions)
if __name__ == "__main__":
main()
1.0
11w47a
11w48a
11w49a
11w50a
12w01a
1.1
12w03a
12w04a
12w05a
12w05b
12w06a
12w07a
12w07b
12w08a
1.2
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
12w15a
12w16a
12w17a
12w18a
12w19a
12w21a
12w21b
12w22a
12w23a
12w23b
12w24a
12w25a
12w26a
12w27a
12w30a
12w30b
12w30c
12w30d
12w30e
1.3
1.3.1
1.3.2
12w32a
12w34a
12w34b
12w36a
12w37a
12w38a
12w38b
12w39a
12w39b
12w40a
12w40b
12w41a
12w41b
12w42a
12w42b
1.4
1.4.1
1.4.2
1.4.3
1.4.4
12w49a
12w50a
12w50b
1.4.5
1.4.6
1.4.7
13w01a
13w01b
13w02a
13w02b
13w03a
13w04a
13w05a
13w05b
13w06a
13w07a
13w09a
13w09b
13w09c
13w10a
13w10b
1.5
13w11a
13w12~
1.5.1
2.0
1.5.2
13w16a
13w16b
13w17a
13w18a
13w18b
13w18c
13w19a
13w21a
13w21b
13w22a
13w23a
13w23b
13w24a
13w24b
13w25a
13w25b
13w25c
13w26a
1.6
1.6.1
1.6.2
1.6.3
1.6.4
13w36a
13w36b
13w37a
13w37b
13w38a
13w38b
13w38c
13w39a
13w39b
13w41a
13w41b
13w42a
13w42b
13w43a
1.7
1.7.1
1.7.2
13w47a
13w47b
13w47c
13w47d
13w47e
13w48a
13w48b
13w49a
1.7.3
1.7.4
1.7.5
1.7.6-pre1
1.7.6-pre2
1.7.6
1.7.7
1.7.8
1.7.9
1.7.10-pre1
1.7.10-pre2
1.7.10-pre3
1.7.10-pre4
1.7.10
14w02a
14w02b
14w02c
14w03a
14w03b
14w04a
14w04b
14w05a
14w05b
14w06a
14w06b
14w07a
14w08a
14w10a
14w10b
14w10c
14w11a
14w11b
14w17a
14w18a
14w18b
14w19a
14w20a
14w20b
14w21a
14w21b
14w25a
14w25b
14w26a
14w26b
14w26c
14w27a
14w27b
14w28a
14w28b
14w29a
14w29b
14w30a
14w30b
14w30c
14w31a
14w32a
14w32b
14w32c
14w32d
14w33a
14w33b
14w33c
14w34a
14w34b
14w34c
14w34d
1.8-pre1
1.8-pre2
1.8-pre3
1.8
1.8.1-pre1
1.8.1-pre2
1.8.1-pre3
1.8.1-pre4
1.8.1-pre5
1.8.1
1.8.2-pre1
1.8.2-pre2
1.8.2-pre3
1.8.2-pre4
1.8.2-pre5
1.8.2-pre6
1.8.2-pre7
1.8.2
1.8.3
15w14a
1.8.4
1.8.5
1.8.6
1.8.7
1.8.8
1.8.9
15w31a
15w31b
15w31c
15w32a
15w32b
15w32c
15w33a
15w33b
15w33c
15w34a
15w34b
15w34c
15w34d
15w35a
15w35b
15w35c
15w35d
15w35e
15w36a
15w36b
15w36c
15w36d
15w37a
15w38a
15w38b
15w39a
15w39b
15w39c
15w40a
15w40b
15w41a
15w41b
15w42a
15w43a
15w43b
15w43c
15w44a
15w44b
15w45a
15w46a
15w47a
15w47b
15w47c
15w49a
15w49b
15w50a
15w51a
15w51b
16w02a
16w03a
16w04a
16w05a
16w05b
16w06a
16w07a
16w07b
1.9-pre1
1.9-pre2
1.9-pre3
1.9-pre4
1.9
1.9.1-pre1
1.9.1-pre2
1.9.1-pre3
1.9.1
1.9.2
1.RV-Pre1
16w14a
16w15a
16w15b
1.9.3-pre1
1.9.3-pre2
1.9.3-pre3
1.9.3
1.9.4
16w20a
16w21a
16w21b
1.10-pre1
1.10-pre2
1.10
1.10.1
1.10.2
16w32a
16w32b
16w33a
16w35a
16w36a
16w38a
16w39a
16w39b
16w39c
16w40a
16w41a
16w42a
16w43a
16w44a
1.11-pre1
1.11
16w50a
1.11.1
1.11.2
17w06a
17w13a
17w13b
17w14a
17w15a
17w16a
17w16b
17w17a
17w17b
17w18a
17w18b
1.12-pre1
1.12-pre2
1.12-pre3
1.12-pre4
1.12-pre5
1.12-pre6
1.12-pre7
1.12
17w31a
1.12.1-pre1
1.12.1
1.12.2-pre1
1.12.2-pre2
1.12.2
17w43a
17w43b
17w45a
17w45b
17w46a
17w47a
17w47b
17w48a
17w49a
17w49b
17w50a
18w01a
18w02a
18w03a
18w03b
18w05a
18w06a
18w07a
18w07b
18w07c
18w08a
18w08b
18w09a
18w10a
18w10b
18w10c
18w10d
18w11a
18w14a
18w14b
18w15a
18w16a
18w19a
18w19b
18w20a
18w20b
18w20c
18w21a
18w21b
18w22a
18w22b
18w22c
1.13-pre1
1.13-pre2
1.13-pre3
1.13-pre4
1.13-pre5
1.13-pre6
1.13-pre7
1.13-pre8
1.13-pre9
1.13-pre10
1.13
18w30a
18w30b
18w31a
18w32a
18w33a
1.13.1-pre1
1.13.1-pre2
1.13.1
1.13.2-pre1
1.13.2-pre2
1.13.2
18w43a
18w43b
18w43c
18w44a
18w45a
18w46a
18w47a
18w47b
18w48a
18w48b
18w49a
18w50a
19w02a
19w03a
19w03b
19w03c
19w04a
19w04b
19w05a
19w06a
19w07a
19w08a
19w08b
19w09a
19w11a
19w11b
19w12a
19w12b
19w13a
19w13b
3D Shareware v1.34
19w14a
19w14b
1.14 Pre-Release 1
1.14 Pre-Release 2
1.14 Pre-Release 3
1.14 Pre-Release 4
1.14 Pre-Release 5
1.14
1.14.1 Pre-Release 1
1.14.1 Pre-Release 2
1.14.1
1.14.2 Pre-Release 1
1.14.2 Pre-Release 2
1.14.2 Pre-Release 3
1.14.2 Pre-Release 4
1.14.2
1.14.3-pre1
1.14.3-pre2
1.14.3-pre3
1.14.3-pre4
1.14.3
1.14.4-pre1
1.14.4-pre2
1.14.4-pre3
1.14.4-pre4
1.14.4-pre5
1.14.4-pre6
1.14.4-pre7
1.14.4
19w34a
19w35a
19w36a
19w37a
19w38a
19w38b
19w39a
19w40a
19w41a
19w42a
19w44a
19w45a
19w45b
19w46a
19w46b
1.15-pre1
1.15-pre2
1.15-pre3
1.15-pre4
1.15-pre5
1.15-pre6
1.15-pre7
1.15
1.15.1-pre1
1.15.1
1.15.2-pre1
1.15.2-pre2
1.15.2
20w06a
20w07a
20w08a
20w09a
20w10a
20w11a
20w12a
20w13a
20w13b
20w14infinite
20w14a
20w15a
20w16a
20w17a
20w18a
20w19a
20w20a
20w20b
20w21a
20w22a
1.16-pre1
1.16-pre2
1.16-pre3
1.16-pre4
1.16-pre5
1.16-pre6
1.16-pre7
1.16-pre8
1.16-rc1
1.16
1.16.1
20w27a
20w28a
20w29a
20w30a
1.16.2-pre1
1.16.2-pre2
1.16.2-pre3
1.16.2-rc1
1.16.2-rc2
1.16.2
1.16.3-rc1
1.16.3
1.16.4-pre1
1.16.4-pre2
1.16.4-rc1
1.16.4
1.16.5-rc1
1.16.5
20w45a
20w46a
20w48a
20w49a
20w51a
21w03a
21w05a
21w05b
21w06a
21w07a
21w08a
21w08b
21w10a
21w11a
21w13a
21w14a
21w15a
21w16a
21w17a
21w18a
21w19a
21w20a
1.17-pre1
1.17-pre2
1.17-pre3
1.17-pre4
1.17-pre5
1.17-rc1
1.17-rc2
1.17
1.17.1-pre1
1.17.1-pre2
1.17.1-pre3
1.17.1-rc1
1.17.1-rc2
1.17.1
21w37a
21w38a
21w39a
21w40a
21w41a
21w42a
21w43a
21w44a
1.18-pre1
1.18-pre2
1.18-pre3
1.18-pre4
1.18-pre5
1.18-pre6
1.18-pre7
1.18-pre8
1.18-rc1
1.18-rc2
1.18-rc3
1.18-rc4
1.18
1.18.1-pre1
1.18.1-rc1
1.18.1-rc2
1.18.1-rc3
1.18.1
22w03a
22w05a
22w06a
22w07a
1.18.2-pre1
1.18.2-pre2
1.18.2-pre3
1.18.2-rc1
1.18.2
22w13oneblockatatime
22w11a
22w12a
22w13a
22w14a
22w15a
22w16a
22w16b
22w17a
22w18a
22w19a
1.19-pre1
1.19-pre2
1.19-pre3
1.19-pre4
1.19-pre5
1.19-rc1
1.19-rc2
1.19
22w24a
1.19.1-pre1
1.19.1-pre2
1.19.1-pre3
1.19.1-pre4
1.19.1-pre5
1.19.1-pre6
1.19.1-rc1
1.19.1-rc2
1.19.1-rc3
1.19.1
1.19.2-rc1
1.19.2-rc2
1.19.2
22w42a
22w43a
22w44a
22w45a
22w46a
1.19.3-pre1
1.19.3-pre2
1.19.3-pre3
1.19.3-rc1
1.19.3-rc2
1.19.3-rc3
1.19.3
23w03a
23w04a
23w05a
23w06a
23w07a
1.19.4-pre1
1.19.4-pre2
1.19.4-pre3
1.19.4-pre4
1.19.4-rc1
1.19.4-rc2
1.19.4-rc3
1.19.4
23w12a
23w13a
23w13a_or_b
23w14a
23w16a
23w17a
23w18a
1.20-pre1
1.20-pre2
1.20-pre3
1.20-pre4
1.20-pre5
1.20-pre6
1.20-pre7
1.20-rc1
1.20
1.20.1-rc1
1.20.1
23w31a
23w32a
23w33a
23w35a
1.20.2-pre1
1.20.2-pre2
1.20.2-pre3
1.20.2-pre4
1.20.2-rc1
1.20.2-rc2
1.20.2
23w40a
23w41a
23w42a
23w43a
23w43b
23w44a
23w45a
23w46a
1.20.3-pre1
1.20.3-pre2
1.20.3-pre3
1.20.3-pre4
1.20.3-rc1
1.20.3
1.20.4-rc1
1.20.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment