Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- import argparse
- import shutil
- import zipfile
- from tqdm import tqdm
- import hashlib
- import base64
- def base64_hash(s):
- b64 = base64.b64encode(s.encode()).decode()
- hash_str = ''.join(filter(lambda c: not c.isdigit(), b64))
- return hash_str[:16].lower()
- def md5_hash_without_digits(text: str) -> str:
- # Convert text to bytes and calculate MD5 hash
- hash_object = hashlib.md5(text.encode())
- # Get hex representation of the hash, remove digits, and lowercase
- hash_str = hash_object.hexdigest()
- hash_str_no_digits = hash_str
- # hash_str_no_digits = ''.join(c for c in hash_str if not c.isdigit())
- hash_str_no_digits_lower = hash_str_no_digits.lower()
- # Return the first 8 characters of the hash string
- return hash_str_no_digits_lower
- def process_js_files(extracted_folder):
- # Step 2: Find all files in the specified folder that meet the *_interface.js mask
- files_to_process = []
- for root, dirs, files in os.walk(extracted_folder):
- for file in files:
- if file.endswith("_interface.js"):
- files_to_process.append(os.path.join(root, file))
- # Step 3: Map id and description inside a dictionary
- dictionary = {}
- for file_path in tqdm(files_to_process, desc="Processing files"):
- with open(file_path, "r", encoding='utf-8') as f:
- file_name = os.path.basename(file_path)
- prefix = file_name.split("_")[0:-1] # Get all parts of the filename before "_interface.js"
- prefix = "_".join(prefix)
- content = f.read()
- matches = re.findall(r"\{id:\"(\w+)\", description:\"(\w+)\"", content)
- for match in matches:
- if match[0] == 'Save':
- continue
- dictionary[match[0]] = base64_hash(md5_hash_without_digits(match[1] + "_" + prefix))
- # Step 5: Find all js files inside folder and replace all substrings represented in dictionary as keys with their values inside dictionary
- for root, dirs, files in os.walk(extracted_folder):
- for file_path in tqdm(files, desc="Replacing substrings"):
- if file_path.endswith(".js"):
- with open(os.path.join(root, file_path), "r+", encoding='utf-8') as f:
- content = f.read()
- for key in dictionary:
- content = content.replace(key, dictionary[key])
- f.seek(0)
- f.write(content)
- f.truncate()
- def create_updated_archive(input_archive_path):
- # Step 1: Unzip the archive and get the path to the folder containing the JS files
- with zipfile.ZipFile(input_archive_path, "r") as zip_ref:
- extracted_folder = zip_ref.namelist()[0]
- extracted_folder = os.path.join("temp", extracted_folder)
- zip_ref.extractall("temp")
- process_js_files(extracted_folder)
- # Step 6: Zip the updated files and replace the original archive
- output_archive_path = input_archive_path[:-4] + "_updated.zip"
- with zipfile.ZipFile(output_archive_path, "w") as zip_ref:
- for root, dirs, files in os.walk(extracted_folder):
- for file in files:
- file_path = os.path.join(root, file)
- archive_path = os.path.join(input_archive_path[:-4], file_path[len(extracted_folder):])
- zip_ref.write(file_path, arcname=archive_path)
- # Clean up temporary files
- shutil.rmtree("temp")
- # Replace the original archive with the updated archive
- os.remove(input_archive_path)
- os.rename(output_archive_path, input_archive_path)
- if __name__ == "__main__":
- # Parse command line arguments
- parser = argparse.ArgumentParser(description="Process JS files inside a ZIP archive.")
- parser.add_argument("archive", help="path to the ZIP archive containing the folder with JS files")
- args = parser.parse_args()
- specified_archive_path = args.archive
- create_updated_archive(specified_archive_path)
Add Comment
Please, Sign In to add comment