From 3d7f6edb0e204f4a5f6ae1689d0f6f0b0277a3e7 Mon Sep 17 00:00:00 2001 From: HugoPoi Date: Wed, 11 Nov 2020 18:09:05 +0100 Subject: [PATCH] feat: implement Leeto invoice posting --- .env.browser_fingerprint | 2 ++ .gitignore | 5 +++++ README.md | 29 +++++++++++++++++++++++++++++ config.sh.example | 6 ++++++ last-invoice-to-leeto.sh | 37 +++++++++++++++++++++++++++++++++---- 5 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 .env.browser_fingerprint create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config.sh.example diff --git a/.env.browser_fingerprint b/.env.browser_fingerprint new file mode 100644 index 0000000..9f963ad --- /dev/null +++ b/.env.browser_fingerprint @@ -0,0 +1,2 @@ +header = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0" +header = "Accept-Language: fr-FR,fr;q=0.5" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a5144e1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.html +*.pdf +*.json +config.sh +.env.netflix_cookies diff --git a/README.md b/README.md new file mode 100644 index 0000000..521034e --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +## What is this ? + +Automation tool for POSTING your [Netflix](https://www.netflix.com) invoice to [Leeto](https://leeto.co) + +## Requirements + +`apt install bash curl grep jq wkhtmltopdf` + +## Usage + +1. `cp .env.netflix_cookies.example .env.netflix_cookies` +1. `cp config.sh.example .env.netflix_cookies` +1. Fill the `.env.netflix_cookies` with `SecureNetflixId` and `NetflixId`. + In firefox these are in the developper tools in `Storage/Cookies` +1. Fill the `config.sh` with your email and password for Leeto +1. Change the amount of your invoice usally either `7.99`, `11.99` or + `15.99` +1. If you're not part of [Monibrand](https://www.monibrand.com) you need + to change the `LEETO_ORGANISATION_ID` and `LEETO_QUOTUM_ID` with yours +1. `$ ./last-invoice-to-leeto.sh` +1. then clean the cached files `rm *.html *.pdf` + +## TODO + +* Put this in an automation cron like Zappier or IFTTT or NodeRED +* Better handling error +* Netflix login (can be a pain in the ass) +* Trigger on the good invoice date of Netflix once a month +* Better error handling diff --git a/config.sh.example b/config.sh.example new file mode 100644 index 0000000..ff07ff2 --- /dev/null +++ b/config.sh.example @@ -0,0 +1,6 @@ +NETFLIX_BASE_URL="https://www.netflix.com" +LEETO_EMAIL="" +LEETO_PASSWORD="" +LEETO_QUOTUM_ID="5579" +LEETO_ORGANISATION_ID="417" +LEETO_AMOUNT="7.99" diff --git a/last-invoice-to-leeto.sh b/last-invoice-to-leeto.sh index 6145e1a..69930b0 100755 --- a/last-invoice-to-leeto.sh +++ b/last-invoice-to-leeto.sh @@ -1,9 +1,38 @@ #!/bin/bash -curl 'https://www.netflix.com/BillingActivity'\ - -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0'\ +set -e +source ./config.sh +# Get the Netflix Billing Page +[[ -f "BillingActivity.html" ]] || curl -v 'https://www.netflix.com/BillingActivity'\ -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'\ - -H 'Accept-Language: fr-FR,fr;q=0.5' --compressed \ - --config .env.netflix_cookies + --compressed \ + --config .env.browser_fingerprint\ + --config .env.netflix_cookies > BillingActivity.html +# Download the last invoice +LAST_INVOICE_URL=$(cat BillingActivity.html | grep -o '/invoice/print/[^"]*' | head -1) +[[ -f "LastInvoice.html" ]] || curl -v --compressed --config .env.netflix_cookies "$NETFLIX_BASE_URL$LAST_INVOICE_URL" > LastInvoice.html +# Convert to pdf +[[ -f "LastInvoice.pdf" ]] || wkhtmltopdf LastInvoice.html LastInvoice.pdf +# Leeto login +[[ -f "leeto_auth_payload.json" ]] || curl -v 'https://api.leeto.co/api/v2/users/sign_in?locale=fr'\ + -H 'Accept: application/json, text/plain, */*'\ + --config .env.browser_fingerprint\ + --compressed -H 'Content-Type: application/json'\ + --data-raw '{"user":{"email":"'"$LEETO_EMAIL"'","password":"'"$LEETO_PASSWORD"'"}}' > leeto_auth_payload.json + +LEETO_BEARER_TOKEN=$(cat leeto_auth_payload.json | jq --raw-output '.token') +LEETO_USER_ID=$(cat leeto_auth_payload.json | jq --raw-output '.id') + +curl -v "https://api.leeto.co/api/v2/organisations/$LEETO_ORGANISATION_ID/reimbursement_requests?locale=fr"\ + -H 'Accept: application/json, text/plain, */*'\ + --compressed \ + --header "Authorization: Bearer $LEETO_BEARER_TOKEN"\ + --config .env.browser_fingerprint\ + --form "reimbursement_request[user_id]=$LEETO_USER_ID"\ + --form "reimbursement_request[grantable_id]=$LEETO_USER_ID"\ + --form "reimbursement_request[grantable_type]=User"\ + --form "reimbursement_request[amount]=$LEETO_AMOUNT"\ + --form "reimbursement_request[quotum_id]=$LEETO_QUOTUM_ID"\ + --form "reimbursement_request[receipts_attributes][][image]=@LastInvoice.pdf"