Skip to content

Instantly share code, notes, and snippets.

@petermolnar
Created August 5, 2019 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petermolnar/988ba2fa2770b71a443e437cd4052aeb to your computer and use it in GitHub Desktop.
Save petermolnar/988ba2fa2770b71a443e437cd4052aeb to your computer and use it in GitHub Desktop.
exporting entries from wallabag
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment