Add googlemaps for geoloc

This commit is contained in:
2024-01-01 11:35:50 +01:00
parent ba43805d6f
commit 8cea379347

View File

@@ -25,7 +25,8 @@ flickr=true # "Bécasseau variable" "Le Teich" Bird
ccpx=true # Bécasseau variable, Le Teich, Bird
gps=0
opt_gps=true
opt_gps=true # insert gps data (false: gps data will be deleted in file)
opt_copyright=true # insert copyright, artist, creator (false: data will be deleted in file)
# Default: all tags
opt_others=false
@@ -36,6 +37,17 @@ opt_nature=false
opt_wildlife=false
opt_all=true # default
# Google maps static
zoom=10
color=blue
label="S"
type=hybrid
map_size="450x450"
prefix=${map_size%%x*} # find x position in map_size string
x=${#prefix}
gm_width=${map_size:0:$x}
echo $gm_width
long_path=`pwd`"/"`basename "$0"` # /Users/bruno/Documents/Scripts/kymsu2/keywords2insta.sh
# long: /Users/bruno/Documents/GitHub/GooFuzz/keywords2insta.sh
#long_path=$(realpath "$0") # vide
@@ -44,7 +56,7 @@ dir=$(dirname "$0") # /Users/bruno/Documents/Scripts/keywords2insta
dotenv () {
set -a
[ -f .env ] && . .env
[ -f .env ] && . .env || echo -e "${red}\nNo .env file found ! Googlemaps reverse geocode not working.${reset}"
set +a
}
@@ -88,17 +100,20 @@ showHelp() {
echo
echo -e "${yellow}USAGE:${reset}"
echo -e "${green}keyword2insta [-Options...] -f <file>${reset}"
echo -e "If no option is chosen, all tags are added (default)."
echo -e "If no option (except gps and copyright) is chosen, all tags are added (default)."
echo -e "Keywords taken from exifs are automatically added."
echo -e "You can add keywords permanently by adding them to the ${underline}others array${reset} at the start of the script. (See others=())"
echo -e "The images files are not modified."
echo
echo -e "If the gps and coypright options are selected, the images files will be modified."
echo
echo -e "${yellow}OPTIONS:${reset}"
echo -e " ${green}-f${reset}, ${green}--file <file>${reset} Image file"
echo -e " ${green}-l${reset}, ${green}--lens${reset} Add lens exif"
echo -e " ${green}-m${reset}, ${green}--model${reset} Add APN model exif"
echo -e " ${green}-n${reset}, ${green}--nature${reset} Add nature related keywords"
echo -e " ${green}-r${reset}, ${green}--region${reset} Add region related keywords"
echo -e " ${green}-c${reset}, ${green}--copyright${reset} Delete artist and copyright in file"
echo -e " ${green}-t${reset} <keywords> Add some additionnals keywords"
echo -e " ${green}-w${reset}, ${green}--wildlife${reset} Add wildlife related keywords"
echo -e " ${green}-h${reset}, ${green}--help${reset} Display this help"
@@ -128,13 +143,15 @@ showHelp() {
echo -e "${yellow}Changelog:${reset}"
echo -e " v3.0: support 500px"
echo -e " v3.1: display thumb"
echo -e " v3.2: suppress copyright and artist infos"
echo -e " v3.3: add a static Google maps for geoloc"
}
# https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options
# le 1er : de $optspec supprime le message d'erreur
unset -v tags
#OPTIND=1
optspec=":wnrf:t:mlh-:"
optspec=":wnrcf:t:mlh-:"
while getopts "$optspec" opt
do
case $opt in
@@ -153,6 +170,7 @@ do
;;
model) opt_model=true;;
lens) opt_lens=true;;
copyright) opt_copyright=false;;
*)
if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" = ":" ]; then
echo "Unknown option --${OPTARG}" >&2
@@ -163,6 +181,7 @@ do
w) opt_wildlife=true;;
n) opt_nature=true;;
r) opt_region=true;;
c) opt_copyright=false;;
f) file="${OPTARG}";;
t) tags=("$OPTARG")
until [[ $(eval "echo \${$OPTIND}") =~ ^-.* ]] || [ -z $(eval "echo \${$OPTIND}") ]; do
@@ -198,7 +217,7 @@ fi
t=$(echo "${tags[@]}" | sed -r 's/[^ ]+/,&/g')
#
: <<'END_COMMENT'
echo "----"
echo "others: " ${others[@]}
echo "tags: " ${tags[@]} # t
@@ -208,8 +227,10 @@ echo "opt_region: " $opt_region # r
echo "opt_nature: " $opt_nature # n
echo "opt_wildlife: " $opt_wildlife # w
echo "opt_others: " $opt_others
echo "opt_copyright: " $opt_copyright # c
echo "----"
#
END_COMMENT
if ! command -v exiftool &> /dev/null; then
echo -e "${bold}exiftool${reset} could not be found !\n"
@@ -221,7 +242,7 @@ fi
[ ! -f "$file" ] && echo -e "${bold}${red}No input file !${reset}" && exit 2
echo -e "\n${greenbold}keywords2insta${reset} v2.1"
echo -e "\n${greenbold}keywords2insta${reset} v3.3"
# dico français / anglais
@@ -281,8 +302,32 @@ altitude=$(echo "$exif" | jq -j '.[] | .GPSAltitude | select( . != null )')
width=$(echo "$exif" | jq -j '.[] | .ImageWidth | select( . != null )')
height=$(echo "$exif" | jq -j '.[] | .ImageHeight | select( . != null )')
size=$(echo "$exif" | jq -j '.[] | .FileSize | select( . != null )')
city=$(echo "$exif" | jq -j '.[] | .City | select( . != null )')
state=$(echo "$exif" | jq -j '.[] | .State | select( . != null )')
country=$(echo "$exif" | jq -j '.[] | .Country | select( . != null )')
comment=$(echo "$exif" | jq -j '.[] | .Comment | select( . != null )')
software=$(echo "$exif" | jq -j '.[] | .Software | select( . != null )')
# EXIF:Artist | IPTC:By-line | XMP:Creator : Pesenti
# EXIF:Copyright | IPTC:CopyrightNotice | XMP:Rights: clicclac.info
art=$(echo "$exif" | jq -j '.[] | .Artist | select( . != null )')
#byline=$(echo "$exif" | jq -j '.[] | .By-line | select( . != null )')
creator=$(echo "$exif" | jq -j '.[] | .Creator | select( . != null )')
if [ -n "$art" ]; then artist="$art"
elif [ -n "$creator" ]; then artist="$creator"
fi
copy=$(echo "$exif" | jq -j '.[] | .Copyright | select( . != null )')
notice=$(echo "$exif" | jq -j '.[] | .CopyrightNotice | select( . != null )')
rights=$(echo "$exif" | jq -j '.[] | .Rights | select( . != null )')
if [ -n "$copy" ]; then copyright="$copy "
elif [ -n "$notice" ]; then copyright="$notice"
else copyright="$rights"
fi
echo "$lensID"
# Display thumb image
@@ -400,40 +445,64 @@ if [[ -n $iso ]] && [[ -n $speed ]] && [[ -n $aperture ]]; then
printf " %-11s %-35s \n" "Size:" "${size}"
if [[ -n $latitude ]] && [[ -n $longitude ]]; then
gps=1
printf " %-11s %-35s \n" "Latitude:" "${latitude}"
printf " %-11s %-35s \n" "Longitude:" "${longitude}"
printf " %-11s %-35s \n" "Altitude:" "${altitude}"
gps=1
city=""
if [[ -n $city ]] && [[ -n $state ]] && [[ -n $country ]]; then
printf " %-11s %-35s \n" "City:" "${city}"
printf " %-11s %-35s \n" "State:" "${state}"
printf " %-11s %-35s \n" "Country:" "${country}"
else
coords=$(exiftool -n -gpslatitude -gpslongitude "$file")
lat=$(echo "$coords" | sed -n '1p' | awk -F":" '{print $2}' | xargs)
long=$(echo "$coords" | sed -n '$p' | awk -F":" '{print $2}' | xargs)
coords=$(exiftool -n -gpslatitude -gpslongitude "$file")
lat=$(echo "$coords" | sed -n '1p' | awk -F":" '{print $2}' | xargs)
long=$(echo "$coords" | sed -n '$p' | awk -F":" '{print $2}' | xargs)
if [[ -n $GM_API_KEY ]]; then
gm=$(curl -s "https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${long}&sensor=false&key=$GM_API_KEY" | jq -r '.results[0].address_components')
city=$(echo "$gm" | jq '. | map( select( .types | contains(["locality"]) ) ) | .[].long_name')
code=$(echo "$gm" | jq '. | map( select( .types | contains(["postal_code"]) ) ) | .[].long_name')
state=$(echo "$gm" | jq '. | map( select( .types | contains(["administrative_area_level_2"]) ) ) | .[].long_name')
region=$(echo "$gm" | jq '. | map( select( .types | contains(["administrative_area_level_1"]) ) ) | .[].long_name')
country=$(echo "$gm" | jq '. | map( select( .types | contains(["country"]) ) ) | .[].long_name')
printf " %-11s %-35s \n" "CP / Ville:" "${code//\"/} ${city//\"/}"
printf " %-11s %-35s \n" "Région:" "${state//\"/}"
printf " %-11s %-35s \n" "Pays:" "${country//\"/}"
if [[ -n $GM_API_KEY ]]; then
gm=$(curl -s "https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${long}&sensor=false&key=$GM_API_KEY" | jq -r '.results[0].address_components')
city=$(echo "$gm" | jq '. | map( select( .types | contains(["locality"]) ) ) | .[].long_name')
code=$(echo "$gm" | jq '. | map( select( .types | contains(["postal_code"]) ) ) | .[].long_name')
state=$(echo "$gm" | jq '. | map( select( .types | contains(["administrative_area_level_2"]) ) ) | .[].long_name')
region=$(echo "$gm" | jq '. | map( select( .types | contains(["administrative_area_level_1"]) ) ) | .[].long_name')
country=$(echo "$gm" | jq '. | map( select( .types | contains(["country"]) ) ) | .[].long_name')
printf " %-11s %-35s \n" "CP / City:" "${code//\"/} ${city//\"/}"
printf " %-11s %-35s \n" "State:" "${state//\"/}"
printf " %-11s %-35s \n" "Region:" "${region//\"/}"
printf " %-11s %-35s \n" "Country:" "${country//\"/}"
#url="https://maps.googleapis.com/maps/api/staticmap?center=$lat,$long&zoom=14&size=400x400&key=$GM_API_KEY"
url="https://maps.googleapis.com/maps/api/staticmap?center=$lat,$long&zoom=$zoom&markers=color:$color%7Clabel:$label%7C$lat,$long&maptype=$type&size=$map_size&key=$GM_API_KEY"
#curl "https://maps.googleapis.com/maps/api/staticmap?center=47.1275353416667,4.78201903333333&zoom=14&size=400x400&key=AIzaSyD94L2Q4K9_ErGSarTBN4FrUxDSspGzNhY" --output /tmp/gm.png
gm_path=/tmp/k2i/gm.png
curl -s "$url" --output $gm_path
if [ -f "$gm_path" ]; then
printf "\n\t\033]1337;File=;width=$((gm_width * 2)) px;inline=1:`cat $gm_path | base64`\a\n"
fi
fi
fi
fi
printf " %-11s %-35s \n" "Comment:" "${comment}"
printf " %-11s %-35s \n" "Artist:" "${artist}"
printf " %-11s %-35s \n" "Copyright:" "${copyright}"
echo
fi
# gps
if [ $opt_gps = true ]; then
if [ $gps -eq 1 ]; then
echo -e "${red}GPS data available !${reset}"
a=$(echo -e "Do you want to ${bold}erase GPS data${reset} ? (k)eep or (e)rase ")
read -p "$a" choice
d=$(echo -e "Do you want to ${bold}erase GPS data${reset} ? (k)eep or (e)rase ")
read -p "$d" choice
if [ "$choice" == "e" ] || [ "$choice" == "E" ]; then
echo "Erasing GPS data..."
@@ -447,6 +516,20 @@ else
fi
# copyright
if [ $opt_copyright = false ]; then
b=$(echo -e "Do you want to ${bold}erase Artist and Copyright data${reset} ? (k)eep or (e)rase ")
read -p "$b" choice
if [ "$choice" == "e" ] || [ "$choice" == "E" ]; then
echo "Erasing Artist and Copyright data..."
#a="-EXIF:Artist= -IPTC:By-line= -XMP:Creator="
#c="-EXIF:Copyright= -IPTC:CopyrightNotice= -XMP:Rights="
exiftool -EXIF:Artist= -IPTC:By-line= -XMP:Creator= -EXIF:Copyright= -IPTC:CopyrightNotice= -XMP:Rights= "$file"
fi
fi
# keywords
k=$(echo "$exif" | jq --compact-output --raw-output '.[] | .Keywords[]?' | tr "\n" "," | sed 's/\,$//')
#k=$(echo "$exif" | sed -n '/^Keywords/p' | awk -F":" '{print $2}' | sed 's/^ *//g')