AI 時代の robots.txt
AI クローラー向け robots.txt を GitHub リポジトリから自動取得するスクリプトを紹介します。
robots.txt でお行儀の良い AI 用クローラーを弾くためにどんな robots.txt を書けばよいのでしょうか。
以下の GitHub リポジトリにまとまっています。
GitHub - ai-robots-txt/ai.robots.txt: A list of AI agents and robots to block.A list of AI agents and robots to block. Contribute to ai-robots-txt/ai.robots.txt development by creating an account on GitHub.https://github.com/ai-robots-txt/ai.robots.txt定期的に見に行って自分の Web サイトにコピペするのは面倒なので、ダウンロードをスクリプトにします。
import { z } from "zod";
const OUTPUT_FILE = "public/robots.txt";
const OWNER = "ai-robots-txt";
const REPO = "ai.robots.txt";
const API_URL = `https://api.github.com/repos/${OWNER}/${REPO}/releases/latest`;
async function getRobotsTxtUrl() {
const resp = await fetch(API_URL, {
headers: {
"User-Agent": "Node.js Script",
"Accept": "application/vnd.github+json",
},
});
if (!resp.ok) {
throw new Error(`リリース情報の取得に失敗しました: ${resp.status} ${resp.statusText}`);
}
const release = z.object({
assets: z.array(
z.object({
name: z.string(),
browser_download_url: z.string(),
}),
),
}).parse(await resp.json());
const asset = release.assets.find(asset => asset.name === "robots.txt");
if (!asset) {
throw new Error("最新リリースに robots.txt が見つかりませんでした。");
}
return asset.browser_download_url;
}
async function downloadFile(url: string, outPath: string) {
const resp = await fetch(url);
if (!resp.ok) {
throw new Error(`robots.txt のダウンロードに失敗しました: ${resp.statusText}`);
}
await Bun.file(outPath).write(resp);
console.log(`robots.txt を ${outPath} に保存しました。`);
}
const downloadUrl = await getRobotsTxtUrl();
await downloadFile(downloadUrl, OUTPUT_FILE);実行するには:
bun run path/to/script.tsすると、定数 OUTPUT_FILE に robots.txt がダウンロードされます。このスクリプトをビルドコマンドの最後に仕込むか、定期的に手動で実行すれば良いと思います。
冒頭で述べたように、お行儀の良いボットは防げますが、ルールを無視してくるボットもいます。そのようなボットには Cloudflare の AI Labyrinth を使えば良いと思います。