This repository was archived by the owner on Jan 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
99 lines (85 loc) · 2.8 KB
/
main.js
File metadata and controls
99 lines (85 loc) · 2.8 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
const { google } = require('googleapis');
const { WebhookClient } = require('discord.js');
const dayjs = require('dayjs');
require('dotenv').config();
const auth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/webmasters.readonly'],
credentials: {
client_email: process.env.GOOGLE_CLIENT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY,
}
});
const endDate = dayjs().format('YYYY-MM-DD');
const startDate = dayjs().subtract(7, 'day').format('YYYY-MM-DD');
async function getSearchPerformance() {
const client = await auth.getClient();
const searchconsole = google.searchconsole({ version: 'v1', auth: client });
const res = await searchconsole.searchanalytics.query({
siteUrl: "sc-domain:utcode.net",
requestBody: {
startDate,
endDate,
dimensions: ['page'],
rowLimit: 25000,
},
});
const grouped = {};
for (const row of res.data.rows || []) {
const url = row.keys[0];
const subdomain = new URL(url).hostname.split('.').slice(0, -2).join('.') || 'utcode.net';
if (!grouped[subdomain]) {
grouped[subdomain] = { clicks: 0, impressions: 0 };
}
grouped[subdomain].clicks += row.clicks;
grouped[subdomain].impressions += row.impressions;
}
return grouped;
}
async function sendToDiscord(data) {
const webhook = new WebhookClient({ id: process.env.WEBHOOK_ID, token: process.env.WEBHOOK_TOKEN });
const fields = Object.entries(data)
.filter(([sub, stats]) => stats.clicks > 0)
.sort(([sub1, stats1], [sub2, stats2]) => stats2.clicks - stats1.clicks)
.slice(0, 10)
.map(([sub, stats], i) => {
let name;
if (sub.startsWith("kf")) {
name = `${sub} (駒場祭${2000 + parseInt(sub.slice(2)) - 51})`;
} else if (sub.startsWith("mf")) {
name = `${sub} (五月祭${2000 + parseInt(sub.slice(2)) - 73})`;
} else if (channelIds[sub]) {
name = `${sub} ${channelIds[sub]}`;
} else {
name = sub;
}
return {
name: `${i + 1}: ${name}`,
value: `クリック数: ${stats.clicks}`,
inline: true,
};
});
await webhook.send({
embeds: [{
title: "1週間の検索パフォーマンス",
description: `${startDate} 〜 ${endDate}`,
fields
}],
});
}
const channelIds = {
"utcode.net": "<#1347510096044883988>",
"syllabus": "<#1356879628907712572>",
"create-cpu": "<#1354435873323876462>",
"coursemate": "<#1352695487056052366>",
"learn": "<#1368523267911974912>",
"ut-bridge": "<#1346690984217677864>",
"extra": "<#1353557111052828716>",
"itsuhima": "<#1356806556297072793>",
"shortcut-game": "<#1364040140850462741>",
"mf98": "<#1353342238477783172>",
"kf76": "<#1373908089794985984>",
};
(async () => {
const data = await getSearchPerformance();
await sendToDiscord(data);
})();