davideisinger.com

My personal website
Log | Files | Refs | README

newsletter (1326B)


      1 #!/usr/bin/env ruby
      2 
      3 require "httparty"
      4 require "nokogiri"
      5 require "open-uri"
      6 
      7 feed = Nokogiri::XML(
      8   URI.open("https://davideisinger.com/index.xml")
      9 )
     10 
     11 post = feed.search("rss channel item").first
     12 title = post.search("title").text
     13 url = post.search("link").text
     14 body = post.search("description").text
     15 
     16 body = %(<h1>#{title}</h1>
     17 <p>
     18   By David Eisinger &middot;
     19   <a href="#{url}">View original post</a>
     20 </p>
     21 #{body})
     22 
     23 # escape curly braces (they're important in listmonk)
     24 body = body
     25   .gsub("{", "&#123;")
     26   .gsub("}", "&#125;")
     27 
     28 # half-size images
     29 body = body
     30   .gsub(/width="(\d+)"/) { %(width="#{$1.to_i/2}") }
     31   .gsub(/height="(\d+)"/) { %(height="#{$1.to_i/2}") }
     32 
     33 # replace audio tags with links
     34 body = body.gsub(/<audio controls src="([^"]+)"><\/audio>/) do
     35   filename = $1.split("/").last
     36   %(<a href="#{$1}">#{filename}</a>)
     37 end
     38 
     39 # remove footnote jump links
     40 body = body
     41   .gsub(/<a href="#fn[^>]+>(\d+)<\/a>/) { $1 }
     42   .gsub(/&#160;<a href="#fnref:[^>]+>&#x21a9;&#xfe0e;<\/a>/, "")
     43 
     44 p HTTParty.post(
     45   "https://dispatch.davideisinger.com/api/campaigns",
     46   basic_auth: {
     47     username: ENV["USERNAME"],
     48     password: ENV["PASSWORD"]
     49   },
     50   headers: { 'Content-Type' => 'application/json' },
     51   body: {
     52     name: title,
     53     subject: title,
     54     body: body,      
     55     lists: [1]
     56   }.to_json,
     57   debug_output: $stdout
     58 )