Skip to content

Instantly share code, notes, and snippets.

@henteko
Last active November 15, 2015 06:32
Show Gist options
  • Save henteko/28a5557a8655de8ae767 to your computer and use it in GitHub Desktop.
Save henteko/28a5557a8655de8ae767 to your computer and use it in GitHub Desktop.
# A sample Gemfile
source "https://rubygems.org"
gem 'atomutil', :github => 'henteko/ruby-atomutil'
#!/usr/local/bin/ruby
# -*- encoding: utf-8 -*-
require 'json'
require 'find'
require 'atomutil'
class Tumblr
attr_reader :title, :date, :body
def initialize(title, date, body)
@title = title
@date = Time.parse(date).utc.iso8601
@body = body
end
def self.read(file_path)
title = ''
date = ''
body = ''
conf_flag = false
blank_flag = false
blank_count = 0
File.open(file_path) do |file|
file.each_line do |labmen|
if md = labmen.match(/^raw: (.*)/)
json = md[1]
hash = JSON.parse(json)
title = hash["title"]
date = hash["date"]
end
body += labmen if blank_flag
# タイトル部分を本文から除外する(改行含めて5行ある)
if conf_flag && !blank_flag
blank_count += 1
blank_flag = true if blank_count >= 5
end
# -->から上は除外する
conf_flag = true if labmen.match(/^-->/)
end
end
Tumblr.new(title, date, body)
end
def self.export_files
base_path = '/path/to/tumblr_export_dir'
results = []
Find.find(base_path) do |path|
next if path == base_path
if File.extname(path) == '.md'
results.push(path)
end
end
results
end
end
class HatenaBlog
def initialize(user_name, blog, password)
@user_name = user_name
@blog = blog
@password = password
@url = "http://blog.hatena.ne.jp/#{@user_name}/#{@blog}/atom/entry"
auth = Atompub::Auth::Wsse.new(
:username => @user_name,
:password => @password
)
@client = Atompub::Client.new(:auth => auth)
end
def upload(title, date, content)
entry = Atom::Entry.new(
:title => title,
:updated => date.to_s,
:content => content)
@client.create_entry(@url, entry)
end
end
hatena = HatenaBlog.new('hatena_user_naem', 'blog_domain', 'password')
Tumblr.export_files.each do |file_path|
tumblr = Tumblr.read(file_path)
p hatena.upload(tumblr.title, tumblr.date, tumblr.body)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment