imsun / gitment

A comment system based on GitHub Issues.
https://imsun.github.io/gitment/
MIT License
4.06k stars 350 forks source link

自动初始化 Gitalk/Gitment 评论的脚本 #88

Open draveness opened 7 years ago

draveness commented 7 years ago

昨天写了个自动化初始评论的脚本

username = "draveness" # GitHub 用户名
new_token = "xxxxxxx"  # GitHub Token
repo_name = "github-comments-repo" # 存放 issues
sitemap_url = "https://draveness.me/sitemap.xml" # sitemap
kind = "gitment" # "gitment" or "Gitalk"

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

sitemap = SitemapParser.new sitemap_url
urls = sitemap.to_a

conn = Faraday.new(:url => "https://api.github.com/repos/#{username}/#{repo_name}/issues") do |conn|
  conn.basic_auth(username, token)
  conn.adapter  Faraday.default_adapter
end

urls.each_with_index do |url, index|
  title = open(url).read.scan(/<title>(.*?)<\/title>/).first.first.force_encoding('UTF-8')
  response = conn.post do |req|
    req.body = { body: url, labels: [kind, url], title: title }.to_json
  end
  puts response.body
  sleep 15 if index % 20 == 0
end

详细使用可以看:https://draveness.me/git-comments-initialize

nanqinlang commented 7 years ago

前排滋瓷一个.

levanax commented 7 years ago

项目作者打call ,这也想的出来。666 啊

aimingoo commented 7 years ago

支持。:)

这里有一个bash版的: https://github.com/aimingoo/monster/blob/master/libexec/updatesite.sh#L173

> monster update --sync-issue
ykk648 commented 6 years ago

写了一个python版本

import requests
import re
import json

site_url = 'http://ykksmile.top/'
sitemap_url = 'http://ykksmile.top/sitemap.xml'
token = 'token '+'*************'
username = 'cloisonne'
repo_name = 'cloisonne.github.io'

def getHTMLText(url):
    try:
        kv = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0'}
        r = requests.get(url, timeout=30, headers=kv)
        r.raise_for_status()  # 检查状态码
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return "产生异常"

wb_data = getHTMLText(sitemap_url)

pattern = re.compile('<loc>([a-zA-z]+://[^\s]*)</loc>')
urls = pattern.findall(str(wb_data))

print(urls)

for url in urls:
    url_data = getHTMLText(url)
    title_pattern = re.compile('<title>(.+)</title>')
    title = title_pattern.search(url_data).group(1).replace('&#39;','\'')
    print(title.replace('&#39;','\''))
    headers = {
        "Accept": "application/vnd.github.squirrel-girl-preview, application/vnd.github.html+json",
        "Accept-Encoding": "gzip, deflate, br",
        'Connection': 'keep-alive',
        'Host': 'api.github.com',
        'Origin': site_url,
        "Referer": url,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0",
        'Authorization': token
    }
    payload = {
        'title': title,
        'labels': ['gitment', url],
        'body': url
    }
    payload_json = json.dumps(payload)
    feedback = requests.post('https://api.github.com/repos/'+username+'/'+repo_name+'/issues',headers=headers,data=payload_json)
    print(feedback)
loveminimal commented 5 years ago

自己写了个脚本,加到那个 script 标签中就可以了 :)

  // Auto Initialize Comments

  let cmtBtns = document.getElementsByClassName('gitment-comments-init-btn');
  let isClicked = false;

  let timer = setInterval(() => {
    Array.prototype.forEach.call(cmtBtns, item => {
      item.click();
      isClicked = true;
      if(isClicked) {
        clearInterval(timer);
        console.log('Clear timer.')
      }        
    })
  }, 1000)