#!/usr/bin/env bash # Based on similar script by Linyos Torovoltos and Alex Epstein multiline="0" # flag that indicates multiline option fileoutput="0" # flag indicating the -f option # Determine which HTTP GET tool installed getConfiguredClient () { if command -v curl &>/dev/null; then configuredClient="curl" elif command -v wget &>/dev/null; then configuredClient="wget" elif command -v http &>/dev/null; then configuredClient="httpie" elif command -v fetch &>/dev/null; then configuredClient="fetch" else echo "Error: This tool requires either curl, wget, httpie or fetch to be installed." >&2 return 1 fi } # Call appropriate http get method httpGet() { case "$configuredClient" in curl) curl -A curl -s "$@" ;; wget) wget -qO- "$@" ;; httpie) http -b GET "$@" ;; fetch) fetch -q "$@" ;; esac } # Get installed version of Python / show error if none getConfiguredPython() { if command -v python3 &>/dev/null; then configuredPython="python3" elif command -v python2 &>/dev/null; then configuredPython="python2" elif command -v python &>/dev/null; then configuredPython="python" else echo "Error: This tool requires python to be installed." return 1 fi } if [[ $(uname) != "Darwin" ]]; then python() { case "$configuredPython" in python3) python3 "$@" ;; python2) python2 "$@" ;; python) python "$@" ;; esac } fi # Encode input, and call to qrenco.de to get response makeqr() { input=$(echo "$input" | sed s/" "/%20/g ) ## replace all spaces in the sentence with HTML-encoded space %20 httpGet qrenco.de/"$input" ## get a response for the qrcode } # Redirects returned QR impage into a png file makeQRFile() { input=$(echo "$input" | sed -e s/" "/%20/g -e s/'\\n'/%0A/g ) ##same as in the makeqr function addFileExt httpGet "api.qrserver.com/v1/create-qr-code/?size=150x150&data=$input" > "$fileName" } # If filename doesn't already have .png extension, append it addFileExt() { if ! echo "$fileName" | grep -E -q ".*\.png$|.*\.PNG$" then fileName="$fileName.png" fi } makeMultiLineQr() { if [[ ${configuredClient} != "curl" ]]; then ## prevent usage without curl it is unreliable echo "Multiline currently only supports curl!" return 1 else input=$(echo "$input" | sed -e s/" "/%20/g -e s/'\\n'/%0A/g ) ##same as in the makeqr function printf "%s" "$input" | curl -F-=\<- qrenco.de fi } # Function to get the json response from POST request decodeQR() { local qrFile="$1" if ! echo "$fileName" | grep -E -q ".*\.png$|.*\.PNG$|.*\.gif$|.*\.jpg$|.*\.jpeg$|.*\.GIF$|.*\.JPG$|.*\.JPEG$" then exit 1 fi # only uses curl # Cannot use wget because it does not support multipart/form-data (as per the man page)] case "$configuredClient" in curl) JSONresponse=$(curl -s -F "file=@$qrFile" http://api.qrserver.com/v1/read-qr-code/) || exit 1;; wget) echo "Error:-Not supported with wget" >&2 && exit 1;; httpie) JSONresponse=$(http -b --form POST http://api.qrserver.com/v1/read-qr-code/ file@"$qrFile") || exit 1;; fetch) echo "Error:-Not supported with wget" >&2 && exit 1;; esac error="$(echo "$JSONresponse" | python -c "from __future__ import print_function; import sys, json; print(json.load(sys.stdin)[0]['symbol'][0]['error'])")" if [[ "$error" == "None" ]] then data="$(echo "$JSONresponse" | python -c "from __future__ import print_function; import sys, json; print(json.load(sys.stdin)[0]['symbol'][0]['data'])")" else echo "Error:-$error" >&2 && exit 1 fi } checkInternet() { httpGet github.com > /dev/null 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query github with a get request } usage() { cat <