You're browsing snippets anonymously. Log in or create an account to save and manage your own snippets.
Public Snippets
Showing
14 snippets
Bulk download and extract zip files from urls
#!/bin/bash
# Configuration variables - modify these as needed
URLS_FILE="/path/to/urls.txt" # Path to file containing list of ZIP URLs
TARGET_DIR="/path/to/target/directory" # ...
By
xtream1101
•
•
Updated
2025-08-05 13:35
Django Query Stats per request
# stats.py
import time
from django.db import connection
from loguru import logger
class StatsMiddleware:
def __init__(self, get_response):
self.get_response = get_response
...
By
xtream1101
•
•
Updated
2025-05-11 00:04
Reset default python log handlers
import logging
formatter = logging.Formatter('FOO: %(message)s')
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# This is the key line that removes the double logging
...
By
xtream1101
•
•
Updated
2025-03-04 18:33
Check if a server has strictsni enabled
curl -v -k https://1.2.3.4
By
xtream1101
•
•
Updated
2025-02-28 14:13
Firefox hide tab bar
/* Source file https://github.com/MrOtherGuy/firefox-csshacks/tree/master/chrome/hide_tabs_toolbar_v2.css made available under Mozilla Public License v. 2.0
See the above repository for updates as we...
By
xtream1101
•
•
Updated
2025-02-27 21:45
Get domain cert details in cli
DOMAIN=yourdomain.tld
echo | openssl s_client -showcerts -servername $DOMAIN -connect $DOMAIN:443 2>/dev/null | openssl x509 -inform pem -noout -text
By
xtream1101
•
•
Updated
2025-02-12 19:48
List mounted drives
df -h --output=target,size,used,avail,pcent,source | grep -e Mounted -e $1 | awk 'NR<2{print $0;next}{print $0| "sort"}'
By
xtream1101
•
•
Updated
2025-02-08 22:44
Django graphene mutation input validation
import graphene
class ScratchpadMutation(graphene.relay.ClientIDMutation):
class Input:
created_by = graphene.String(required=True)
foobar = graphene.String()
test = g...
By
xtream1101
•
•
Updated
2025-02-06 14:13
Try, try again - Multiple try/except blocks in a flat format
data = {'some_key': 'key value'}
key_data = None
for _ in range(1):
try:
key_data = data['someKey']
except Exception: pass
else: break # It worked
try:
key_d...
By
xtream1101
•
•
Updated
2025-02-06 13:43
Multi key sort on a list of dicts
data = [
{'name': 'Alice', 'age': 25, 'city': 'New York'},
{'name': 'Bob', 'age': 30, 'city': 'London'},
{'name': 'Charlie', 'age': 20, 'city': 'New York'}
]
# Sort by 'city' first,...
By
xtream1101
•
•
Updated
2025-02-06 13:27
Convert image to dxf file
inkscape --export-type=dxf my-image.svg
By
xtream1101
•
•
Updated
2025-02-06 05:18
Clean up empty dirs
# List/print empty dirs (does not delete)
find /mnt/my-data/ -type d -empty -print
# Delete empty dirs
find /mnt/my-data/ -type d -empty -delete
By
xtream1101
•
•
Updated
2025-02-06 05:15
Bash command test
# Access individual arguments
echo "The first argument is: $1"
echo "The second argument is: $2"
# Access all arguments
echo "All arguments: $@"
# Access the number of arguments
echo "Number...
By
xtream1101
•
•
Updated
2025-02-06 05:13
Showing
14 snippets
Snippet
#!/bin/bash
# Configuration variables - modify these as needed
URLS_FILE="/path/to/urls.txt" # Path to file containing list of ZIP URLs
TARGET_DIR="/path/to/target/directory" # Directory where files will be extracted
TEMP_DIR="/tmp/zip_downloads" # Temporary directory for downloads
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if required tools are installed
check_dependencies() {
local missing_deps=()
command -v wget >/dev/null 2>&1 || command -v curl >/dev/null 2>&1 || missing_deps+=("wget or curl")
command -v unzip >/dev/null 2>&1 || missing_deps+=("unzip")
if [ ${#missing_deps[@]} -ne 0 ]; then
print_error "Missing dependencies: ${missing_deps[*]}"
exit 1
fi
}
# Function to validate inputs
validate_inputs() {
# Check if URLs file exists
if [ ! -f "$URLS_FILE" ]; then
print_error "URLs file not found: $URLS_FILE"
exit 1
fi
# Check if URLs file is readable
if [ ! -r "$URLS_FILE" ]; then
print_error "Cannot read URLs file: $URLS_FILE"
exit 1
fi
# Check if target directory exists, create if it doesn't
if [ ! -d "$TARGET_DIR" ]; then
print_warning "Target directory doesn't exist. Creating: $TARGET_DIR"
mkdir -p "$TARGET_DIR"
if [ $? -ne 0 ]; then
print_error "Failed to create target directory: $TARGET_DIR"
exit 1
fi
fi
}
# Function to create temporary directory
setup_temp_dir() {
if [ ! -d "$TEMP_DIR" ]; then
mkdir -p "$TEMP_DIR"
if [ $? -ne 0 ]; then
print_error "Failed to create temporary directory: $TEMP_DIR"
exit 1
fi
fi
}
# Function to download file
download_file() {
local url="$1"
local filename="$2"
local filepath="$TEMP_DIR/$filename"
print_status "Downloading: $url"
# Try wget first, then curl
if command -v wget >/dev/null 2>&1; then
wget -q -O "$filepath" "$url"
elif command -v curl >/dev/null 2>&1; then
curl -s -L -o "$filepath" "$url"
else
print_error "Neither wget nor curl is available"
return 1
fi
if [ $? -eq 0 ] && [ -f "$filepath" ]; then
print_status "Downloaded successfully: $filename"
return 0
else
print_error "Failed to download: $url"
return 1
fi
}
# Function to extract zip file
extract_zip() {
local zipfile="$1"
local filename=$(basename "$zipfile")
print_status "Extracting: $filename"
# Extract to target directory
unzip -q -o "$zipfile" -d "$TARGET_DIR"
if [ $? -eq 0 ]; then
print_status "Extracted successfully: $filename"
return 0
else
print_error "Failed to extract: $filename"
return 1
fi
}
# Function to cleanup downloaded zip file
cleanup_zip() {
local zipfile="$1"
local filename=$(basename "$zipfile")
print_status "Cleaning up: $filename"
rm -f "$zipfile"
if [ $? -eq 0 ]; then
print_status "Cleaned up: $filename"
else
print_warning "Failed to clean up: $filename"
fi
}
# Function to process a single URL
process_url() {
local url="$1"
local filename=$(basename "$url")
# If filename doesn't end in .zip, append .zip
if [[ ! "$filename" =~ \.zip$ ]]; then
filename="${filename}.zip"
fi
local filepath="$TEMP_DIR/$filename"
print_status "Processing: $url"
# Download the file
if download_file "$url" "$filename"; then
# Extract the file
if extract_zip "$filepath"; then
# Clean up the downloaded zip
cleanup_zip "$filepath"
print_status "Successfully processed: $url"
return 0
else
# Clean up on extraction failure
cleanup_zip "$filepath"
return 1
fi
else
return 1
fi
}
# Main execution function
main() {
print_status "Starting zip download and extraction script"
print_status "URLs file: $URLS_FILE"
print_status "Target directory: $TARGET_DIR"
print_status "Temporary directory: $TEMP_DIR"
# Check dependencies
check_dependencies
# Validate inputs
validate_inputs
# Setup temporary directory
setup_temp_dir
# Process each URL
local success_count=0
local failure_count=0
local total_count=0
local failed_urls=()
while IFS= read -r url; do
# Skip empty lines and comments
if [[ -z "$url" || "$url" =~ ^[[:space:]]*# ]]; then
continue
fi
# Remove leading/trailing whitespace
url=$(echo "$url" | xargs)
total_count=$((total_count + 1))
if process_url "$url"; then
success_count=$((success_count + 1))
else
failure_count=$((failure_count + 1))
failed_urls+=("$url")
fi
echo "" # Add blank line between processes
done < "$URLS_FILE"
# Final cleanup - remove temp directory if empty
if [ -d "$TEMP_DIR" ] && [ -z "$(ls -A "$TEMP_DIR")" ]; then
rmdir "$TEMP_DIR"
print_status "Removed empty temporary directory"
fi
# Print summary
print_status "Processing complete!"
print_status "Total URLs processed: $total_count"
print_status "Successful: $success_count"
if [ $failure_count -gt 0 ]; then
print_warning "Failed: $failure_count"
echo ""
print_error "Failed URLs:"
for failed_url in "${failed_urls[@]}"; do
echo " - $failed_url"
done
echo ""
print_status "You can create a new URLs file with just the failed URLs to retry them."
fi
}
# Run the main function
main "$@"
By
xtream1101
•
•
Updated
2025-08-05 13:35