mirror of
https://github.com/SophiaAtkinson/yourls-bash.git
synced 2025-06-26 22:27:42 -07:00
70 lines
1.6 KiB
Bash
70 lines
1.6 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Configure these 2 lines and leave the rest
|
|
YOURLS_HOST="https://sho.rt/"
|
|
YOURLS_KEY="eb9444558f" # see sho.rt/admin/tools.php
|
|
|
|
yourls_help() {
|
|
echo "Shorten URLs with YOURLS"
|
|
echo
|
|
echo "Usage:"
|
|
echo " ${0##*/} <url>"
|
|
echo " ${0##*/} <url> -k <KEYWORD> -t <TITLE> -f <FORMAT>"
|
|
echo " ${0##*/} <url> --help"
|
|
echo
|
|
echo "Options:"
|
|
echo " -h | --help Show this screen"
|
|
echo " -k | --keyword <KEYWORD> Custom keyword"
|
|
echo " -t | --title <TITLE> Custom title"
|
|
echo " -f | --format <FORMAT> Ouput format (json, xml, simple)"
|
|
exit 1
|
|
}
|
|
|
|
# If no param or param is -h|--help
|
|
if [ -z "$1" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
|
yourls_help
|
|
fi
|
|
|
|
# Assuming first parameter is the URL
|
|
URL=$1;
|
|
shift;
|
|
|
|
POSITIONAL=()
|
|
while [[ $# -gt 0 ]]
|
|
do
|
|
key="$1"
|
|
case $key in
|
|
-k|--keyword)
|
|
KEYWORD="$2"
|
|
shift # argument
|
|
shift # value
|
|
;;
|
|
-t|--title)
|
|
TITLE="$2"
|
|
shift # argument
|
|
shift # value
|
|
;;
|
|
-f|--format)
|
|
FORMAT="$2"
|
|
shift # argument
|
|
shift # value
|
|
;;
|
|
-h|--help)
|
|
yourls_help
|
|
exit 1
|
|
;;
|
|
*) # unknown option
|
|
echo "${0##*/}: unknown option $1"
|
|
echo "Try '${0##*/} --help' for more information."
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ -z "$FORMAT" ]; then
|
|
FORMAT="simple"
|
|
fi
|
|
|
|
curl -s "$YOURLS_HOST/yourls-api.php?signature=$YOURLS_KEY&action=shorturl&url=$URL&keyword=$KEYWORD&title=$TITLE&format=$FORMAT"
|
|
|