Created
May 9, 2012 07:24
-
-
Save mahm/2642659 to your computer and use it in GitHub Desktop.
はてなブックマークのエクスポートデータをPinboardにインポートする
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
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
# | |
# ** Hatebu2Pinboard ** | |
# はてブのAtomエクスポートデータをPinboardにインポートします。 | |
# カレントディレクトリにエクスポートデータ(dump.yml)と、 | |
# Pinboardのユーザ名、パスワードを書いたYAMLファイル(auth.yml)を | |
# 用意して下さい。 | |
# | |
# [usage] | |
# ruby hatebu2pinboard.rb | |
# | |
# [auth.yml例] | |
# username: "your_username" | |
# password: "your_password" | |
# | |
require 'nokogiri' | |
require 'time' | |
require 'pp' | |
require 'cgi' | |
require 'yaml' | |
module Hatebu2Pinboard | |
class HatebuAtomParser | |
attr_accessor :bookmarks | |
def initialize(dump_file_name) | |
@doc = Nokogiri::XML(File::open(dump_file_name).read) | |
@bookmarks = Array.new | |
parse | |
end | |
private | |
def parse | |
@doc.css('feed entry').each do |feed| | |
@bookmarks << Bookmark.new(feed) | |
end | |
end | |
end | |
class Bookmark | |
def initialize(node_set) | |
@node_set = node_set | |
end | |
def description | |
@node_set.search('title').children.first.to_s | |
end | |
def url | |
@node_set.search('link').first.attr('href') | |
end | |
def extended | |
@node_set.search('summary').children.first.to_s | |
end | |
def dt | |
Time.parse(@node_set.search('issued').children.first.to_s).to_s | |
end | |
def tags | |
@node_set.search('dc|subject').children.map{ |text| text.to_s }.join(' ') | |
end | |
end | |
class Executer | |
def self.execute(bookmarks) | |
bookmarks.each do |bm| | |
args = { | |
url: bm.url, | |
description: bm.description, | |
extended: bm.extended, | |
tags: bm.tags, | |
dt: bm.dt | |
} | |
query = "?" + args.map{ |k, v| "#{k}=#{CGI.escape(v)}" }.join('&') | |
`curl "#{url}#{query}"` | |
end | |
end | |
def self.auth | |
@@auth ||= YAML.load(File::open('auth.yml').read) | |
end | |
def self.url | |
"https://#{auth["username"]}:#{auth["password"]}@api.pinboard.in/v1/posts/add" | |
end | |
end | |
end | |
if __FILE__ == $0 | |
parser = Hatebu2Pinboard::HatebuAtomParser.new('dump.xml') | |
Hatebu2Pinboard::Executer.execute(parser.bookmarks) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment