Initial script

This commit is contained in:
cyber-dream 2022-08-02 06:10:24 +03:00
parent c756769866
commit a91fb0e098

121
main.py Normal file
View File

@ -0,0 +1,121 @@
import os
import sys
import hashlib
import json
from pprint import pprint
from configparser import ConfigParser
def calculate_md5(file_path):
hash_md5 = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def read_config(config_path):
config = ConfigParser()
config.read(config_path)
return config
def temp_forge_json():
with open('temp_forge.json') as json_file:
data = json.load(json_file)
return data
def parse_servers_config(config: ConfigParser):
servers = []
for section in config.sections():
if section.split(' ')[0] == 'Server' or section.split(' ')[0] == 'server':
new_server = {'id': (config.get(section, 'id')),
'name': (config.get(section, 'name')),
'description': (config.get(section, 'description')),
'icon': (config.get(section, 'icon')),
'version': (config.get(section, 'version')),
'address': (config.get(section, 'address')),
'minecraftVersion': (config.get(section, 'minecraftVersion')),
'discord_shortId': (config.get(section, 'discord_shortId')),
'discord_largeImageText': (config.get(section, 'discord_largeImageText')),
'discord_largeImageKey': (config.get(section, 'discord_largeImageKey')),
'mainServer': (config.get(section, 'mainServer')),
'autoconnect': (config.get(section, 'autoconnect')),
'_path': (config.get(section, 'path')),
'_download_url': (config.get(section, 'download_url')),
'modules': generate_mod_list(config.get(section, 'path'),
config.get(section, 'download_url'))}
servers.append(new_server)
return servers
def scan_mods(path):
f = []
for (dirpath, dirnames, filenames) in os.walk(path):
f.extend(filenames)
break
return f
def generate_mod_list(server_path, download_url):
mods_file_list = scan_mods(os.path.join(server_path, 'mods'))
mod_list = []
mod_list.append(temp_forge_json())
for mod in mods_file_list:
path_to_mod = os.path.join(server_path, 'mods', mod)
mod = mod[:-4]
parsed_mod = mod.split('_')
mod_id = parsed_mod[0]
parsed_mod[0] = parsed_mod[0].split(':')
parsed_mod[1] = parsed_mod[1].replace('-', ' ')
is_mod_required = True
if parsed_mod[-1] is not None:
if parsed_mod[-1] == 'ServerSide':
continue
if parsed_mod[-1] == 'Optional':
is_mod_required = False
new_mod = {
'id': mod_id,
'name': parsed_mod[1],
'type': 'ForgeMod',
'required': is_mod_required,
'artifact': {'size': sys.getsizeof(path_to_mod),
'MD5': calculate_md5(path_to_mod),
'url': download_url + mod + '.jar'
}
}
mod_list.append(new_mod)
return mod_list
def generate_json(config: ConfigParser):
new_distribution = {
'version': (config.get('Basic', 'version')),
'discord': {
'clientId': (config.get('Basic', 'version')),
'smallImageText': (config.get('Basic', 'discord_smallImageText')),
'smallImageKey': (config.get('Basic', 'dicord_smallImageKey'))
},
'java': {
'oracle': (config.get('Basic', 'java'))
},
'rss': (config.get('Basic', 'rss')),
'servers': parse_servers_config(config)
}
return new_distribution
if __name__ == '__main__':
temp_forge_json()
config = read_config(sys.argv[1])
# servers_list = parse_servers_config(config)
# mod_list = generate_mod_list(servers_list)
distribution = generate_json(config)
pprint(distribution)
with open('distribution.json', 'w', encoding='utf-8') as f:
json.dump(distribution, f, ensure_ascii=False, indent=4)