-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
304 lines (271 loc) · 9.37 KB
/
Copy pathmain.js
File metadata and controls
304 lines (271 loc) · 9.37 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env node
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
import { render } from "oh-my-logo";
import { isPackageLatest } from "is-package-latest";
import axios from "axios";
import chalk from "chalk";
import * as cheerio from "cheerio";
import inquirer from "inquirer";
import MarkdownIt from "markdown-it";
import prettier from "prettier";
import open from "open";
import { exec } from "child_process";
import util from "util";
import pkg from "./package.json" with { type: "json" };
const currentVersion = pkg.version;
const packageName = pkg.name;
const TARGET_URL = "https://connpass.com/event/[id]/participation/";
async function checkVersion() {
try {
const res = await isPackageLatest(pkg);
const latestVersion = res.latestVersion;
if (!res.isLatest) {
console.log(
chalk.yellow(
`[Notice] 新しいバージョンが利用可能です!: ${chalk.gray(
currentVersion
)} --> ${chalk.green(latestVersion)}`
)
);
console.log(
chalk.yellow(
`更新コマンド: ${chalk.cyan(
`npm install -g ${packageName}@latest`
)}\n`
)
);
const { shouldUpdate } = await inquirer.prompt([
{
type: "confirm",
name: "shouldUpdate",
message: "パッケージを更新しますか?",
default: true,
},
]);
if (shouldUpdate) {
console.log(chalk.cyan(`\nパッケージを更新しています...`));
const execPromise = util.promisify(exec);
const command = `npm install -g ${packageName}@latest`;
try {
await execPromise(command);
console.log(chalk.green("完了しました!"));
console.log(
chalk.yellow(
"再度コマンドを実行して、新しいバージョンをご利用ください"
)
);
} catch (error) {
console.error(chalk.red("失敗しました"));
console.error(error);
console.log(chalk.yellow("以下のコマンドを実行してください:"));
console.log(chalk.cyan(`npm install -g ${packageName}@latest`));
}
process.exit(0);
}
}
} catch (e) {
console.error("バージョンチェックに失敗しました:", e.message);
}
}
async function main() {
try {
console.log(`${packageName}@${currentVersion}\n`);
const logo = await render("CONNPASS\nPICKUP", {
palette: "sunset",
direction: "horizontal",
});
console.log(logo, "\n");
console.log("\n--- 免責事項 ---");
console.log(
chalk.yellow("本ツール(以下、ツール)は実験目的で作成・公開されました。")
);
console.log(chalk.yellow("ツールの使用を推奨しません。"));
console.log(
chalk.yellow(
"ツールを使用して発生した損害に関しては一切責任を負いません。\n"
)
);
await checkVersion();
let eventId;
const argEventId = process.argv[2];
if (argEventId) {
eventId = argEventId;
} else {
const answers = await inquirer.prompt([
{
type: "input",
name: "event",
message: "connpassのeventURLまたはeventIdを入力してください:",
validate: (input) =>
input
? true
: "この項目は必須です(eventId: https://connpass.com/event/[id]/ の[id]の部分)",
},
]);
eventId = answers.event;
}
const matchEventId = eventId.match(
/https?:\/\/.*?connpass\.com\/event\/(\d+)/
);
eventId = matchEventId ? matchEventId[1] : eventId;
const target = TARGET_URL.replace("[id]", eventId);
console.log(`\neventId: ${eventId}`);
console.log(`取得中: ${target}`);
const response = await axios.get(target, {
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
},
});
const data = response.data;
const $ = cheerio.load(data);
const participants = {};
const adminTable = $(".concerned_area .participants_table");
if (adminTable.length) {
const adminNames = [];
adminTable.find("tbody .user .display_name a").each((j, user) => {
const userElement = $(user);
const name = userElement.text().trim();
const href = userElement.attr("href");
const match = href ? href.match(/user\/([^\/]+)/) : null;
const username = match ? match[1] : "";
adminNames.push(`${name} (${username})`);
});
if (adminNames.length > 0) {
participants["☆管理者"] = adminNames;
}
}
$(".participation_table_area").each((i, section) => {
const roleNameElement = $(section).find("thead .label_ptype_name");
if (roleNameElement.length) {
const roleName = roleNameElement.text().trim();
const names = [];
$(section)
.find("tbody .user .display_name a")
.each((j, user) => {
const userElement = $(user);
const name = userElement.text().trim();
const href = userElement.attr("href");
const match = href ? href.match(/user\/([^\/]+)/) : null;
const username = match ? match[1] : "";
names.push(`${name} (${username})`);
});
if (names.length > 0) {
participants[roleName] = names;
}
}
});
console.log("\n--- 取得完了 ---");
console.log(participants);
let members = [];
const availableRoles = Object.keys(participants);
let followUpAnswers;
if (availableRoles.length > 0) {
followUpAnswers = await inquirer.prompt([
{
type: "checkbox",
name: "selectedRoles",
message: "対象の参加枠を選択してください:",
choices: availableRoles,
},
{
type: "confirm",
name: "allowDuplicates",
message: "抽選リストの重複を削除しますか?(同じ人が複数枠にいる場合)",
default: true,
},
{
type: "confirm",
name: "saveHtml",
message: "結果をHTMLファイルで保存・表示しますか?",
default: true,
},
{
type: "input",
name: "additionalMembers",
message:
"追加参加者をカンマ(,)区切りで入力してください(Enterでスキップ):",
},
]);
const rolesToProcess = followUpAnswers.selectedRoles;
if (followUpAnswers.allowDuplicates) {
const memberSet = new Set();
for (const role of rolesToProcess) {
if (participants[role]) {
for (const member of participants[role]) {
memberSet.add(member);
}
}
}
members = [...memberSet];
} else {
for (const role of rolesToProcess) {
if (participants[role]) {
members.push(...participants[role]);
}
}
}
if (followUpAnswers.additionalMembers) {
const additional = followUpAnswers.additionalMembers
.split(",")
.map((name) => name.trim())
.filter((name) => name);
members.push(...additional);
}
} else {
console.log("\n参加者が見つかりませんでした");
}
if (members.length === 0) {
console.log("\n参加者がいないので処理を終了します");
return process.exit(0);
}
let markdown = `# 順番 \n[Event URL](https://connpass.com/event/${eventId})`;
console.log("\n--- 順番 ---");
for (let i = members.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[members[i], members[j]] = [members[j], members[i]];
}
members.forEach((member, index) => {
const result = `${index + 1}. ${member}`;
console.log(result);
markdown += `\n${index + 1}. ${member}`;
});
if (followUpAnswers && followUpAnswers.saveHtml) {
const md = new MarkdownIt();
const htmlFragment = md.render(markdown);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const stylePath = path.join(__dirname, "assets", "style.css");
const style = await fs.readFile(stylePath, "utf-8");
const htmlPath = path.join(__dirname, "assets", "index.html");
const html = await fs.readFile(htmlPath, "utf-8");
const editedHtml = await prettier.format(
html
.replace("<style></style>", `<style>${style}</style>`)
.replace("<main></main>", `<main>${htmlFragment}</main>`),
{
parser: "html",
}
);
const outputDir = path.join(__dirname, "results");
const tempFilePath = path.join(
outputDir,
`${eventId}_${Date.now()}.html`
);
await fs.mkdir(outputDir, { recursive: true });
await fs.writeFile(tempFilePath, editedHtml, "utf-8");
await open(tempFilePath);
console.log(`\nブラウザで結果を表示します: ${tempFilePath}`);
}
} catch (e) {
if (e.isTtyError) {
console.log("\n処理がキャンセルされました");
} else {
console.error("\nエラー:", e.message);
console.log("処理がキャンセルされました");
}
return process.exit(1);
}
process.exit(0);
}
main();