From 313594066a94fea4d0d315f0a1feabff69494553 Mon Sep 17 00:00:00 2001 From: Aaditya Naik Date: Sat, 16 Dec 2017 20:22:02 +0530 Subject: [PATCH] Added the option to decode a QR code (#154) * Fixed support for single-word strings Earlier Qrify didn't work for strings of single words (even URLs) * Added functionality to store the qr code as a .png file * Fixed issues with the -f option in the getopts block Removed the fileValidation function * Enabled URL encoding for new line character for multiline messages Only works in the -f option * Update qrify * Made a general knowledge based quiz * removed file gkquiz from master branch * Added the file extension detection using grep * Added option to accept QR code as a PNG/GIF/JP(E)G file and decode it * Updated usage * Removed function syntax, indented stuff * Add files via upload --- qrify/qrify | 59 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/qrify/qrify b/qrify/qrify index 1bcd753..6818c42 100755 --- a/qrify/qrify +++ b/qrify/qrify @@ -85,15 +85,15 @@ makeqr() } # redirects the image obtained from the goqr api into a png file -function makeQRFile { +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 + httpGet "api.qrserver.com/v1/create-qr-code/?size=150x150&data=$input" > "$fileName" } -function addFileExt { +addFileExt() { if ! echo "$fileName" | grep -E -q ".*\.png$|.*\.PNG$" then fileName="$fileName.png" @@ -111,6 +111,35 @@ makeMultiLineQr() 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 'import sys, json; print json.load(sys.stdin)[0]["symbol"][0]["error"]') + + if [ "$error" = "None" ] + then + data=$(echo "$JSONresponse" | python -c '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 @@ -127,18 +156,20 @@ Usage: qrify [stringtoturnintoqrcode] -h Show the help -v Get the tool version -f Store the QR code as a PNG file + -d Decode the QR code from a PNG/GIF/JP(E)G file Examples: qrify this is a test string qrify -m two\\\\nlines qrify github.com (no http:// or https://) qrify -f fileoutputName google.com + qrify -d fileName.png EOF } getConfiguredClient || exit 1 -while getopts "f:m:hvu*:" option +while getopts "d:f:m:hvu*:" option do case "${option}" in v) echo "Version $currentVersion" && exit 0 ;; @@ -149,6 +180,9 @@ do fileName=$OPTARG #file name is the first argument of the option -f fileoutput="1";; + d) + fileName=$OPTARG + decode="1";; esac done @@ -172,12 +206,17 @@ elif [[ $# == "1" ]];then else if [[ $fileoutput == "1" ]] then - checkInternet || exit 1 - input=$(printf '%s ' "${@:3}") # first arg is -f, second is the file name, third onwards is the rest of the argument - # will have to be changed when implementing multiline QR code - makeQRFile || exit 1 - exit 0 - elif [[ $multiline == "0" ]]; then + checkInternet || exit 1 + input=$(printf '%s ' "${@:3}") # first arg is -f, second is the file name, third onwards is the rest of the argument + # will have to be changed when implementing multiline QR code + makeQRFile || exit 1 + exit 0 + elif [[ $decode == "1" ]] + then + checkInternet || exit 1 + ( decodeQR "$fileName" && echo "$data" ) || exit 1 + exit 0 + elif [[ $multiline == "0" ]]; then checkInternet || exit 1 input=$(printf '%s ' "$@") makeqr || exit 1