Last active
February 21, 2019 07:26
-
-
Save kako-jun/3f05ba73fcb5f292712860e1327cc268 to your computer and use it in GitHub Desktop.
Google+からエクスポートした投稿を、ブログにコピペしやすいmd形式に変換するRubyスクリプトやよ
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
# coding: utf-8 | |
#!/usr/bin/ruby | |
Dir.chdir( File.expand_path( File.dirname( __FILE__ ) ) ) | |
require 'json' | |
require 'date' | |
target_month = Time.now.strftime('%Y%m') | |
target_month = ARGV[0] unless ARGV.empty? | |
half = 0 | |
half = ARGV[1].to_i unless ARGV.empty? and ARGV.count > 1 | |
# フォルダ内のjsonを配列に | |
posts = {} | |
src_files = Dir.glob('./アクティビティ/**/*.json') | |
src_files.each do |src_file| | |
json = File.read(src_file, :encoding => Encoding::UTF_8) | |
json_data = JSON.parse(json, :quirks_mode => true) | |
published = DateTime.parse(json_data['published']) | |
post_id = File.basename(json_data['url']) | |
embed = "<div class='g-post' data-href='https://plus.google.com/+JpicoInfo/posts/" + post_id + "'></div>" | |
# 配列に蓄える | |
if published.strftime('%Y%m') == target_month then | |
case half | |
when 0 then | |
posts[published.to_s.to_sym] = embed | |
when 1 then | |
if published.strftime('%d').to_i.between?(0, 15) then | |
posts[published.to_s.to_sym] = embed | |
end | |
when 2 then | |
if published.strftime('%d').to_i.between?(16, 31) then | |
posts[published.to_s.to_sym] = embed | |
end | |
end | |
end | |
end | |
# 並び替え | |
posts = Hash[posts.sort.reverse] | |
# 出力 | |
year = target_month[0, 4] | |
month = target_month[4, 5] | |
puts '---' | |
puts "title: 歩み. #{year}-#{month}の歩み#{half}" | |
if half == 1 then | |
puts "date: #{year}-#{month}-15" | |
else | |
puts "date: #{year}-#{month}-" | |
end | |
puts '---' | |
puts '' | |
puts '' | |
puts '' | |
puts 'この期間の' | |
puts '<a href="https://plus.google.com/+JpicoInfo/posts" class="btn btn-danger" target="_blank">Google+</a>' | |
puts 'をまとめたものです。' | |
puts '読み込みにちょっと時間が掛かります。' | |
puts '' | |
posts.each_with_index do |(published, embed), i| | |
puts embed | |
if i < posts.count - 1 then | |
puts '<hr>' | |
end | |
end | |
puts '' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment