开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 7276|回复: 6
收起左侧

[技术专题] Gitment/Gitalk自动初始化

[复制链接]
发表于 2019-6-26 18:01:42 | 显示全部楼层 |阅读模式   北京市北京市
一个ruby脚本解放你双手啪啪啪的敲键盘三分钟~
PS.
  • ruby新手,轻拍。。
  • 三是虚指。。
起因
之前看到人家的私站都是用的GitHub做的评论系统。。很想要,但是人家没有用pages这样的玩意。。
今天看到味精大佬的RSS跪了,然后看到人家用的是GitHub的评论。。顿时觉得想要,23333
然后看了一哈 是这个:gitment
配置
这个我就不说了,作者有写中文文档,看一眼就明白了。
列一下我的使用NexT主题的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Gitment
# Introduction: https://imsun.net/posts/gitment-introduction/
gitment:
  enable: true
  mint: true # RECOMMEND, A mint on Gitment, to support count, language and proxy_gateway
  count: true # Show comments count in post meta area
  lazy: false # Comments lazy loading with a button
  cleanly: false # Hide 'Powered by ...' on footer, and more
  language: # Force language, or auto switch by theme
  github_user: xxxxxx # MUST HAVE, Your Github ID
  github_repo: xxxxxx # MUST HAVE, The repo you use to store Gitment comments
  client_id: xxxxxx # MUST HAVE, Github client id for the Gitment
  client_secret: xxxxxx # EITHER this or proxy_gateway, Github access secret token for the Gitment
  proxy_gateway: # Address of api proxy, See: https://github.com/aimingoo/intersect
  redirect_protocol: # Protocol of redirect_uri with force_redirect_protocol when mint enabled
自动初始化
关于作者的初始化评论框方案讨论还在讨论中。。。
但是我要用。。等是不可能等的了。
看到自动初始化 Gitalk 和 Gitment 评论的脚本想着刚好我的也是自动发布、备份,这不是刚好嘛。。
但是存在多次执行就会多次创建的问题。这不是我想要的。
第一版:支持多次执行
GitHub提供较为完善的API,用我这水水的rb水平,大致可以完善如下:
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文件下,防止泄漏。。

username = "madordie" # GitHub 用户名
token = `cat .git-token`  # GitHub Token
repo_name = "madordie.github.io" # 存放 issues
sitemap_url = "https://raw.githubusercontent.com/madordie/madordie.github.io/master/sitemap.xml" # sitemap 此处由于github.io不是立马生效,但是仓库是立马生效的,所以此处应该更换为仓库的raw
kind = "gitment" # "Gitalk" or "gitment"

require 'open-uri'
require 'faraday'
require 'active_support'
require 'active_support/core_ext'
require 'sitemap-parser'

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 = 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}"

    puts "正在创建: #{url}"
    if JSON.parse(response.body)['total_count'] > 0
      puts "\t↳ 已存在"
      `echo #{url} >> .commenteds`
      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], title: title }.to_json
      end
      if JSON.parse(response.body)['number'] > 0
        `echo #{url} >> .commenteds`
        puts "\t&#8627; 已创建成功"
      else
        puts "\t&#8627; #{response.body}"
      end
    end
  end
end
脚本OK,还需要安装一些库用这个就行:
1
sudo gem install faraday activesupport sitemap-parser
正常情况都会安装成功,那么跑一下脚本吧:
1
ruby comment.rb
第一次运行请求多,稍微等一会。表急。。
跑完之后如果你的链接总长度都是 <= 50 字符,那么真嗨,这就行了。
但是如果以后有可能 > 50,或者不确定以后会不会写一个链接贼长的文章,那么你可能还要往下再看一下。。
第二版:兼容文章链接很长长长
关于这个的讨论很多,在issues中搜一下大约这样:Error: Validation Failed
这个Error: Validation Failed就是label太长。
关于这个问题在API: Create a label并未提及。
但是在任何一个仓库下,按照Issues -> New label的时候,输入的Label name是有限制的,输入超过50个自符之后便无法再接收输入。就酱,没找到什么文档。。
看了这个Validation Failed ID长度问题建议之后觉得,MD5一下吧那就。。
为了选择一个KEY去MD5,顺便解决一下同一个页面,带锚点#more会初始化一条新的issue这个问题,
所以KEY使用关于hexo博客单篇文章初始化两次的问题中提出的window.location.pathname吧,但是关于/的讨论,我这里貌似并没有看到,我的都是有的&#128514;。。如果看到的话再更,或者保险期间,先按照这种方案更新一下。
将上面的做完,现在的rb应该长这个样子:
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&#8627; 已创建成功"
      else
        puts "\t&#8627; #{response.body}"
      end
    end
  end
end
同时别忘了修改对应的网页。。我这里使用的是NexT.Pisces v5.1.4
需要修改/themes/next/layout/_third-party/comments/gitment.swig文件,由于JS不支持MD5,所以还需要引入一个JS,于是乎大约这样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    {% if theme.gitment.mint %}
        {% set CommentsClass = "Gitmint" %}
        <link rel="stylesheet" >
        <script src="https://aimingoo.github.io/gitmint/dist/gitmint.browser.js"></script>
+       <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
    {% else %}
        {% set CommentsClass = "Gitment" %}
        <link rel="stylesheet" >
        <script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
+       <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
    {% endif %}
...
        var gitment = new {{CommentsClass}}({
-           id: document.location.href,
+           id: md5(window.location.pathname.replace(/index.html/, "")),
            owner: '{{ theme.gitment.github_user }}',
            repo: '{{ theme.gitment.github_repo }}',
至于这个MD5的引入,我是随便搜的一个。。这个if theme.gitment.mint我并不知道在哪里配置的,所以俩都加上吧。
执行一下脚本吧,应该齐活了。


结帖率:65% (22/34)

签到天数: 1 天

发表于 2019-12-8 12:09:37 | 显示全部楼层   安徽省芜湖市
感谢分享。。。。。。。。。
回复 支持 反对

使用道具 举报

结帖率:65% (22/34)

签到天数: 1 天

发表于 2019-12-8 12:07:47 | 显示全部楼层   安徽省芜湖市
感谢分享。。。。。。。。。
回复 支持 反对

使用道具 举报

结帖率:65% (22/34)

签到天数: 1 天

发表于 2019-12-8 12:06:58 | 显示全部楼层   安徽省芜湖市
感谢分享。。。。。。。。。
回复 支持 反对

使用道具 举报

结帖率:65% (22/34)

签到天数: 1 天

发表于 2019-12-8 12:06:35 | 显示全部楼层   安徽省芜湖市
66666666666
回复 支持 反对

使用道具 举报

发表于 2019-9-13 05:37:09 | 显示全部楼层   广东省深圳市

感谢发布原创作品,精易因你更精彩!s
回复 支持 反对

使用道具 举报

结帖率:81% (71/88)

签到天数: 13 天

发表于 2019-6-26 22:01:56 | 显示全部楼层   重庆市重庆市
感谢分享。。。。。。。。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表