Created
March 3, 2011 13:40
-
-
Save namutaka/852782 to your computer and use it in GitHub Desktop.
Import from a rss xml file to ReadItLater.
This file contains hidden or 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
#! /opt/local/bin/ruby -Ku | |
require 'kconv' | |
require 'cgi' | |
require "rexml/document" | |
require "net/http" | |
require "uri" | |
uid = ARGV[0] | |
password = ARGV[1] | |
hatena_id = ARGV[2] | |
if uid.nil? or password.nil? or hatena_id.nil? | |
print "Invalid arguments: uid='#{uid}', password='#{password}', hatena_id='#{hatena_id}'" | |
exit 1 | |
end | |
login_host = "readitlaterlist.com" | |
hatena_rss_host = "b.hatena.ne.jp" | |
hatena_rss_path = "/#{hatena_id}/rss?tag=*%E3%81%82%E3%81%A8%E3%81%A7%E8%AA%AD%E3%82%80" | |
cookies = nil | |
Net::HTTP.start(login_host, 80) do |http| | |
response = http.post("/login_process/", "feed_id=#{uid}&password=#{password}") | |
cookies = response.get_fields('Set-Cookie') | |
end | |
uri = URI.parse("http://readitlaterlist.com/edit_process.php?BL=") | |
header = { | |
"user-agent" => "Ruby/#{RUBY_VERSION} MyHttpClient", | |
"cookie" => cookies.map{|v| v[0...v.index(';')]}.join("; ") | |
} | |
source = Net::HTTP.get(hatena_rss_host, hatena_rss_path) | |
REXML::Document.new(source).elements.each('//item') do |elm| | |
elms = elm.elements | |
item = { | |
:title => elms["title"].text, | |
:url => elms["link"].text, | |
:tags => elms.each("dc:subject") {}.map(&:text).join(","), | |
:ref => "http://readitlaterlist.com/add" | |
} | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
body = item.map {|k,v| "#{k}=#{CGI.escape(v)}"}.join("&") | |
response = http.post(uri.path, body, header) | |
puts (response["location"] == "/unread" ? "success" : "failed") + ": " + item[:url] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment