Home Git 根据blog,自动生成新的blog的内容并且gitamend修改日期和时间jekyll
Post
Cancel

Git 根据blog,自动生成新的blog的内容并且gitamend修改日期和时间jekyll

脚本:

用来从siwei.me 读取新的数据, 并且生成 _posts 文件夹下面的一系列文章。 l

# cat script/generate_blog_files.rb
# encoding: UTF-8
#
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'production'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'rails'
require 'rubygems'

FOLDER = './posts'
start_date_string = "2017-07-01"
#git_commit_name = 'yates'
#git_commit_email = '[email protected]'

start_date = DateTime.strptime(start_date_string, '%Y-%m-%d')
end_date = Time.now()

all_posts_count = Refinery::Blog::Post.where('title like ? or body like ? or body like ?', "%sec%", "%security%", "%sqli%").count
days = (end_date - start_date).to_i / (24 * 60 * 60)
days_step = (days / all_posts_count).to_i

puts "=== days: #{days}"
puts "=== days_step: #{days_step}"
puts "=== all_posts_count: #{all_posts_count}"

#   Refinery::Blog::Post Load (1.7ms)  SELECT `refinery_blog_posts`.* FROM `refinery_blog_posts` ORDER BY published_at ASC LIMIT 1
#   => #<Refinery::Blog::Post id: 639,
#   title: "应用开源项目时,你会大肆封装,修改它吗?",
#   body: "<div id=\"blog_content\" class=\"blog_content\">\r\n    趁...",
#   draft: false, published_at: "2007-06-12 08:52:00", created_at: "2007-06-12 08:52:00", updated_at: "2015-01-20 03:00:43", user_id: 1, cached_slug: nil, custom_url: "", custom_teaser: "", source_url: "", source_url_title: "", access_count: 2094, slug: "--3">
Refinery::Blog::Post.where(
    'title like ? or title like ? or title like ? or title like ? or title like ? or title like ? ' +
    ' or body like ? or body like ? or body like ? or body like ? or body like ? or body like ?',
    '%反编译%', "%黑客%", "%hacker%", "%sqli%", "%vmware%", '%metasploit%',
    "%反编译%", "%黑客%", "%hacker%", "%sqli%", "%vmware%", '%metasploit%',
  ).to_a.reverse().each_with_index do |blog, index|
  puts "== blog.published_at: #{blog.published_at}"

  # 每次随机 10 +- 5, 也就是[-5, 5] 之间进行 日期的浮动
  date = start_date + (index * days_step) + ( rand(2) - 2 )
  title = "#{date.strftime('%Y-%m-%d')}-#{blog.title.gsub("[**]",'').gsub("security", '').gsub(' ', '').gsub('/', '')}.html"

  full_file_name = FOLDER + '/' + title
  File.open(full_file_name, "w+") { |file| file.write blog.body }
  puts "#{full_file_name} generated."
  break if date > Time.now()
end

运行结果如下:

根据 _posts 文件夹下的一系列的 html 文件,生成git commit, 并且及时的amend

run_amend_git.rb:

require 'date'

IS_DRY_RUN = false
FOLDER = "_posts"

Dir.glob(FOLDER + '/**').select{ |e| File.file?(e) }.each do |file|
  file = file.gsub("#{FOLDER}/", '')
  temp = file[0,10]
  puts "=== temp: #{temp}"
  date = Date.strptime temp, "%Y-%m-%d"
  hour = rand(12) + 8
  minute = rand(59)
  second = rand(59)
  git_date_time = "#{date.strftime("%d %l %Y")} #{hour}:#{minute}:#{second}"
  puts "--- file: #{file}"
  puts "git_date_time: #{git_date_time}"

  #git_command = %Q{GIT_COMMITTER_DATE="2 Mar 2022 20:19:19" git commit --amend --no-edit --date "2 Mar 2022 20:19:19 "}
  #git_command = %Q{GIT_COMMITTER_DATE="#{git_date_time}" git commit --amend --no-edit --date "git_date_time"}
  commit_command = %Q{git add "#{FOLDER}/#{file}" && git commit -m "add blog"}
  amend_command = %Q{GIT_COMMITTER_DATE="#{git_date_time}" git commit --amend --no-edit --date "#{git_date_time}"}
  puts "commit_command: #{commit_command}"
  puts "amend_command: #{amend_command}"

  unless IS_DRY_RUN
    `#{commit_command}`
    `#{amend_command}`
  end

  sleep 0
end

非常好看:

git log 看起来很丰满:

This post is licensed under CC BY 4.0 by the author.
Contents