- 17 Nis 2015
- 513
- 1
Merhaba, Python için bir script'e sahibim ancak bunu çalıştıramadım yardımcı olabilir misiniz?
Kod:
import configparser
from datetime import datetime
import glob
import re
import urllib.request
import os
import subprocess
import zipfile
import logging
from shutil import copy2
LOCAL_ARCHIVE = "/archive/shaiya-gamigo"
PATCH_REGEX = "^ps([0-9]{4})"
UPDATE_ENDPOINT = "http://shaiya-us.download.aeriagames.com"
UPDATE_HEADER = "update.sah"
UPDATE_DATA = "update.saf"
GAME_CLIENT = "game.exe"
def init():
logger = logging.getLogger("logging_tryout2")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s: %(message)s",
"%d-%m-%Y %H:%M:%S")
ch = logging.FileHandler("download.log")
ch.setFormatter(formatter)
ch.setLevel(logging.INFO)
logger.addHandler(ch)
logger.info("Checking for updates...")
local_patch = get_local_gamigo_patch(f"{LOCAL_ARCHIVE}/patches/*")
remote_patch = get_remote_gamigo_patch()
logger.info(f"Current archive patch={local_patch}, Current gamigo patch={remote_patch}")
if local_patch >= remote_patch:
logger.info("No new updates detected")
for patch_number in range(local_patch+1, remote_patch+1):
download_patch(logger, patch_number)
def download_patch(logger, patch_number):
patch_name = f"ps{patch_number:04}"
logger.info(f"Downloading {patch_name}...")
endpoint = f"{UPDATE_ENDPOINT}/client/{patch_name}.patch"
response = urllib.request.urlopen(endpoint)
last_modified = response.headers["last-modified"]
date = datetime.strptime(last_modified, "%a, %d %b %Y %H:%M:%S %Z")
date_format = date.strftime("")
dated_name = f"{patch_name}-{date.day}-{date.month}-{date.year}"
patch_directory = f"{LOCAL_ARCHIVE}/patches/{dated_name}"
os.mkdir(patch_directory)
patch_path = f"{patch_directory}/{patch_name}.patch"
patch_file = open(patch_path, "xb") # x = create, b = bytes
patch_file.write(response.read())
patch_file.close()
with zipfile.ZipFile(patch_path, "r") as zip_ref:
logger.info(f"Extracting {patch_name} to {patch_directory}")
zip_ref.extractall(patch_directory)
# Extract the archive updates if needed.
if os.path.isfile(f"{patch_directory}/{UPDATE_HEADER}"):
logger.info(f"Extracting update archive (update.sah/saf) contents")
subprocess.call(["openshaiya-extractor", f"--data={patch_directory}/{UPDATE_DATA}", f"--header={patch_directory}/{UPDATE_HEADER}", f"--output={patch_directory}"])
os.remove(f"{patch_directory}/{UPDATE_DATA}")
os.remove(f"{patch_directory}/{UPDATE_HEADER}")
# Create a copy of the game client if it exists
patch_client = f"{patch_directory}/{GAME_CLIENT}"
if os.path.isfile(patch_client):
dest_path = f"{LOCAL_ARCHIVE}/clients/{patch_name}-{date.day}-{date.month}-{date.year}-game.exe"
logger.info(f"Copying new game client to {dest_path}")
copy2(patch_client, dest_path)
pass
def map_name_to_patch(name):
last_index_slash = name.rfind("/")
name = name[last_index_slash+1:last_index_slash+7]
pattern = re.compile(PATCH_REGEX)
result = pattern.search(name)
return int(result.group(1))
def get_local_gamigo_patch(path):
patches = list(map(map_name_to_patch, glob.glob(path)))
patches.sort(reverse=True)
return patches[0]
def get_remote_gamigo_patch():
url = f"{UPDATE_ENDPOINT}/UpdateVersion.ini"
response = urllib.request.urlopen(url)
text = response.read().decode("utf-8")
config = configparser.ConfigParser()
config.read_string(text)
return int(config["Version"]["PatchFileVersion"])
if __name__ == "__main__":
init()