Created
March 20, 2019 17:19
-
-
Save sbamin/8721452a9d0ceff28461d44aca693620 to your computer and use it in GitHub Desktop.
make short urls for https://go.verhaaklab.com - Note that these urls can not be edited later and does not allow custom url string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## shorturl generator | |
## Samir B. Amin, @sbamin | |
## EDIT: Add valid API token at FBXK variable. | |
## Optional: Install jq via brew or conda | |
# usage | |
show_help() { | |
cat << EOF | |
Make shorturl using google firebase REST API request | |
-l: url to shorten, must start with http or https (required) | |
-d: short url domain (default: https://rvl.page.link) | |
-k: Web API key (from firebase console, default: RV) | |
-g: short or long (hard to guess) short url (default: short) | |
-t: short url domain (default: RV) | |
Example: ${0##*/} -l https://example.com/foo/bar | |
EOF | |
} | |
if [[ $# == 0 ]];then show_help;exit 1;fi | |
while getopts "l:k:d:g:t:h" opt; do | |
case "$opt" in | |
h) show_help;exit 0;; | |
l) LONGURL=$OPTARG;; | |
k) FBXK=$OPTARG;; | |
d) DOMAINURL=$OPTARG;; | |
g) STR_LENGTH=$OPTARG;; | |
t) LINK_TARGET=$OPTARG;; | |
'?') show_help >&2 exit 1 ;; | |
esac | |
done | |
FBXK="${FBXK:-"ABC123"}" | |
DOMAINURL="${DOMAINURL:-"https://rvl.page.link"}" | |
STR_LENGTH="${STR_LENGTH:-short}" | |
LINK_TARGET="${LINK_TARGET:-RV}" | |
if [[ "$LINK_TARGET" == "SA" ]]; then | |
FBXK="blahblah" | |
DOMAINURL="blahblah" | |
fi | |
POSTURL=$(printf "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=%s" "$FBXK") | |
if [[ "$STR_LENGTH" == "short" ]]; then | |
GUESSID="SHORT" | |
else | |
GUESSID="UNGUESSABLE" | |
fi | |
if [[ -z "$LONGURL" ]]; then | |
echo -e "\nERROR: Missing long url, parsed as -l $LONGURL\nExample: -l https://example.com/foo/bar/\n" >&2 | |
show_help >&2 | |
exit 1 | |
fi | |
#### Make json input #### | |
generate_post_data() | |
{ | |
cat <<EOF | |
{ | |
"dynamicLinkInfo": { | |
"domainUriPrefix": "$DOMAINURL", | |
"link": "$LONGURL" | |
}, | |
"suffix": { | |
"option": "$GUESSID" | |
} | |
} | |
EOF | |
} | |
######## POST REQUEST ######## | |
#### parse output json if jq is present | |
JQ="$(command -v jq)" | |
if [[ -z "$JQ" || ! -x "$JQ" ]]; then | |
curl --silent --show-error --fail \ | |
-H "Accept: application/json" \ | |
-H "Content-Type:application/json" \ | |
-X POST --data "$(generate_post_data)" "${POSTURL}" | |
else | |
curl --silent --show-error --fail \ | |
-H "Accept: application/json" \ | |
-H "Content-Type:application/json" \ | |
-X POST --data "$(generate_post_data)" "${POSTURL}" | jq -r '.shortLink' | |
fi | |
## END ## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment