feat: implement Leeto invoice posting

This commit is contained in:
HugoPoi 2020-11-11 18:09:05 +01:00
parent 41fbae76c4
commit 3d7f6edb0e
5 changed files with 75 additions and 4 deletions

2
.env.browser_fingerprint Normal file
View File

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

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*.html
*.pdf
*.json
config.sh
.env.netflix_cookies

29
README.md Normal file
View File

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

6
config.sh.example Normal file
View File

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

View File

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