medium_reader.rb (729B)
1 #!/usr/bin/env ruby 2 3 require 'open-uri' 4 require 'nokogiri' 5 require 'tempfile' 6 7 if ARGV.empty? 8 print "Enter url: " 9 url = gets.chomp 10 else 11 url = ARGV[0] 12 end 13 14 if ! URI.parse(url).host.downcase.include? 'medium.com' 15 abort "Not a Medium page." 16 end 17 doc = Nokogiri(open url) 18 19 f = Tempfile.new(['article', '.html']) 20 begin 21 f.puts "<!DOCTYPE html><html><head>" 22 doc.css('head meta').each do |e| 23 f.puts e.to_html 24 end 25 doc.css('head link').each do |e| 26 f.puts e.to_html 27 end 28 f.puts doc.css('head title').first.to_html 29 f.puts "</head><body>" 30 doc.css('main div.section-content').each do |e| 31 f.puts e.to_html 32 end 33 f.puts "</body></html>" 34 35 `open 'file://#{f.path}' && sleep 5` 36 ensure 37 f.close 38 f.unlink 39 end