-
Notifications
You must be signed in to change notification settings - Fork 4
/
action.js
170 lines (151 loc) · 5.51 KB
/
action.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
let { Octokit } = require('@octokit/rest')
let { loadWikipedia, loadBaiduBaike, generateArticle, googlePhoto } = require('./lib')
const crypto = require('crypto');
let fs = require('fs')
require('dotenv').config()
let TOKEN = process.env.TOKEN
let REPOSITORY = process.env.REPOSITORY
let [OWNER, REPO] = REPOSITORY.split('/')
let octokit = new Octokit({
auth: TOKEN
})
const FILE = '100644'; // commit mode
const encoding = 'utf-8';
const ref = 'heads/master';
let committer = {
name: 'NodeBE4',
email: '[email protected]'
}
async function performTasks() {
let { data } = await octokit.issues.listForRepo({
owner: OWNER,
repo: REPO,
state: 'open'
})
let promises = data.map(async (issue) => {
try {
if (issue.title.substring(0,12)=='add_request:'){
let titletext = issue.title.split(" ")[1]
let people = titletext.split("-")[0].trim()
let keyword = titletext.replace(people,'').replace("-",'')
let wiki = issue.body.split('\n')[0]
let match = /^https:\/\/(zh|en).wikipedia.org\/wiki\//
let match2 = /^https:\/\/baike.baidu.com\/item\//
if (match.test(wiki) || (match2.test(wiki))){
let text = fs.readFileSync('./index.json', {encoding:'utf8', flag:'r'})
let json = JSON.parse(text)
let text2 = fs.readFileSync('./blacklist.json', {encoding:'utf8', flag:'r'})
let bjson = JSON.parse(text2)
var topleader = bjson.filter(function (item) {
return item.wiki == wiki ;
});
if (topleader.length > 0){
throw Error('英雄榜不接受现任国家元首,他们已经霸占了新闻头条。');
}
let hash = crypto.createHash('md5').update(people+wiki).digest("hex")
var thisperson = json.filter(function (item) {
return item.wiki == wiki ;
});
if (thisperson.length > 0){
let votefile = `_data/votes/vote_${thisperson[0].hash}`
let count = 2
if (fs.existsSync(votefile)) {
text = fs.readFileSync(votefile)
count = parseInt(text)+1
}
thisperson[0].vote = count
fs.writeFileSync(votefile, count.toString())
json.map(item =>{
let votefile = `_data/votes/vote_${item.hash}`;
if (fs.existsSync(votefile)) {
text = fs.readFileSync(votefile)
item.vote = parseInt(text)
}
})
let content = JSON.stringify(json, undefined, 4);
fs.writeFileSync(`./index.json`, content)
await octokit.issues.createComment({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
body: `${thisperson[0].people} 已经榜上有名了,你对TA赞了一次,试试在网页上的搜索TA的名字`
})
await octokit.issues.update({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
state: 'closed',
title: issue.title,
labels: ['duplicated']
})
}else{
let photourl = await googlePhoto(people + keyword)
new Promise(resolve => setTimeout(resolve, 2000))
let newhero = {
people: people,
keyword: keyword,
wiki: wiki,
vote: 1,
hash: hash,
photo: photourl
}
json.push({
...newhero
});
console.log(json);
json.map(item =>{
let votefile = `_data/votes/vote_${item.hash}`;
if (fs.existsSync(votefile)) {
text = fs.readFileSync(votefile)
item.vote = parseInt(text)
}
})
let content = JSON.stringify(json, undefined, 4);
let prTitle = `添加新人物-${people}`
let intro
if (match.test(wiki)){
intro = await loadWikipedia(newhero.wiki, "mw-content-text")
}else if (match2.test(wiki)){
intro = await loadBaiduBaike(newhero.wiki)
}else{
throw new UserException('Invalid: wikipedia or baidu baike' )
}
let page = generateArticle(newhero, intro)
fs.writeFileSync(`./index.json`, content)
await octokit.issues.createComment({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
body: page
})
await octokit.issues.update({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
state: 'closed',
title: prTitle,
labels: ['success']
})
}
}
}
} catch(error) {
await octokit.issues.createComment({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
body: `错误 ${error.toString()}`
})
await octokit.issues.update({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
state: 'closed',
labels: ['error']
})
throw error
}
})
await Promise.all(promises)
}
performTasks()