Skip to content

Instantly share code, notes, and snippets.

@s-wool
Forked from kyanny/Rakefile
Created February 27, 2018 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-wool/8efa5db3d170589e407f81ebeb74f4d9 to your computer and use it in GitHub Desktop.
Save s-wool/8efa5db3d170589e407f81ebeb74f4d9 to your computer and use it in GitHub Desktop.
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