-
Notifications
You must be signed in to change notification settings - Fork 0
/
dank.resource-pack-builder.py
220 lines (183 loc) · 7.88 KB
/
dank.resource-pack-builder.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import json
import time
import hashlib
import requests
import zipfile
import shutil
import pretty_errors
from rich.live import Live
from rich.panel import Panel
from rich.table import Table
from dankware import cls, clr, rm_line, white, red, multithread
from rich.progress import Progress, SpinnerColumn, BarColumn, TextColumn, TimeRemainingColumn, TimeElapsedColumn
def prepare():
if not os.path.isdir("downloads"):
os.mkdir("downloads")
if os.path.isfile("dank.resource-pack.zip"):
os.remove("dank.resource-pack.zip")
if os.path.isdir("dank.resource-pack"):
shutil.rmtree("dank.resource-pack")
def file_downloader(url, file_name):
while True:
try:
response = requests.get(url, headers=headers, allow_redirects=True, timeout=3)
response.raise_for_status()
file_size = int(response.headers.get('Content-Length', 0)) / 1024 / 1024
with open(file_name, "wb") as file:
file.write(response.content)
print(clr(f" > Downloaded [ {file_name} ] [ {file_size:.3f} MB ]\n"))
break
except requests.exceptions.RequestException as e:
input(clr(f" > Failed [ {file_name} ] Press {white}ENTER{red} to try again... \n", 2))
rm_line()
rm_line()
def download_zips():
os.chdir("downloads")
to_download_urls = []
to_download_file_names = []
'''response = session.get("https://vanillaccurate.space/2021/07/17/vanillaccurate/#download").content.decode().splitlines()
for line in response:
if "hardtop-vanillaccurate-32x" in line:
url = line.split('href="')[2].split('"')[0]
zip_name = url.split("/")[-1]
if not os.path.exists(zip_name):
for existing_file in os.listdir():
if "hardtop-vanillaccurate-32x" in existing_file:
os.remove(existing_file)
to_download_urls.append(url)
to_download_file_names.append(zip_name)
break'''
if to_download_urls:
print(clr("\n > Starting Multiple Downloads... [ this might take a few minutes ]\n"))
while True:
try:
start_time = time.time()
multithread(file_downloader, 10, to_download_urls, to_download_file_names)
time_taken = int(time.time() - start_time)
print(clr(f"\n > Finished Downloads in {time_taken} seconds\n"))
break
except:
input(clr("\n > Failed to download files! Do not use [ Ctrl + C ]! Press [ENTER] to try again... ", 2))
cls()
os.chdir("..")
def merge_dicts(dict1, dict2):
"""
Recursively merge two dictionaries, sorting the keys alphabetically.
"""
for key, value in sorted(dict2.items(), key=lambda x: x[0]):
if key in dict1 and isinstance(dict1[key], dict) and isinstance(value, dict):
dict1[key] = merge_dicts(dict1[key], value)
elif key in dict1 and isinstance(dict1[key], list) and isinstance(value, list):
dict1[key].extend(value)
else:
dict1[key] = value
return dict1
def merge_json_files(file1_path, file2_path):
with open(file1_path, 'r', encoding='utf-8') as file:
file1_data = file.read().splitlines()
with open(file2_path, 'r', encoding='utf-8') as file:
file2_data = file.read().splitlines()
file1_data = "\n".join(line for line in file1_data if not line.strip().startswith("//"))
file2_data = "\n".join(line for line in file2_data if not line.strip().startswith("//"))
file1_data = json.loads(file1_data)
file2_data = json.loads(file2_data)
merged_data = merge_dicts(file1_data, file2_data)
return merged_data
def copy_and_overwrite(src, dst):
for item in os.listdir(src):
src_item = os.path.join(src, item)
dst_item = os.path.join(dst, item)
if os.path.isdir(src_item):
if not os.path.isdir(dst_item):
os.makedirs(dst_item)
copy_and_overwrite(src_item, dst_item)
else:
if dst_item.endswith(".json"):
if os.path.isfile(dst_item):
combined_file_data = json.dumps(merge_json_files(src_item, dst_item), indent=4)
with open(dst_item, "w", encoding="utf-8") as file:
file.write(combined_file_data)
print(clr(f" > Merged [ {dst_item} ]"))
continue
shutil.copy(src_item, dst_item)
def extract_zips():
for dir in ["downloads", "github", "github_ordered"]:
for zip_file in sorted(os.listdir(dir)):
if os.path.isdir("tmp"):
shutil.rmtree("tmp")
with zipfile.ZipFile(os.path.join(dir, zip_file), "r") as zip_ref:
zip_ref.extractall("tmp")
copy_and_overwrite("tmp", "dank.resource-pack")
def cleanup():
os.chdir("dank.resource-pack")
paths = [
"readme.txt",
"Changelog.txt",
"credits.txt",
#"assets/minecraft/texts/splashes.txt",
"LICENSE",
"LICENSE.txt",
]
for path in paths:
if os.path.exists(path):
try:
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)
except Exception as e:
print(clr(f"Failed to remove {path}: {e}"))
os.chdir("..")
shutil.rmtree("tmp")
def compress_to_zip():
path_to_backup = os.path.join(os.getcwd(), "dank.resource-pack")
zip_name = 'dank.resource-pack.zip'
compression_level = 9
source_files = []
prefixes = []
for root, dirs, files in os.walk(path_to_backup):
for file in files:
filepath = os.path.join(root, file)
prefix = str(filepath.split("dank.resource-pack")[1]).replace(f"\\{file}",'')
source_files.append(filepath)
prefixes.append(prefix)
width = os.get_terminal_size().columns
job_progress = Progress("{task.description}", SpinnerColumn(), BarColumn(bar_width=width),
TextColumn("[progress.percentage][bright_green]{task.percentage:>3.0f}%"), "[bright_cyan]ETA",
TimeRemainingColumn(), TimeElapsedColumn())
overall_task = job_progress.add_task("[bright_green]Compressing", total=len(source_files))
progress_table = Table.grid()
progress_table.add_row(Panel.fit(job_progress, title="[bright_white]Jobs", border_style="bright_red", padding=(1, 2)))
with Live(progress_table, refresh_per_second=10):
with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED, True, compression_level) as zf:
for i, filepath in enumerate(source_files):
prefix = prefixes[i]
relpath = os.path.relpath(filepath, path_to_backup)
zip_path = os.path.join(prefix, relpath)
zf.write(filepath, zip_path)
job_progress.update(overall_task, advance=1)
def save_sha1():
with open('dank.resource-pack.zip', 'rb') as file:
sha1_hash = hashlib.sha1(file.read()).hexdigest()
with open('sha1.txt', 'w', encoding="utf-8") as file:
file.write(sha1_hash)
if __name__ == "__main__":
cls()
session = requests.Session()
os.chdir(os.path.dirname(os.path.realpath(__file__)))
headers = {'User-Agent': 'dankware', 'Content-Type': 'application/zip'}
print(clr("\n > Preparing..."))
prepare()
print(clr("\n > Checking Downloaded Zips..."))
download_zips()
print(clr("\n > Extracting Zips...\n"))
extract_zips()
print(clr("\n > Copying and Overwriting Base..."))
copy_and_overwrite("base", "dank.resource-pack")
print(clr("\n > Cleaning Up..."))
cleanup()
print(clr("\n > Compressing to Zip...\n"))
compress_to_zip()
print(clr("\n > Saving SHA-1...\n"))
save_sha1()