-
Notifications
You must be signed in to change notification settings - Fork 2
/
自动初始化Gitment评论(实现中).sh
65 lines (55 loc) · 1.89 KB
/
自动初始化Gitment评论(实现中).sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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