Created
January 26, 2012 12:41
-
-
Save mxswd/1682602 to your computer and use it in GitHub Desktop.
Import articles from a feed into Instapaper
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
# gem install faraday nokogiri | |
require 'faraday' | |
require 'nokogiri' | |
START_PAGE = "/Articles/Classic-WTF-What-Is-Truth.aspx" | |
USERNAME = "INSTAPAPER USERNAME" | |
PASSWORD = "INSTAPAPER PASSWORD" | |
$conn = Faraday.new(:url => "https://www.instapaper.com") do |builder| | |
builder.request :url_encoded | |
builder.adapter :net_http | |
end | |
def add_to_instapaper(url) | |
resp = add_result = $conn.get do |req| | |
req.url "/api/add" | |
req.params["url"] = url | |
req.params["username"] = USERNAME | |
req.params["password"] = PASSWORD | |
end | |
if resp.status == 201 | |
else | |
puts "!! Failed to add #{url}" | |
puts resp.body | |
raise "HTTPFailedYou" | |
end | |
end | |
URL = "http://thedailywtf.com/" | |
tdwtf = Faraday.new(:url => URL) do |builder| | |
builder.request :url_encoded | |
builder.adapter :net_http | |
end | |
def get_next_page(page) | |
doc = Nokogiri::HTML(page) | |
doc.xpath('//a[@rel="prev"]').first.attributes["href"] | |
end | |
page = tdwtf.get(START_PAGE) | |
while true | |
next_url = get_next_page(page.body) | |
add_to_instapaper(URL + next_url) | |
puts "+ #{next_url}" | |
page = tdwtf.get(next_url) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment