Initial commit: MVP

This commit is contained in:
Yuki Joou 2024-11-10 16:27:35 +01:00
commit 148f148b0d
2 changed files with 152 additions and 0 deletions

33
pack2task-add.sh Executable file
View file

@ -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

119
pack2taskd.sh Executable file
View file

@ -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" <<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')
;;
esac
sleep 1 # Avoid overloading the servers or triggering some rate limiting!
done