From eaf64cb56155cd817a234c3835c942008f77b1c3 Mon Sep 17 00:00:00 2001 From: pie Date: Sat, 2 Apr 2022 13:42:32 +0200 Subject: [PATCH] The magic bash script collecting ilmanifesto just in time for your breakfast --- ilmanifesto.sh | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 ilmanifesto.sh diff --git a/ilmanifesto.sh b/ilmanifesto.sh new file mode 100644 index 0000000..ad2067f --- /dev/null +++ b/ilmanifesto.sh @@ -0,0 +1,76 @@ +#!/bin/bash +# +# Minimal CLI interface for ilmanifesto.it +# + +# Get latest news +curl -s --compressed \ + -H 'accept: application/json; charset=utf-8' \ + -H 'user-agent: okhttp/4.9.1' \ + https://api.ilmanifesto.it/api/v1/wp/editions/latest > latest.json + +# Or maybe older ones? +#curl -H 'accept: application/json; charset=utf-8' --compressed -H 'user-agent: okhttp/4.9.1' 'https://api.ilmanifesto.it/api/v1/wp/editions?pageSize=6&pageNumber=1' + +# List +bold=$(tput bold) +normal=$(tput sgr0) + +title=$(cat latest.json | jq -r .title) +subtitle=$(cat latest.json | jq -r .excerpt) +echo -e "${bold}${title}${normal}\n${subtitle}" + +read -n 1 -r -p "Download and open? [Y/n]" +if [[ $REPLY =~ ^[Nn]$ ]]; +then + echo "Bye!" + exit +fi + + +# Login needed? +if [ ! -f "token.json" ]; +then + + # First login + read -p 'Email: ' email + read -sp 'Password: ' pass + + curl -s --compressed \ + -H 'accept: application/json, text/plain, */*' \ + -H 'x-mobile-entitlement: app' -H 'content-type: application/json' \ + -H 'user-agent: okhttp/4.9.1' \ + -X POST https://api.ilmanifesto.it/api/v1/auth/login -d "{\"email\":\"$email\",\"password\":\"$pass\"}" > login.json + + cat login.json | jq -r .token > token.json + +else + + # Renew token + timeout=$(cat token.json | jq .expiresIn) + let timepassed=`date +%s`-`stat -c %Y token.json` + + if [[ $timepassed > $timeout ]] ; then + rtoken=$(cat token.json | jq -r .refreshToken) + curl -s --compressed \ + -H 'accept: application/json, text/plain, */*' \ + -H 'content-type: application/json' \ + -H 'user-agent: okhttp/4.9.1' \ + -X POST https://api.ilmanifesto.it/api/v1/auth/token \ + -d "{\"refreshToken\":\"$rtoken\"}" > token.json + fi + +fi + +# Get PDF +token=$(cat token.json | jq -r '.accessToken') +slug=$(cat latest.json | jq -r '.slug') +pdf=$(cat latest.json | jq -r '.pdf') + +curl -H 'range: bytes=null-' \ + -H "authorization: Bearer $token" -H 'accept: application/pdf' \ + -H 'content-type: application/pdf' -H 'user-agent: okhttp/4.9.1' \ + "https://api.ilmanifesto.it/api/v1/wp/pdfs/slug/$pdf/download" > $pdf.pdf + +# And open it +xdg-open $pdf.pdf