#!/bin/bash
###################################################################### 
#Copyright (C) 2023  Kris Occhipinti
#https://filmsbykris.com

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation version 3 of the License.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
###################################################################### 

# downloads and converts Walmart Reciepts to pdf with ORC
# example url 'https://receipts-query.edge.walmart.com/er/930d5bb3-3784-58bb-06b0-e712ba6eadc6'
[[ $1 ]] ||{ 
  echo -e "URL Needed\nExample: $0 'https://receipts-query.edge.walmart.com/er/930d5bb3-3784-58bb-06b0-e712ba6eadc6'"
  exit 1
}

url="$*"
[[ "$url" == *"https://receipts-query.edge.walmart.com"* ]] ||{
  echo "Invalid URL"
  exit 1
}

id="$(date +%s)_$RANDOM"
wget "$url" -qO- |tr " " "\n"|grep "src="|cut -d\" -f2|cut -d\, -f2|base64 -d > $id.png
convert "$id.png" "$id.pdf"
rm "$id.png"


pdf2ps "$id.pdf"
ps2pdf "$id.ps" || exit 1
rm "$id.ps"
ocrmypdf "$id.pdf" "$id.pdf"

#pdf2txt "$1" |sed '/^$/d'
pdftotext -layout "$id.pdf" -
