Files
clima/ilmanifesto.sh

77 lines
2.1 KiB
Bash
Raw Normal View History

#!/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`
2022-04-07 14:50:14 +02:00
if [[ $timepassed -gt $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