Skip to content

Instantly share code, notes, and snippets.

@TheExpertNoob
Created April 24, 2024 04:06
Show Gist options
  • Save TheExpertNoob/4a0c070e3bd14469b6c5c9095f3c9e87 to your computer and use it in GitHub Desktop.
Save TheExpertNoob/4a0c070e3bd14469b6c5c9095f3c9e87 to your computer and use it in GitHub Desktop.
My rendition of finding missing files.
import os
import re
import requests
import zstandard
import subprocess
import shutil
import time
# Directory path to search
search_directory = "/path/to/stash"
file_url = "http://tinfoil.media/repo/db/db.bin"
download_path = "db.bin"
decompressed_path = "db.nsp"
input_subdirectory = 'db'
input_file_name = 'ranks.txt'
missing_file_name = 'missing.txt'
input_file_path = os.path.join(input_subdirectory, input_file_name)
missing_file_path = os.path.join(input_subdirectory, missing_file_name)
# Start the timer
start_time = time.time()
# Download the file
print(f"Downloading compressed database")
response = requests.get(file_url)
if response.status_code == 200:
with open(download_path, 'wb') as f:
f.write(response.content)
print(f"File downloaded successfully to {download_path}")
else:
print(f"Failed to download file. Status code: {response.status_code}")
exit()
# Decompress the file with zstd
print(f"Decompressing database")
with open(download_path, 'rb') as compressed_file, open(decompressed_path, 'wb') as decompressed_file:
decompressor = zstandard.ZstdDecompressor()
decompress_stream = decompressor.stream_reader(compressed_file)
while True:
chunk = decompress_stream.read(8192)
if not chunk:
break
decompressed_file.write(chunk)
print(f"File decompressed successfully to {decompressed_path}")
# Run the nspx.py script
print(f"Extract needed file")
subprocess.run(['python', 'nspx.py', '-xf', 'db.nsp', 'ranks.txt'])
# Cleanup
print(f"Cleanup of extra files")
os.remove(download_path)
os.remove(decompressed_path)
shutil.rmtree("__pycache__")
# Modify ranks.txt to contain only 16-digit hexadecimal strings
print(f"Strip extra characters from ranks.txt")
with open(input_file_path, 'r') as file:
next(file) # Skip the first line
lines = file.readlines()
modified_lines = [re.search(r'[0-9a-fA-F]{16}', line).group().upper() + '\n' for line in lines if re.search(r'[0-9a-fA-F]{16}', line)]
with open(input_file_path, 'w') as file:
file.writelines(modified_lines)
# Search for each line from ranks.txt recursively in all subdirectories
print(f"Searching for missing files")
with open(input_file_path, 'r') as ranks_file, open(missing_file_path, 'w') as missing_file:
for line in ranks_file:
filename_part = line.strip()
found = False
for root, dirs, files in os.walk(search_directory):
for file in files:
if filename_part in file: # Match the line from ranks.txt with the file name
found = True
break
if found:
print(f"Title Exists: {line.strip()}")
break
if not found:
missing_file.write(line)
# print(f"Missing line added to missing.txt: {line.strip()}")
# End the timer
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Search complete. Missing lines added to missing.txt")
print(f"Elapsed time: {elapsed_time:.2f} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment