101 lines
2.4 KiB
Bash
101 lines
2.4 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Default configuration
|
|
YOURLS_HOST="https://sho.rt/"
|
|
YOURLS_API_PATH="yourls-api.php"
|
|
YOURLS_KEY="" # Replace with your YOURLS key if you have one
|
|
|
|
# Function to display help information
|
|
yourls_help() {
|
|
cat << EOF
|
|
Shorten URLs with YOURLS
|
|
|
|
Usage:
|
|
${0##*/} <url>
|
|
${0##*/} <url> -k <keyword> -t <title> -f <format> [--api-path <path>] [--api-key <key>]
|
|
${0##*/} <url> --help
|
|
|
|
Options:
|
|
-h, --help Show this help message and exit
|
|
-k, --keyword <keyword> Set a custom keyword
|
|
-t, --title <title> Set a custom title
|
|
-f, --format <format> Set output format (json, xml, simple)
|
|
--api-path <path> Set custom API path (default: yourls-api.php)
|
|
--api-key <key> Set custom API key (if needed)
|
|
EOF
|
|
exit 1
|
|
}
|
|
|
|
# Check if no parameter is passed or if help option is provided
|
|
if [[ -z "$1" || "$1" == "--help" || "$1" == "-h" ]]; then
|
|
yourls_help
|
|
fi
|
|
|
|
# Initialize variables
|
|
URL=""
|
|
KEYWORD=""
|
|
TITLE=""
|
|
FORMAT="simple"
|
|
|
|
# Parse command line options
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
-k|--keyword)
|
|
KEYWORD="$2"
|
|
shift 2
|
|
;;
|
|
-t|--title)
|
|
TITLE="$2"
|
|
shift 2
|
|
;;
|
|
-f|--format)
|
|
FORMAT="$2"
|
|
shift 2
|
|
;;
|
|
--api-path)
|
|
YOURLS_API_PATH="$2"
|
|
shift 2
|
|
;;
|
|
--api-key)
|
|
YOURLS_KEY="$2"
|
|
shift 2
|
|
;;
|
|
--)
|
|
shift
|
|
break
|
|
;;
|
|
*)
|
|
URL="$1"
|
|
shift
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Check if URL is provided
|
|
if [[ -z "$URL" ]]; then
|
|
echo "Error: URL is required."
|
|
yourls_help
|
|
fi
|
|
|
|
# Set default format if not provided
|
|
if [[ -z "$FORMAT" ]]; then
|
|
FORMAT="simple"
|
|
fi
|
|
|
|
# Construct the API URL
|
|
API_URL="${YOURLS_HOST}${YOURLS_API_PATH}"
|
|
|
|
# Make API call to shorten URL if YOURLS_KEY is provided
|
|
if [[ -n "$YOURLS_KEY" ]]; then
|
|
RESPONSE=$(curl --fail --silent "${API_URL}?signature=${YOURLS_KEY}&action=shorturl&url=${URL}&keyword=${KEYWORD}&title=${TITLE}&format=${FORMAT}")
|
|
else
|
|
RESPONSE=$(curl --fail --silent "${API_URL}?action=shorturl&url=${URL}&keyword=${KEYWORD}&title=${TITLE}&format=${FORMAT}")
|
|
fi
|
|
|
|
# Check for HTML response
|
|
if [[ "$RESPONSE" == *"<html"* ]]; then
|
|
echo "Too Many Requests! Slow down please."
|
|
elif [[ -n "$RESPONSE" ]]; then
|
|
echo "$RESPONSE"
|
|
fi
|