Skip to content

Instantly share code, notes, and snippets.

@mururu
Created February 13, 2012 07:26
Show Gist options
  • Save mururu/1814609 to your computer and use it in GitHub Desktop.
Save mururu/1814609 to your computer and use it in GitHub Desktop.
twitter favorite -> hatena bookmark
# coding: utf-8
require 'rubygems'
require 'twitter'
require 'net/http'
require 'uri'
require 'digest/sha1'
# WSSE認証
def wsse(id, pass)
nonce = Time.now.to_s
created = Time.now.utc.iso8601
digest = [Digest::SHA1.digest(nonce + created + pass)].pack("m").gsub(/\n/,"")
{"X-WSSE" => "UsernameToken Username=\"#{id}\", PasswordDigest=\"#{digest}\", Nonce=\"#{nonce}\", Created=\"#{created}\""}
end
# はてぶにPOSTするxmlを生成
def body(link, comment = nil)
<<-EOS
<entry xmlns="http://purl.org/atom/ns#">
<title>dummy</title>
<link rel="related" type="text/html" href="#{link}" />
<summary type="text/plain">#{comment}</summary>
</entry>
EOS
end
# はてぶにPOSTしますよ
def post(xml,header)
http = Net::HTTP.new('b.hatena.ne.jp')
code = http.post('/atom/post', xml, header).code
raise "hatena failed to authenticate" if code == '401'
code
end
print "twitter_screenname:"
t_name = gets.chomp
print "hatena_id:"
h_id = gets.chomp
print "hatena_password:"
h_pass = gets.chomp
header = wsse(h_id,h_pass)
begin
failed = []
Twitter.favorites(t_name, :count => 200).map(&:text).inject([]){|a,s|a << URI.extract(s, %w{http https})}.flatten.reverse.map{|u| post(body(u),header) == '201' ? print('.') : failed << u }
puts "failed_list" unless failed.empty?
failed.each{|f|puts f}
rescue Twitter::Error::NotFound
puts "twitter_user #{t_name} is not found"
rescue => e
puts e.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment