#!/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" < $tmpdir/pack2task-$uid.ics <> $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 < -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 > '"$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 -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" } [ -z "$( ls -A "$PACKAGES_DIR/" )" ] && exit # No packages to check! 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 '
$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