A Wallabag bookmarking script for Newsboat

· 229 words · 2 minute read

I don’t have any idea what was going wrong where, but I found a “post to Wallabag” (previously) sh script that worked great from the command line, but wouldn’t run within Newsboat. So I took the script apart, converted it to Ruby/httparty, and it runs fine in this form.

Now I have two scripts for bookmarking in Newsboat, but it only has one reserved key for bookmarking a post. It does have macros, though, so you can use one to set the bookmarking command to a given script and bind it to something memorable. My mutt convention is to lead macros with a period, so I kept that here by unbinding Newsboat’s default macro prefix (,) and setting “.”.

So, .l to save to Linkding, .w to save to Wallabag:

# macros
bind-key . macro-prefix
unbind-key ,
macro w set bookmark-cmd "op run --env-file=\"$HOME/.env\" -- ~/bin/wallabag.rb"; bookmark
macro l set bookmark-cmd "op run --env-file=\"$HOME/.env\" -- ~/bin/linkding.rb"; bookmark

And here’s the Wallabag bookmarking script:

#!/usr/bin/env ruby

require 'httparty'
require 'json'

entry_url = ARGV[0]

wallabag_url="https://reader.example.net"

client_id = ENV['WALLABAG_CLIENT']
client_secret = ENV['WALLABAG_SECRET']
username = ENV['WALLABAG_USER']
password = ENV['WALLABAG_PASSWORD']

token_params = {grant_type: "password", client_id: client_id, client_secret: client_secret, username: username, password: password}
token_req = HTTParty.post("#{wallabag_url}/oauth/v2/token", body: token_params)
access_token = token_req["access_token"]

headers = {'Content-Type' => "application/json", 'Authorization' => "Bearer #{access_token}"}
params = {url: entry_url, starred: 0, archive: 0}
resp = HTTParty.post("#{wallabag_url}/api/entries.json", body: params.to_json, headers: headers)