Qiita::Team お引越しスクリプト
require 'json' | |
require 'yaml' | |
require 'nokogiri' | |
require 'uri' | |
require 'faraday' | |
require 'ostruct' | |
require 'pry' | |
def y data | |
puts YAML.dump data | |
end | |
def fetch_img(item) | |
html = Nokogiri::HTML.parse(item.body) | |
html.css('img').each_with_index do |node, i| | |
uri = URI.parse(node[:src]) | |
if uri.host.to_s.match(/qiita-image-store/) | |
filename = "#{item.uuid}-#{i}#{File.extname(uri.path)}" | |
File.binwrite("./images/#{filename}", Faraday.get(uri).body) | |
item.raw_body.sub!(/#{uri}/, "./images/#{filename}") | |
end | |
end | |
end | |
def fetch_icon(item) | |
name = item.user.url_name | |
url = item.user.profile_image_url | |
filename = "#{name}#{File.extname(url)}" | |
unless File.exists?(filename) | |
File.binwrite("./images/#{filename}", Faraday.get(url).body) | |
end | |
"./images/#{filename}" | |
end | |
desc '' | |
task :default do | |
system "rm -rf ./data" | |
system "mkdir -p ./data" | |
data = JSON.parse(File.read('data.json'), object_class: OpenStruct) | |
Dir.chdir('./data') do | |
data.articles.each do |article| | |
body = '' | |
author = article.user.url_name | |
title = article.title.gsub(/[\p{P}]/, '-').gsub(/[[:blank:]]/, '-') | |
created_at = Time.at(article.created_at_as_seconds) | |
day = created_at.strftime('%F') | |
filename = "%s-%s-%s.md" % [day, author, title] | |
year = created_at.year | |
month = created_at.month | |
system "mkdir -p #{year}/#{month}/images" | |
Dir.chdir("#{year}/#{month}") do | |
body << "# #{title}\n\n" | |
fetch_img(article) | |
body << "#{article.raw_body}\n\n" | |
icon = fetch_icon(article) | |
system "mogrify -resize 32x #{icon}" | |
body << "Posted at #{created_at} by <img src=\"#{icon}\"> #{author}\n\n" | |
if article.comments.length > 0 | |
article.comments.each do |comment| | |
body << "<hr>\n\n" | |
fetch_img(comment) | |
body << "#{comment.raw_body}\n\n" | |
icon = fetch_icon(comment) | |
system "mogrify -resize 32x #{icon}" | |
body << "Comment posted by <img src=\"#{icon}\"> #{comment.user.url_name}\n\n" | |
end | |
end | |
File.write(filename, body) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment