-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetLatestPaper.py
77 lines (63 loc) · 2.41 KB
/
getLatestPaper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from urllib import request
import json
from sys import argv
import hashlib
import os
# jarファイルパス
PATH = argv[1]
# バージョン
VERSION = argv[2]
def getBuildObj(Name: str, Version: str):
# 最新のビルド 取得
with request.urlopen(f"https://api.papermc.io/v2/projects/{Name}/versions/{Version}/builds") as response:
data = json.load(response)
item = [item for item in data["builds"]
if item["channel"] == "default"][-1]
BUILD = item["build"]
FILE = item["downloads"]["application"]["name"]
SHA256 = item["downloads"]["application"]["sha256"]
# 最新バージョンの情報を出力
return {
"name": Name,
"version": Version,
"build": BUILD,
"file": FILE,
"sha256": SHA256
}
def getFile_sha256(path: str):
hash = hashlib.sha256()
try:
with open(path, "rb") as f:
# ファイルをチャンクごとに読み込んでハッシュを計算
for byte_block in iter(lambda: f.read(4096), b""):
hash.update(byte_block)
except FileNotFoundError:
return None
# sha256 のハッシュ値を return
return hash.hexdigest()
def downloadLatest(LatestObj: object, path: str):
# URL
URL = f"https://api.papermc.io/v2/projects/{LatestObj['name']}/versions/{LatestObj['version']}/builds/{LatestObj['build']}/downloads/{LatestObj['file']}"
# フォルダが存在しない場合は作成
dir_path = os.path.dirname(path)
if not os.path.exists(dir_path) and not os.path.isfile(path):
os.makedirs(dir_path)
# ファイル ダウンロード
binary = request.urlopen(URL).read()
download_path = os.path.join(dir_path, LatestObj["file"])
with open(download_path, mode="wb") as f:
f.write(binary)
if path != download_path and os.path.isfile(path):
os.remove(path)
return download_path
LatestObj = getBuildObj("paper",VERSION)
# 最新版の Velocity のファイルのハッシュ値
sha256_cloud = LatestObj["sha256"]
# ローカルにある Velocity のファイルのハッシュ値
sha256_local = getFile_sha256(PATH)
# 最新版の Velocity とローカルにある Velocity のハッシュ値が違っていたら
if sha256_cloud != sha256_local:
# ダウンロードする
print(f"{LatestObj['file']} Download...")
path = downloadLatest(LatestObj, PATH)
print(f"{path} Done")