-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-build.py
More file actions
123 lines (98 loc) · 4.49 KB
/
fetch-build.py
File metadata and controls
123 lines (98 loc) · 4.49 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import requests, json, sys, os
from github import Github
from github import Auth
from github import GitRelease
token = str(os.environ.get("GITHUB_TOKEN"))
auth = Auth.Token(token)
github = Github(auth=auth)
repo = github.get_repo("TeeMidNight/teeworlds-data")
branch = repo.get_branch("main")
def FindRelease(tag_name) -> GitRelease.GitRelease | None:
releases = repo.get_releases()
matched_releases = [release for release in releases if release.tag_name.find(tag_name) != -1]
if matched_releases:
return matched_releases[0]
return None
def FindReleases(comp, check_repo=repo) -> list[GitRelease.GitRelease]:
releases = check_repo.get_releases()
matched_releases = [release for release in releases if release.tag_name.find(comp) != -1]
if matched_releases:
return matched_releases
return []
def ReleaseFile(tag_name, ):
github.update_release()
def CreateDirIfNotExists(dirpath):
if os.path.exists(dirpath) == False:
os.mkdir(dirpath)
def FastDownloadFile(url, save_path):
response = requests.get(url, allow_redirects=True, stream=True)
file = open(save_path, "wb")
for i in response.iter_content(chunk_size=2048):
file.write(i)
file.close()
print("\r下载完成")
def FetchDDNet():
try:
url = "https://update.ddnet.org/update.json"
update = requests.get(url, allow_redirects=True)
update_json = json.loads(update.content) #获取版本号
Version = str(update_json[0]["version"])
if FindRelease(f"ddnet-{Version}") == None:
FileName = f"cache/DDNet/DDNet-{Version}.apk"
print(f"正在拉取DDNet-Android-{Version}")
CreateDirIfNotExists("cache/DDNet")
FastDownloadFile(f"https://ddnet.org/downloads/DDNet-{Version}.apk", FileName)
release = repo.create_git_release(f"ddnet-{Version}", f"DDNet-{Version}", "自动上传")
release.upload_asset(FileName)
print("DDNet已更新到最新!")
except Exception as e:
print("拉取DDNet部分失败!")
print(repr(e))
def BuildWeb():
CreateDirIfNotExists("web-build")
file = open("example_html", "r")
buffer = file.read()
# generate json
json_dump = [{"name" : "DDraceNetwork (Android)", "items": []},
{"name": "TaterClient (Windows, Gores)", "items": []},
{"name": "InfClass-Client (Windows, 感染模式)", "items": []},
{"name": "Carbon-Client (Windows)", "items": []},
{"name": "Teeworlds 0.7 (Windows, MacOS)", "items": []}]
for i in FindReleases("ddnet", repo):
item = {"title": f"{i.title}", "source": f"https://github.com/TeeMidnight/teeworlds-data/releases/download/{i.tag_name}/{i.title}.apk"}
json_dump[0]["items"].append(item)
for i in FindReleases("V", github.get_repo("sjrc6/TaterClient-ddnet")):
item = {"title": f"{i.title}", "source": f"https://github.com/sjrc6/TaterClient-ddnet/releases/download/{i.tag_name}/DDNet.exe"}
json_dump[1]["items"].append(item)
for i in FindReleases("v", github.get_repo("infclass/infclass-client")):
# find file
for file in i.get_assets():
if(file.name.find("win64") != -1):
item = {"title": f"{i.title}", "source": f"https://github.com/infclass/infclass-client/releases/download/{i.tag_name}/{file.name}"}
json_dump[2]["items"].append(item)
break
for i in FindReleases("c", github.get_repo("NewTeeworldsCN/teeworlds-carbon")):
# find file
for file in i.get_assets():
if(file.name.find("windows") != -1):
item = {"title": f"{i.title}", "source": f"https://github.com/NewTeeworldsCN/teeworlds-carbon/releases/download/{i.tag_name}/{file.name}"}
json_dump[3]["items"].append(item)
break
for i in FindReleases("teeworlds-latest"):
for file in i.get_assets():
item = {"title": f"{file.name}", "source": f"https://github.com/TeeMidnight/teeworlds-data/releases/download/{i.tag_name}/{file.name}"}
json_dump[4]["items"].append(item)
break
buffer = buffer.format(json.dumps(json_dump, ensure_ascii=False))
file = open("web-build/index.html", "w")
file.write(buffer)
file.close()
file = open("web-build/CNAME", "w")
file.write("data.teemidnight.online")
file.close()
def main():
CreateDirIfNotExists("cache")
FetchDDNet()
BuildWeb()
if __name__ == "__main__":
sys.exit(main())