Skip to content

Instantly share code, notes, and snippets.

@jericjan
Created March 29, 2025 16:08
Show Gist options
  • Save jericjan/def27d45449007e77cce279db8bda99b to your computer and use it in GitHub Desktop.
Save jericjan/def27d45449007e77cce279db8bda99b to your computer and use it in GitHub Desktop.
check for size parameter in URL that returns 200
"""
Full disclosure. Generatedd this with Gemini 2.5 Pro
"""
import requests
import sys
import concurrent.futures
import time
# --- Configuration ---
# Put the full URL (including a size parameter, it will be replaced)
TARGET_URL_WITH_SIZE = "https://cdn.discordapp.com/banners/396892407884546058/a_2fdbb5320902335cf881c17c45ffd027.gif?size=720"
# Set the starting size for the check (inclusive)
START_SIZE = 640 # <-- NEW: Start checking from this size
# Set the maximum size you want to check up to (inclusive)
MAX_SIZE = 4096 # Example: Discord often uses powers of 2 up to 4096
# Number of concurrent worker threads
NUM_WORKERS = 10
# Timeout for each request in seconds
TIMEOUT_SECONDS = 10
# --- End Configuration ---
def get_base_url(full_url):
"""Extracts the URL part before the query string (?...)"""
try:
return full_url.split('?')[0]
except IndexError:
return full_url
except Exception as e:
print(f"Error parsing base URL from '{full_url}': {e}", file=sys.stderr)
sys.exit(f"Could not determine base URL from: {full_url}")
def check_size(base_url, size, timeout):
"""
Checks a single size parameter.
Returns the size if status code is 200, otherwise returns None.
"""
current_url = f"{base_url}?size={size}"
try:
# Perform a HEAD request
response = requests.head(current_url, timeout=timeout, allow_redirects=True)
if response.status_code == 200:
# print(f"Size {size}: OK") # Print immediately if desired
return size # Return the successful size
return None
except requests.exceptions.Timeout:
return None # Indicate failure/timeout
except requests.exceptions.RequestException as e:
return None # Indicate failure
# --- Main Script ---
# Validate START_SIZE and MAX_SIZE
if START_SIZE < 0:
print(f"Error: START_SIZE ({START_SIZE}) cannot be negative.", file=sys.stderr)
sys.exit(1)
if MAX_SIZE < START_SIZE:
print(f"Error: MAX_SIZE ({MAX_SIZE}) cannot be less than START_SIZE ({START_SIZE}).", file=sys.stderr)
sys.exit(1)
base_url = get_base_url(TARGET_URL_WITH_SIZE)
print(f"Using Base URL: {base_url}")
print(f"Checking sizes from {START_SIZE} up to {MAX_SIZE} using {NUM_WORKERS} workers...")
found_valid_sizes = []
start_time = time.monotonic()
# Calculate the total number of sizes to check for progress reporting
total_sizes_to_check = MAX_SIZE - START_SIZE + 1
# Use ThreadPoolExecutor for managing worker threads
with concurrent.futures.ThreadPoolExecutor(max_workers=NUM_WORKERS) as executor:
# Create a dictionary to map future objects to their corresponding size
# MODIFIED: Use range(START_SIZE, MAX_SIZE + 1)
future_to_size = {
executor.submit(check_size, base_url, size, TIMEOUT_SECONDS): size
for size in range(START_SIZE, MAX_SIZE + 1) # <--- Use START_SIZE here
}
processed_count = 0
# Iterate over futures as they complete
for future in concurrent.futures.as_completed(future_to_size):
size = future_to_size[future] # Get the original size for this future
processed_count += 1
try:
result = future.result() # Get the result from the worker function
if result is not None:
print(f"Size {result}: OK") # Print success immediately
found_valid_sizes.append(result)
# Optional: Progress indicator
# Update progress indicator frequency/condition if desired
if processed_count % 100 == 0 or processed_count == total_sizes_to_check:
elapsed = time.monotonic() - start_time
# MODIFIED: Use total_sizes_to_check in progress report
print(f"Progress: Checked {processed_count}/{total_sizes_to_check} sizes in {elapsed:.2f}s", file=sys.stderr)
except Exception as exc:
print(f"Size {size}: Generated an exception: {exc}", file=sys.stderr)
end_time = time.monotonic()
print("\n--- Check Complete ---")
print(f"Finished in {end_time - start_time:.2f} seconds.")
if found_valid_sizes:
found_valid_sizes.sort() # Sort the results for readability
print(f"Valid sizes found in range [{START_SIZE}-{MAX_SIZE}]: {found_valid_sizes}")
else:
print(f"No sizes between {START_SIZE} and {MAX_SIZE} returned a 200 OK status.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment