File size: 6,396 Bytes
0cc319d b4f74d0 0cc319d | 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 | export type FieldMap = {
id: string;
previewUrl: string;
fileUrl: string;
ext: string;
tags: string;
source?: string;
rating?: string;
score?: string;
sampleUrl?: string;
fileSize?: string;
width?: string;
height?: string;
md5?: string;
uploader?: string;
createdAt?: string;
};
export type SiteConfig = {
id: string;
name: string;
baseUrl: string;
searchPath: string;
params: { tags: string; page: string; limit: string };
postsPath: string; // dotted path to array of posts in JSON response; "" means root
fields: FieldMap;
headers?: Record<string, string>;
pageStartsAt?: number; // default 1
};
export const BUILTIN_SITES: SiteConfig[] = [
{
id: "e621",
name: "e621",
baseUrl: "https://e621.net",
searchPath: "/posts.json",
params: { tags: "tags", page: "page", limit: "limit" },
// e621 switched to a new posts format on 2026-11-13 (e621ng f3c1163): root array + files.*.
// "|" separates fallbacks so the legacy format keeps working.
postsPath: "|posts",
fields: {
id: "id",
previewUrl: "files.preview.jpg|files.preview.webp|preview.url",
fileUrl: "files.original.url|file.url",
ext: "files.meta.ext|file.ext",
tags: "tags|tags.general",
source: "sources.0",
rating: "rating",
score: "stats.score.total|score.total",
sampleUrl: "files.sample.jpg|files.sample.webp|sample.url",
fileSize: "files.meta.size|file.size",
width: "files.original.width|file.width",
height: "files.original.height|file.height",
md5: "files.meta.md5|file.md5",
uploader: "uploader_name|uploader_id",
createdAt: "created_at",
},
headers: { "User-Agent": "LovableGrabber/1.0 (booru client)" },
pageStartsAt: 1,
},
{
id: "floofgens",
name: "Floofgens",
baseUrl: "https://floofgens.net",
searchPath: "/posts.json",
params: { tags: "tags", page: "page", limit: "limit" },
// e621 switched to a new posts format on 2026-11-13 (e621ng f3c1163): root array + files.*.
// "|" separates fallbacks so the legacy format keeps working.
postsPath: "|posts",
fields: {
id: "id",
previewUrl: "files.preview.jpg|files.preview.webp|preview.url",
fileUrl: "files.original.url|file.url",
ext: "files.meta.ext|file.ext",
tags: "tags|tags.general",
source: "sources.0",
rating: "rating",
score: "stats.score.total|score.total",
sampleUrl: "files.sample.jpg|files.sample.webp|sample.url",
fileSize: "files.meta.size|file.size",
width: "files.original.width|file.width",
height: "files.original.height|file.height",
md5: "files.meta.md5|file.md5",
uploader: "uploader_name|uploader_id",
createdAt: "created_at",
},
headers: { "User-Agent": "LovableGrabber/1.0 (booru client)" },
pageStartsAt: 1,
},
];
// Resolve a dotted path like "a.b.0.c" against an arbitrary value.
// "a.b|c.d" tries each alternative and returns the first non-null value.
export function getByPath(obj: unknown, path: string): unknown {
if (path.includes("|")) {
for (const alt of path.split("|")) {
const v = getByPath(obj, alt.trim());
if (v != null && v !== "") return v;
}
return undefined;
}
if (!path) return obj;
const parts = path.split(".");
let cur: unknown = obj;
for (const p of parts) {
if (cur == null) return undefined;
if (Array.isArray(cur)) {
const idx = Number(p);
if (!Number.isInteger(idx)) return undefined;
cur = cur[idx];
} else if (typeof cur === "object") {
cur = (cur as Record<string, unknown>)[p];
} else {
return undefined;
}
}
return cur;
}
export type NormalizedPost = {
id: string;
previewUrl: string;
fileUrl: string;
ext: string;
tags: string[];
source?: string;
rating?: string;
score?: number;
sampleUrl?: string;
fileSize?: number;
width?: number;
height?: number;
md5?: string;
uploader?: string;
createdAt?: string;
rawJson?: string;
};
function optStr(v: unknown): string | undefined {
if (v == null) return undefined;
if (typeof v === "string") return v || undefined;
return String(v);
}
function optNum(v: unknown): number | undefined {
if (v == null || v === "") return undefined;
const n = typeof v === "number" ? v : Number(v);
return Number.isFinite(n) ? n : undefined;
}
export function normalizePost(raw: unknown, fields: FieldMap): NormalizedPost | null {
const fileUrl = getByPath(raw, fields.fileUrl);
const previewUrl = getByPath(raw, fields.previewUrl) ?? fileUrl;
const id = getByPath(raw, fields.id);
if (!fileUrl || !id) return null;
const tagsRaw = getByPath(raw, fields.tags);
let tags: string[] = [];
if (Array.isArray(tagsRaw)) tags = tagsRaw.map(String);
else if (tagsRaw && typeof tagsRaw === "object")
tags = Object.values(tagsRaw as Record<string, unknown>).flatMap((v) =>
Array.isArray(v) ? v.map(String) : [],
);
else if (typeof tagsRaw === "string") tags = tagsRaw.split(/\s+/).filter(Boolean);
const extRaw = getByPath(raw, fields.ext);
const ext =
(typeof extRaw === "string" && extRaw) ||
(typeof fileUrl === "string" ? (fileUrl.split(".").pop()?.split("?")[0] ?? "jpg") : "jpg");
return {
id: String(id),
previewUrl: String(previewUrl),
fileUrl: String(fileUrl),
ext,
tags,
source: fields.source ? optStr(getByPath(raw, fields.source)) : undefined,
rating: fields.rating ? optStr(getByPath(raw, fields.rating)) : undefined,
score: fields.score ? optNum(getByPath(raw, fields.score)) : undefined,
sampleUrl: fields.sampleUrl ? optStr(getByPath(raw, fields.sampleUrl)) : undefined,
fileSize: fields.fileSize ? optNum(getByPath(raw, fields.fileSize)) : undefined,
width: fields.width ? optNum(getByPath(raw, fields.width)) : undefined,
height: fields.height ? optNum(getByPath(raw, fields.height)) : undefined,
md5: fields.md5 ? optStr(getByPath(raw, fields.md5)) : undefined,
uploader: fields.uploader ? optStr(getByPath(raw, fields.uploader)) : undefined,
createdAt: fields.createdAt ? optStr(getByPath(raw, fields.createdAt)) : undefined,
rawJson: safeStringify(raw),
};
}
function safeStringify(v: unknown): string | undefined {
try {
return JSON.stringify(v);
} catch {
return undefined;
}
}
|