Created
December 1, 2008 08:19
-
-
Save jdhuntington/30677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'mechanize' | |
require 'digest/md5' | |
BASE_URL = 'http://www.instapaper.com' | |
LOGIN_URL = 'http://www.instapaper.com/user/login' | |
USERNAME = ARGV.first | |
unless USERNAME && !USERNAME.empty? | |
STDERR.puts("Need a username (email)") | |
exit 1 | |
end | |
Story = Struct.new(:title, :contents, :filename) | |
def log(msg) | |
STDERR.puts(msg) | |
end | |
def get_articles(hp_doc) | |
hp_doc/'#unread_articles div.article_bar' | |
end | |
def get_story(hp_article, agent) | |
title = (hp_article/'a')[2].inner_html | |
text_url = (hp_article/'a')[1].attributes['href'] | |
log "Getting contents of #{title}..." | |
contents = get_contents(text_url, agent) | |
Story.new(title, contents) | |
end | |
def get_contents(url, agent) | |
agent.get(url).root.to_html | |
end | |
doc = nil | |
agent = WWW::Mechanize.new do |agent| | |
agent.user_agent_alias = 'Mac Safari' | |
agent.follow_meta_refresh = true | |
end | |
log "Logging in..." | |
agent.get(LOGIN_URL) do |page| | |
result = page.form_with(:action => "/user/login") do |f| | |
f.username = USERNAME | |
end.submit | |
doc = result.root | |
end | |
log "Getting articles..." | |
stories = get_articles(doc).collect { |article| get_story(article, agent) } | |
stories.each do |story| | |
story.filename = "Story_" + Digest::MD5.hexdigest("#{story} #{Time.new.to_s}") + ".html" | |
File.open(story.filename, 'w') do |f| | |
f.puts story.contents | |
end | |
end | |
log "Writing index..." | |
File.open("Stories.html", 'w') do |f| | |
stories.each do |s| | |
f.puts "<li><a href=\"#{s.filename}\">#{s.title}</a>" | |
end | |
end | |
log "Done." | |
log "Spawning 'html2lrf'." | |
`html2lrf Stories.html` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment