import type { DanbooruService } from '../services/danbooru.js';
import type { TursoService } from '../services/turso.js';
import type { CollectResult } from '../types.js';
export async function collectAndSave(
danbooru: DanbooruService,
turso: TursoService,
tag: string,
maxPosts?: number,
startPage?: number
): Promise<CollectResult> {
try {
console.error(`Collecting data for tag: ${tag} (starting from page ${startPage || 1})`);
// Collect posts from Danbooru
const posts = await danbooru.collectAllPosts(tag, maxPosts, startPage);
if (posts.length === 0) {
return {
success: false,
tag,
collected: 0,
new_posts: 0,
updated_posts: 0,
error: 'No posts found'
};
}
console.error(`Collected ${posts.length} posts, saving to Turso...`);
// Upsert to Turso (INSERT OR REPLACE)
const { new_posts, updated_posts } = await turso.upsertPosts(posts);
console.error(`Saved: ${new_posts} new, ${updated_posts} updated`);
return {
success: true,
tag,
collected: posts.length,
new_posts,
updated_posts
};
} catch (error) {
console.error(`Error collecting data: ${error}`);
return {
success: false,
tag,
collected: 0,
new_posts: 0,
updated_posts: 0,
error: error instanceof Error ? error.message : String(error)
};
}
}