Files
davideisinger.com/bin/newsletter
2024-02-26 10:28:34 -05:00

59 lines
1.3 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require "httparty"
require "nokogiri"
require "open-uri"
feed = Nokogiri::XML(
URI.open("https://davideisinger.com/index.xml")
)
post = feed.search("rss channel item").first
title = post.search("title").text
url = post.search("link").text
body = post.search("description").text
body = %(<h1>#{title}</h1>
<p>
By David Eisinger &middot;
<a href="#{url}">View original post</a>
</p>
#{body})
# escape curly braces (they're important in listmonk)
body = body
.gsub("{", "&#123;")
.gsub("}", "&#125;")
# half-size images
body = body
.gsub(/width="(\d+)"/) { %(width="#{$1.to_i/2}") }
.gsub(/height="(\d+)"/) { %(height="#{$1.to_i/2}") }
# replace audio tags with links
body = body.gsub(/<audio controls src="([^"]+)"><\/audio>/) do
filename = $1.split("/").last
%(<a href="#{$1}">#{filename}</a>)
end
# remove footnote jump links
body = body
.gsub(/<a href="#fn[^>]+>(\d+)<\/a>/) { $1 }
.gsub(/&#160;<a href="#fnref:[^>]+>&#x21a9;&#xfe0e;<\/a>/, "")
p HTTParty.post(
"https://dispatch.davideisinger.com/api/campaigns",
basic_auth: {
username: ENV["USERNAME"],
password: ENV["PASSWORD"]
},
headers: { 'Content-Type' => 'application/json' },
body: {
name: title,
subject: title,
body: body,
lists: [1]
}.to_json,
debug_output: $stdout
)