代码拉取完成,页面将自动刷新
# from : https://madordie.github.io/post/blog-gitment-auto-setup
# 另外,token已放在.git-token文件下,并被.gitignore标记,防止泄漏。。
username = "madordie" # GitHub 用户名
token = `cat .git-token` # GitHub Token
repo_name = "madordie.github.io" # 存放 issues
sitemap_url = "https://madordie.github.io/sitemap.xml" # sitemap
kind = "gitment" # "Gitalk" or "gitment"
require 'open-uri'
require 'faraday'
require 'active_support'
require 'active_support/core_ext'
require 'sitemap-parser'
require 'digest'
puts "正在检索URL"
sitemap = SitemapParser.new sitemap_url
urls = sitemap.to_a
puts "检索到文章共#{urls.count}个"
conn = Faraday.new(:url => "https://api.github.com") do |conn|
conn.basic_auth(username, token)
conn.headers['Accept'] = "application/vnd.github.symmetra-preview+json"
conn.adapter Faraday.default_adapter
end
commenteds = Array.new
`
if [ ! -f .commenteds ]; then
touch .commenteds
fi
`
File.open(".commenteds", "r") do |file|
file.each_line do |line|
commenteds.push line
end
end
urls.each_with_index do |url, index|
url.gsub!(/index.html$/, "")
if commenteds.include?("#{url}\n") == false
url_key = Digest::MD5.hexdigest(URI.parse(url).path)
response = conn.get "/search/issues?q=label:#{url_key}+state:open+repo:#{username}/#{repo_name}"
if JSON.parse(response.body)['total_count'] > 0
`echo #{url} >> .commenteds`
else
puts "正在创建: #{url}"
title = open(url).read.scan(/<title>(.*?)<\/title>/).first.first.force_encoding('UTF-8')
response = conn.post("/repos/#{username}/#{repo_name}/issues") do |req|
req.body = { body: url, labels: [kind, url_key], title: title }.to_json
end
if JSON.parse(response.body)['number'] > 0
`echo #{url} >> .commenteds`
puts "\t↳ 已创建成功"
else
puts "\t↳ #{response.body}"
end
end
end
end
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。