|
| 1 | +from steam.client import SteamClient |
| 2 | +from steam.client.cdn import CDNClient, CDNDepotFile |
| 3 | +import os |
| 4 | +import hashlib |
| 5 | +import json |
| 6 | +import time |
| 7 | +import sys |
| 8 | + |
| 9 | +ARMA3_SERVER_APP_ID = 233780 |
| 10 | +CACHE_DIR = "cache" |
| 11 | +MANIFEST_CACHE_FILE = os.path.join(CACHE_DIR, "manifests.json") |
| 12 | +CACHE_EXPIRY_SECONDS = 5 * 60 |
| 13 | + |
| 14 | +CDLC_IDS = { |
| 15 | + "csla": 233793, |
| 16 | + "gm": 233792, |
| 17 | + "vn": 233794, |
| 18 | + "ws": 233795, |
| 19 | + "spe": 233788, |
| 20 | + "rf": 233799, |
| 21 | + "ef": 233798 |
| 22 | +} |
| 23 | + |
| 24 | +def login(username, password): |
| 25 | + client = SteamClient() |
| 26 | + client.login(username, password) |
| 27 | + print("Logged in to Steam as", client.user.name) |
| 28 | + return client |
| 29 | + |
| 30 | +def load_cached_manifests(): |
| 31 | + """Load cached manifest data if it exists and is not expired""" |
| 32 | + if not os.path.exists(MANIFEST_CACHE_FILE): |
| 33 | + return None |
| 34 | + |
| 35 | + try: |
| 36 | + with open(MANIFEST_CACHE_FILE, 'r') as f: |
| 37 | + cache_data = json.load(f) |
| 38 | + |
| 39 | + if time.time() - cache_data.get('timestamp', 0) > CACHE_EXPIRY_SECONDS: |
| 40 | + print("Manifest cache expired, will refetch...") |
| 41 | + return None |
| 42 | + |
| 43 | + return cache_data.get('manifests', []) |
| 44 | + except (json.JSONDecodeError, FileNotFoundError): |
| 45 | + print("Cache file corrupted or missing, will refetch...") |
| 46 | + return None |
| 47 | + |
| 48 | +def save_manifests_to_cache(manifests): |
| 49 | + """Save manifest data to cache with timestamp""" |
| 50 | + os.makedirs(CACHE_DIR, exist_ok=True) |
| 51 | + |
| 52 | + manifest_data = [] |
| 53 | + for manifest in manifests: |
| 54 | + manifest_data.append({ |
| 55 | + 'name': manifest.name, |
| 56 | + 'gid': manifest.gid, |
| 57 | + 'depot_id': manifest.depot_id |
| 58 | + }) |
| 59 | + |
| 60 | + cache_data = { |
| 61 | + 'timestamp': time.time(), |
| 62 | + 'manifests': manifest_data |
| 63 | + } |
| 64 | + |
| 65 | + with open(MANIFEST_CACHE_FILE, 'w') as f: |
| 66 | + json.dump(cache_data, f, indent=2) |
| 67 | + |
| 68 | + print(f"Manifest data cached to {MANIFEST_CACHE_FILE}") |
| 69 | + |
| 70 | +def download_depot(client, depot_id): |
| 71 | + cdn_client = CDNClient(client) |
| 72 | + |
| 73 | + cached_manifests = load_cached_manifests() |
| 74 | + |
| 75 | + if cached_manifests: |
| 76 | + manifests = cached_manifests |
| 77 | + print("Got manifests from cache for ARMA3 server app ID:", ARMA3_SERVER_APP_ID) |
| 78 | + else: |
| 79 | + print("Fetching fresh manifests from Steam...") |
| 80 | + manifests_obj = cdn_client.get_manifests(ARMA3_SERVER_APP_ID, branch="creatordlc") |
| 81 | + |
| 82 | + save_manifests_to_cache(manifests_obj) |
| 83 | + |
| 84 | + manifests = [] |
| 85 | + for manifest in manifests_obj: |
| 86 | + manifests.append({ |
| 87 | + 'name': manifest.name, |
| 88 | + 'gid': manifest.gid, |
| 89 | + 'depot_id': manifest.depot_id |
| 90 | + }) |
| 91 | + |
| 92 | + target_manifest = next((m for m in manifests if m['depot_id'] == depot_id), None) |
| 93 | + if not target_manifest: |
| 94 | + print(f"No manifest found for depot ID {depot_id}") |
| 95 | + return |
| 96 | + |
| 97 | + print(f"Downloading Manifest ID: {target_manifest['gid']}, Depot ID: {target_manifest['depot_id']}") |
| 98 | + |
| 99 | + files_generator = cdn_client.iter_files(ARMA3_SERVER_APP_ID, branch="creatordlc", filter_func=lambda d_id, depot_info: d_id == target_manifest['depot_id']) |
| 100 | + files = list(files_generator) |
| 101 | + files = [f for f in files if f.is_file] |
| 102 | + print(f"Found {len(files)} files to download") |
| 103 | + download_files(client, cdn_client, files, destination="server/") |
| 104 | + |
| 105 | +def download_workshop(client, workshop_id): |
| 106 | + cdn_client = CDNClient(client) |
| 107 | + workshop_manifest = cdn_client.get_manifest_for_workshop_item(workshop_id) |
| 108 | + files_generator = workshop_manifest.iter_files() |
| 109 | + files = list(files_generator) |
| 110 | + files = [f for f in files if f.is_file] |
| 111 | + print(f"Found {len(files)} files to download") |
| 112 | + download_files(client, cdn_client, files, destination=f"server/workshop/{workshop_id}/") |
| 113 | + |
| 114 | +def download_files(client, cdn_client, files, destination): |
| 115 | + print(f"Verifying {len(files)} files...") |
| 116 | + files_to_download = [] |
| 117 | + |
| 118 | + for i, file in enumerate(files): |
| 119 | + file.local = os.path.join(destination, file.filename).lower() |
| 120 | + |
| 121 | + if os.path.exists(file.local): |
| 122 | + with open(file.local, 'rb') as f: |
| 123 | + existing_hash = hashlib.sha1(f.read()).hexdigest() |
| 124 | + expected_hash = file.sha_content.hex() if isinstance(file.sha_content, bytes) else file.sha_content |
| 125 | + if existing_hash == expected_hash: |
| 126 | + if file.is_executable: |
| 127 | + os.chmod(file.local, 0o755) |
| 128 | + continue |
| 129 | + files_to_download.append(file) |
| 130 | + |
| 131 | + print(f"Need to download {len(files_to_download)} files...") |
| 132 | + |
| 133 | + for i, file in enumerate(files_to_download): |
| 134 | + print(f"Downloading {i+1}/{len(files_to_download)}: {file.filename} ({file.size} bytes)") |
| 135 | + _download_single_file(file) |
| 136 | + |
| 137 | + print("All files downloaded successfully.") |
| 138 | + |
| 139 | +def _download_single_file(file): |
| 140 | + if file.local and os.path.dirname(file.local) != "": |
| 141 | + os.makedirs(os.path.dirname(file.local), exist_ok=True) |
| 142 | + |
| 143 | + chunk_size = 1024 * 1024 # 1MB chunks |
| 144 | + downloaded = 0 |
| 145 | + failed = False |
| 146 | + |
| 147 | + with open(file.local, 'wb') as f: |
| 148 | + while downloaded < file.size: |
| 149 | + remaining = file.size - downloaded |
| 150 | + read_size = min(chunk_size, remaining) |
| 151 | + |
| 152 | + chunk = file.read(read_size) |
| 153 | + if not chunk: |
| 154 | + break |
| 155 | + |
| 156 | + f.write(chunk) |
| 157 | + downloaded += len(chunk) |
| 158 | + |
| 159 | + if downloaded % (10 * 1024 * 1024) == 0: |
| 160 | + percent = (downloaded / file.size) * 100 |
| 161 | + print(f" Progress: {percent:.1f}% ({downloaded}/{file.size} bytes)") |
| 162 | + |
| 163 | + if failed: |
| 164 | + print(f"Failed to download {file.filename}") |
| 165 | + else: |
| 166 | + if file.is_executable: |
| 167 | + os.chmod(file.local, 0o755) |
| 168 | + print(f"✓ Downloaded {file.filename}") |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + import os |
| 172 | + import sys |
| 173 | + |
| 174 | + if len(sys.argv) != 3: |
| 175 | + print("Usage: python api.py <username> <password>") |
| 176 | + sys.exit(1) |
| 177 | + |
| 178 | + username = sys.argv[1] |
| 179 | + password = sys.argv[2] |
| 180 | + |
| 181 | + client = login(username, password) |
| 182 | + if client: |
| 183 | + download_depot(client, 233785) # Western Sahara |
| 184 | + download_workshop(client, 463939057) # Example workshop ID for ACE3 mod |
0 commit comments