commit 148f148b0d68f7003f851ae17b7342940dd0d8f8 Author: Yuki Joou Date: Sun Nov 10 16:27:35 2024 +0100 Initial commit: MVP diff --git a/pack2task-add.sh b/pack2task-add.sh new file mode 100755 index 0000000..c2baec9 --- /dev/null +++ b/pack2task-add.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +set -ex; + +DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/pack2task" +PACKAGES_DIR="$DATA_DIR/packages" +mkdir -p $PACKAGES_DIR + +URL=$1 +host=$(trurl "$URL" -g '{host}') + +tmpfile=$(mktemp) + +case "$host" in + "www.mondialrelay.fr") + query=$(trurl "$URL" -g '{query}') + # TODO: Check order of query parameters! + tracking_id=$(echo $query | cut -d'&' -f1 | cut -d'=' -f2) + postcode=$(echo $query | cut -d'&' -f2 | cut -d'=' -f2) + filename="$PACKAGES_DIR"/"mrfr-$(date '+%Y%m%dT%H%M%S').json" + echo "{}" > "$filename" + + jq '.tracker = "MRFR" | .waitingFor = "delivery"' "$filename" > "$tmpfile" + mv "$tmpfile" "$filename" + jq ".trackerInfo.id = \"$tracking_id\" | .trackerInfo.postcode = \"$postcode\"" "$filename" > "$tmpfile" + mv "$tmpfile" "$filename" + ;; + + *) + echo "Invalid tracking URL."; + exit -1; + ;; +esac diff --git a/pack2taskd.sh b/pack2taskd.sh new file mode 100755 index 0000000..c5fe8c5 --- /dev/null +++ b/pack2taskd.sh @@ -0,0 +1,119 @@ +#!/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" +} + +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') + ;; + esac + + sleep 1 # Avoid overloading the servers or triggering some rate limiting! +done