加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
自动初始化Gitment评论(实现中).sh 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
思伟 提交于 2019-03-13 13:48 . 添加不蒜子访问量统计功能
# 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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化