davideisinger.com

My personal website
Log | Files | Refs | README

newsletter (1350B)


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