Last active
February 22, 2025 05:48
-
-
Save shtrom/2b20d32aa9311301e9137053becab795 to your computer and use it in GitHub Desktop.
Bash script to post a local HTML file to a Wallabag instance https://blog.narf.ssji.net/2025/01/17/manually-sending-document-contents-to-wallabag/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -u | |
# Bash script to post a local HTML file to a Wallabag instance | |
# | |
# This assumes client and user credentials are in a pass(1) entry formatted as | |
# follows: | |
# | |
# <PASSWORD> | |
# login: <USERNAME> | |
# client_id: <CLIENT_ID> | |
# client_secret: <CLIENT_SECRET> | |
# | |
# This can also be cajoled as a (Neo)Mutt filter with, e.g., | |
# | |
# macro attach,index,pager W "\ | |
# <pipe-message>~/bin/post_to_wallabag.sh -u $URL<Enter>"\ | |
# "Post to Wallabag" | |
# | |
# This requires a `:setenv URL ...` before calling the macro, as mutt macros | |
# don't support interactive input [0]. | |
# | |
# [0] https://github.com/neomutt/neomutt/issues/3665 | |
# | |
# SPDX-License-Identifier: BSD-3-Clause | |
# | |
# Copyright (c) 2025, Olivier Mehani <[email protected]> | |
# All rights reserved. | |
# Replace the next two variables with actual values for your instance. | |
WB_URL=${WALLABAG_URL} | |
PASS_ENTRY=${WALLABAG_PASS_ENTRY} | |
ARGS=$(getopt -o 'f:hit:u:' --long 'file:,help,interactive,title:,url:' -n "${0}" -- "$@") | |
eval set -- "$ARGS" | |
unset ARGS | |
FILE= | |
INTERACTIVE= | |
URL= | |
TITLE= | |
while :; do | |
case "$1" in | |
'-f'|'--file') | |
FILE=${2} | |
shift 2 | |
continue | |
;; | |
'-h'|'--help') | |
cat << EOF | |
usage: $(basename "$0") [-i|--interactive] [-f|--file FILE] [-t|--title TITLE] [-u|-url URL] | |
EOF | |
exit | |
;; | |
'-i'|'--interactive') | |
shift 1 | |
INTERACTIVE=1 | |
;; | |
'-t'|'--title') | |
TITLE=${2} | |
shift 2 | |
continue | |
;; | |
'-u'|'--url') | |
URL=${2} | |
shift 2 | |
continue | |
;; | |
'--') | |
break; | |
esac | |
done | |
CREDENTIALS=$(pass ${PASS_ENTRY}) | |
CLIENT_ID=$(sed -n 's/client_id: *//p' <<< "${CREDENTIALS}") | |
CLIENT_SECRET=$(sed -n 's/client_secret: *//p' <<< "${CREDENTIALS}") | |
USERNAME=$(sed -n 's/login: *//p' <<< "${CREDENTIALS}") | |
PASSWORD=$(head -n 1 <<< "${CREDENTIALS}") | |
WB_TOKEN=$(curl -s -XPOST ${WB_URL}/oauth/v2/token \ | |
-d grant_type=password \ | |
-d "client_id=${CLIENT_ID}" \ | |
-d "client_secret=${CLIENT_SECRET}" \ | |
-d "username=${USERNAME}" \ | |
-d "password=${PASSWORD}" \ | |
| jp -u @.access_token) | |
CONTENT="$(sed '/gmail_quote gmail_quote_container/d' ${FILE})" | |
if [ -n "${INTERACTIVE}" ]; then | |
read | |
test -z "${TITLE}" && echo -n "Title: " && read TITLE | |
test -z "${URL}" && echo -n "URL: " && read URL | |
fi | |
# shellcheck disable=SC2086 | |
curl -q -XPOST ${WB_URL}/api/entries.json \ | |
--oauth2-bearer "${WB_TOKEN}" \ | |
${TITLE:+--data "title=${TITLE}"} \ | |
${URL:+--data "url=${URL}"} \ | |
--data-urlencode "content=${CONTENT}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment