wallabag2epub/wallabag2epub.sh
Peter Molnar d77c57003f
2019-08-05 11:20:50 +00:00

45 lines
1.9 KiB
Bash

#!/usr/bin/env bash
# script needs env vars passed, needs to be called as:
# WALLABAG_ID='client_id' WALLABAG_SECRET='client_secret' WALLABAG_USERNAME='username' WALLABAG_PASSWORD='password' WALLABAG_URL='url' bash wallabag2epub.sh
# requires `jq` https://stedolan.github.io/jq/ because nobody should deal with parsing json with sed
function err {
>&2 echo $1
}
function archive_batch {
mkdir -p "${HOME}/archive"
# $1 is the json blob of entries export
# $2 is access_token
local ids=($(echo $1 | jq -r '._embedded.items[].id' | xargs))
local urls=($(echo $1 | jq -r '._embedded.items[].url' | xargs))
for i in "${!ids[@]}"; do
local id="${ids[$i]}"
local fname="$(echo ${urls[$i]} | sed -r 's#https?://([w]{3}\.)?##' | sed -e 's/[^[:alnum:]]/-/g' | sed -r 's/s(.*)-/\1/' | tr A-Z a-z)"
if [ "${fname}" == "null" ]; then
err "problems with ${ids[$i]}: url is null"
continue
fi
local target="${HOME}/archive/${id}_${fname}.epub"
if [ ! -e "${target}" ]; then
curl -s -G -H "Authorization:Bearer $2" "${WALLABAG_URL}/api/entries/${id}/export.epub" > "${target}"
fi
done
}
token="$(curl -s -d "grant_type=password" -d "client_id=${WALLABAG_ID}" -d "client_secret=${WALLABAG_SECRET}" -d "username=${WALLABAG_USERNAME}" -d "password=${WALLABAG_PASSWORD}" ${WALLABAG_URL}/oauth/v2/token)"
access_token="$(echo "${token}" | jq -r '.access_token')"
perpage=10
first="$(curl -s -G -H "Authorization:Bearer ${access_token}" -d "perPage=${perpage}" "${WALLABAG_URL}/api/entries")"
pages="$(echo "${first}" | jq -r ".pages")"
page="$(echo "${first}" | jq -r ".page")"
archive_batch "${first}" "${access_token}"
while [ $page -lt $pages ]; do
page=$((page+1))
entries="$(curl -s -G -H "Authorization:Bearer ${access_token}" -d "perPage=${perpage}" -d "page=${page}" "${WALLABAG_URL}/api/entries")"
archive_batch "${entries}" "${access_token}"
done