commit 3c0cfd82acd04517605f862f5856b170cc146fd1
parent c07db9b68a9688eb6c0f8b997beacab213b05ba1
Author: Alex Balgavy <a.balgavy@gmail.com>
Date: Fri, 7 Jun 2019 17:23:47 +0200
medium_reader.rb: simplified Medium articles
Former-commit-id: b474951d1801fb227571642f8afb3b2a012ad90f
Diffstat:
2 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -24,6 +24,7 @@ If you read a script and see some improvements that could be made, let me know.
* `it-style`: can find and set the genre in a track in iTunes (along with its Ruby helper), using the Discogs database (you need an API key, set $DISCOGS_API_TOKEN in your profile). Usage: click/play a track in iTunes, and run the program.
* `linkdir`: symlink all files/folders from source dir to target dir. Usage: `linkdir $source` to create symlinks in cwd, `linkdir $source $target` otherwise.
* `mdvl`: markdown renderer in Python
+* `medium_reader.rb`: create a simplified Medium article and open it in a browser. Pass the URL as argument, or run interactively.
* `modified_cfscrape.py`: can scrape and download CloudFlare-protected websites. Usage: `python3 modified_cfscrape.py http://url.com`
* `mp3tagger.jar`: a Java application to tag MP3 files
* `pass-import-txt`: imports passwords into `pass` from a text file that's formatted as `username:password` (newline-separated)
diff --git a/scripts/medium_reader.rb b/scripts/medium_reader.rb
@@ -0,0 +1,39 @@
+#!/usr/bin/env ruby
+
+require 'open-uri'
+require 'nokogiri'
+require 'tempfile'
+
+if ARGV.empty?
+ print "Enter url: "
+ url = gets.chomp
+else
+ url = ARGV[0]
+end
+
+if ! URI.parse(url).host.downcase.include? 'medium.com'
+ abort "Not a Medium page."
+end
+doc = Nokogiri(open url)
+
+f = Tempfile.new(['article', '.html'])
+begin
+ f.puts "<!DOCTYPE html><html><head>"
+ doc.css('head meta').each do |e|
+ f.puts e.to_html
+ end
+ doc.css('head link').each do |e|
+ f.puts e.to_html
+ end
+ f.puts doc.css('head title').first.to_html
+ f.puts "</head><body>"
+ doc.css('main div.section-content').each do |e|
+ f.puts e.to_html
+ end
+ f.puts "</body></html>"
+
+ `open 'file://#{f.path}' && sleep 5`
+ensure
+ f.close
+ f.unlink
+end