Skip to content

Instantly share code, notes, and snippets.

@craigeley
Last active December 15, 2015 02:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigeley/e8f9c52052dd8f38640c to your computer and use it in GitHub Desktop.
Save craigeley/e8f9c52052dd8f38640c to your computer and use it in GitHub Desktop.
A simple Ruby script that uses the Readability API to save articles for offline viewing and storage. Create a Readability API key at https://www.readability.com/settings/account
#!/usr/bin/ruby
require 'json'
require 'erb'
require 'open-uri'
# Uncomment these lines if you are having encoding issues on OS X Yosemite
# if RUBY_VERSION =~ /2.0.0/
# Encoding.default_external = Encoding::UTF_8
# Encoding.default_internal = Encoding::UTF_8
# end
template = ERB.new <<-TEMPLATE
<head>
<link href="http://path/to/cool/css/css.css" rel="stylesheet"></link>
</head>
<h1> <a href="<%= art_path %>"><%= title %></a></h1>
<%= content %>
TEMPLATE
dropboxpath = "~/Dropbox/Read_Later/"
path = "https://readability.com/api/content/v1/parser?url="
URL = `osascript -e 'tell application "Chrome" to get URL of active tab of first window'`
path = path + URL.strip + "&token=YOUR_API_TOKEN"
path = path.strip
buffer = open(path).read
data = JSON.parse(buffer)
title = data["title"]
art_path = data["url"]
content = data["content"]
fh = File.new(File.expand_path(dropboxpath+"#{title}"+".html"),'w+')
fh.puts template.result(binding)
fh.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment