pack2task/pack2taskd.sh

147 lines
5.2 KiB
Bash
Executable file

#!/bin/bash
set -ex;
UA="Pack2Task"
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pack2task"
mkdir -p "$CONFIG_DIR/"
CONFIG_FILE="$CONFIG_DIR/config.json"
[ ! -f "$CONFIG_FILE" ] && \
echo "Config not found! Installing default. Please set appropriate values in $CONFIG_FILE!" && \
cat > "$CONFIG_FILE" <<EOF && exit 1
{
"login": "someusername:somepassword",
"calendar": "https://yourserver.host/calendar/"
}
EOF
DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/pack2task"
PACKAGES_DIR="$DATA_DIR/packages"
mkdir -p $PACKAGES_DIR
set_reminder() {
uid=$1
message=$2
url=$3
delay=$4
tmpdir=$(mktemp -d)
cat > $tmpdir/pack2task-$uid.ics <<EOF
BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VTODO
EOF
echo "UID:$uid" >> $tmpdir/pack2task-$uid.ics
echo "CREATED:$(date '+%Y%m%dT%H%M%S')" >> $tmpdir/pack2task-$uid.ics
echo "SUMMARY:$message" >> $tmpdir/pack2task-$uid.ics
echo "DUE:$(date --date="$delay" '+%Y%m%dT%H%M%S')" >> $tmpdir/pack2task-$uid.ics
echo "DESCRIPTION:$url" >> $tmpdir/pack2task-$uid.ics
cat >> $tmpdir/pack2task-$uid.ics <<EOF
END:VTODO
END:VCALENDAR
EOF
set +x
echo '+ curl -X PUT --user <REDACTED> <REDACTED> -T '"$tmpdir/pack2task-$uid.ics"
curl -X PUT --user "$(jq -r '.login' "$CONFIG_FILE")" "$(jq -r '.calendar' "$CONFIG_FILE")" -T $tmpdir/pack2task-$uid.ics
set -x
rm "$tmpdir/pack2task-$uid.ics"
rmdir "$tmpdir"
}
complete_reminder() {
uid=$1
tmpdir=$(mktemp -d)
set +x
echo '+ curl --user <REDACTED> <REDACTED> > '"$tmpdir/pack2task-$uid.ics"
curl --user "$(jq -r '.login' "$CONFIG_FILE")" "$(jq -r '.calendar' "$CONFIG_FILE")/pack2task-$uid.ics" > $tmpdir/pack2task-$uid.ics
set -x
awk '/END:VTODO/{print "PERCENT-COMPLETE:100\nSTATUS:COMPLETED"} 1' $tmpdir/pack2task-$uid.ics > $tmpdir/pack2task-$uid.ics.new
mv $tmpdir/pack2task-$uid.ics.new $tmpdir/pack2task-$uid.ics # This is a bit awkward but this avoids long lines while having error checking.
awk "/END:VTODO/{print \"COMPLETED:$(date '+%Y%m%dT%H%M%S')\"} 1" $tmpdir/pack2task-$uid.ics > $tmpdir/pack2task-$uid.ics.new
mv $tmpdir/pack2task-$uid.ics.new $tmpdir/pack2task-$uid.ics
set +x
echo '+ curl -X PUT --user <REDACTED> <REDACTED> -T '"$tmpdir/pack2task-$uid.ics"
curl -X PUT --user "$(jq -r '.login' "$CONFIG_FILE")" "$(jq -r '.calendar' "$CONFIG_FILE")" -T $tmpdir/pack2task-$uid.ics
set -x
rm "$tmpdir/pack2task-$uid.ics"
rmdir "$tmpdir"
}
for package_file in "$PACKAGES_DIR"/*.json; do
echo "$package_file"
tracker_type=$(jq -r '.tracker' "$package_file")
case "$tracker_type" in
"MRFR")
tracking_id=$(jq -r '.trackerInfo.id' "$package_file")
postcode=$(jq -r '.trackerInfo.postcode' "$package_file")
waiting_for=$(jq -r '.waitingFor' "$package_file")
url="https://www.mondialrelay.fr/suivi-de-colis/?NumeroExpedition=$tracking_id&CodePostal=$postcode"
token=$(curl "$url" -H "User-Agent: $UA" -H 'Upgrade-Insecure-Requests: 1' | \
grep '<div id="tracking" token=' | sed -r 's/.*token="([^"]*).*"/\1/')
track_data=$(curl "https://www.mondialrelay.fr/api/tracking?shipment=$tracking_id&postcode=$postcode" \
-H "User-Agent: $UA" -H 'Upgrade-Insecure-Requests: 1' -H "RequestVerificationToken: $token")
sender=$(echo "$track_data" | jq -r '.Expedition.Enseigne')
while read event; do
if [ "$waiting_for" = 'delivery' ] && [ "$event" = 'Colis en route vers le point de retrait' ]; then
reminder_uid=$(uuidgen)
set_reminder "$reminder_uid" "Colis $sender à aller chercher (Mondial Relay - $tracking_id)" "$url" '7 day'
# Update the package information file, we're now waiting for pickup
tmpfile=$(mktemp)
jq ".waitingFor = \"pickup\" | .reminderUID = \"$reminder_uid\"" "$package_file" > $tmpfile
mv "$tmpfile" "$package_file"
break
fi
if [ "$waiting_for" = 'pickup' ] && [ "$event" = 'Colis livré au destinataire' ]; then
uid=$(jq -r '.reminderUID' "$package_file")
complete_reminder $uid
rm $package_file
break
fi
done < <(echo "$track_data" | jq -r '.Expedition.Evenements[].Libelle')
;;
"S2S")
tracking_id=$(jq -r '.trackerInfo.id' "$package_file")
waiting_for=$(jq -r '.waitingFor' "$package_file")
url="https://www.chronoshop2shop.fr/tracking/#/numlt/$tracking_id"
track_data=$(curl "https://www.chronoshop2shop.fr/tracking-ws-rest/tracking/suivi-colis-s2s?lang=fr&listeNumerosLT=$tracking_id" \
-H "$UA" -H 'Referer: https://www.chronoshop2shop.fr/tracking/')
while read event; do
if [ "$waiting_for" = 'delivery' ] && [ "$event" = 'Disponible en relais Pickup' ]; then
reminder_uid=$(uuidgen)
set_reminder "$reminder_uid" "Colis Shop2Shop à aller chercher ($tracking_id)" "$url" '7 day'
# Update the package information file, we're now waiting for pickup
tmpfile=$(mktemp)
jq ".waitingFor = \"pickup\" | .reminderUID = \"$reminder_uid\"" "$package_file" > $tmpfile
mv "$tmpfile" "$package_file"
break
fi
if [ "$waiting_for" = 'pickup' ] && [ "$event" = 'Retiré par le destinataire' ]; then
uid=$(jq -r '.reminderUID' "$package_file")
complete_reminder $uid
rm $package_file
break
fi
done < <(echo "$track_data" | jq -r '.etapes[].libelle')
;;
esac
sleep 1 # Avoid overloading the servers or triggering some rate limiting!
done