Skip to content

Instantly share code, notes, and snippets.

@labocho
Created October 3, 2014 07:20
Show Gist options
  • Select an option

  • Save labocho/4f4fe567ccf49bb85885 to your computer and use it in GitHub Desktop.

Select an option

Save labocho/4f4fe567ccf49bb85885 to your computer and use it in GitHub Desktop.
Kobito.db から Kobito 2.0 の同期用 JSON へエクスポート
#!/usr/bin/env ruby
unless ARGV.first
STDERR.puts "Usage #{$0} ~/Library/Kobito/Kobito.db"
exit 1
end
gem "activerecord", "4.1.6"
gem "composite_primary_keys", "7.0.10"
gem "sqlite3", "1.3.9"
require "active_record"
require "composite_primary_keys"
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: ARGV.first
)
class Item < ActiveRecord::Base
self.table_name = "ZITEM"
self.primary_keys = :Z_PK
alias_attribute :id, :Z_PK
alias_attribute :ent, :Z_ENT
alias_attribute :opt, :Z_OPT
alias_attribute :in_trash, :ZIN_TRASH
alias_attribute :private, :ZPRIVATE
alias_attribute :team, :ZTEAM
alias_attribute :created_at, :ZCREATED_AT
alias_attribute :posted_at, :ZPOSTED_AT
alias_attribute :updated_at, :ZUPDATED_AT
alias_attribute :updated_at_on_qiita, :ZUPDATED_AT_ON_QIITA
alias_attribute :body, :ZBODY
alias_attribute :key, :ZKEY
alias_attribute :linked_file, :ZLINKED_FILE
alias_attribute :raw_body, :ZRAW_BODY
alias_attribute :title, :ZTITLE
alias_attribute :url, :ZURL
alias_attribute :uuid, :ZUUID
belongs_to :team, foreign_key: :ZTEAM
has_many :taggings, foreign_key: :Z_1ITEMS
has_many :tags, through: :taggings
def created_at
convert_time self[:ZCREATED_AT]
end
def posted_at
convert_time self[:POSTED_AT]
end
def updated_at
convert_time self[:ZUPDATED_AT]
end
def updated_at_on_qiita
convert_time self[:ZUPDATEDAT_ON_QIITA]
end
def private
self[:ZPRIVATE] == 1
end
def in_trash
self[:ZIN_TRASH] == 1
end
def as_json
{ private: private,
posted_at: json_time(posted_at),
uuid: uuid,
created_at: json_time(created_at),
tags: tags.map(&:name),
url: url,
updated_at_on_qiita: json_time(updated_at_on_qiita),
title: title,
key: key,
updated_at: json_time(updated_at),
raw_body: raw_body,
in_trash: in_trash,
team_url_name: team.try(:url_name)
}
end
def filename
"#{key}.json"
end
private
# http://qiita.com/kenmaz/items/1b170a6aa147a1f0609d
def convert_time(t)
return unless t
Time.at(t + Time.new(2001, 1, 1, 0, 0, 0, 0).to_i).utc
end
def json_time(t)
return unless t
t.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
end
end
class Tag < ActiveRecord::Base
self.table_name = "ZTAG"
self.primary_keys = :Z_PK
alias_attribute :id, :Z_PK
alias_attribute :ent, :Z_ENT
alias_attribute :opt, :Z_OPT
alias_attribute :name, :ZNAME
has_many :taggings, foreign_key: :Z_2TAGS
has_many :items, through: :taggings
end
class Team < ActiveRecord::Base
self.table_name = "ZTEAM"
self.primary_keys = :Z_PK
alias_attribute :id, :Z_PK
alias_attribute :ent, :Z_ENT
alias_attribute :opt, :Z_OPT
alias_attribute :name, :ZNAME
alias_attribute :url_name, :ZURL_NAME
end
class Tagging < ActiveRecord::Base
self.table_name = "Z_1TAGS"
self.primary_keys = :Z_1ITEMS, :Z_2TAGS
alias_attribute :item_id, :Z_1ITEMS
alias_attribute :tag_id, :Z_2TAGS
belongs_to :item, foreign_key: :Z_1ITEMS
belongs_to :tag, foreign_key: :Z_2TAGS
end
class Metadatum < ActiveRecord::Base
self.table_name = "Z_METADATA"
self.primary_keys = :Z_VERSION
alias_attribute :version, :Z_VERSION
alias_attribute :uuid, :Z_UUID
alias_attribute :plist, :Z_PLIST
end
class PrimaryKey < ActiveRecord::Base
self.table_name = "Z_PRIMARYKEY"
self.primary_keys = :Z_ENT
alias_attribute :ent, :Z_ENT
alias_attribute :name, :Z_NAME
alias_attribute :super, :Z_SUPER
alias_attribute :max, :Z_MAX
end
Dir.mkdir("items") unless Dir.exists?("items")
Item.all.each do |item|
json = JSON.pretty_generate(item.as_json)
puts "Writing #{item.filename}"
File.write("items/" + item.filename, json)
end
puts "
1. Launch Kobito.app
2. Enable sync
3. cp items/* ~/Dropbox/Kobito/Kobito\\ Archive/.
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment