From 483d69a8e935ab7e78c6d1f564f8d877bb9a4728 Mon Sep 17 00:00:00 2001 From: Ken Yasue Date: Thu, 17 Jul 2025 05:24:09 +0200 Subject: [PATCH] save changes --- .vscode/launch.json | 7 ++--- src/apitest.ts | 4 +++ src/followback.ts | 59 ++++++++++++++++++++++++++++++++++++++++++ src/lib/AIdocClient.ts | 5 ++++ src/lib/utils.ts | 33 +++++++++++++++++++++++ 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/followback.ts create mode 100644 src/lib/utils.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index 7a9fe60..7a75a39 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,4 +1,4 @@ -/*{ +{ "version": "0.2.0", "configurations": [ { @@ -15,7 +15,7 @@ } ] } -*/ +/* { "version": "0.2.0", "configurations": [ @@ -32,4 +32,5 @@ ] } ] -} \ No newline at end of file +} + */ \ No newline at end of file diff --git a/src/apitest.ts b/src/apitest.ts index cbc53b1..a9619c3 100644 --- a/src/apitest.ts +++ b/src/apitest.ts @@ -38,6 +38,10 @@ async function main() { const following = await client.getFollowing(client.user.id); console.log('Following:', following.length); + + console.log('--- Testing Get User Profile ---'); + const userProfile = await client.getUser(1); // Test with user ID 1 + console.log('User Profile for ID 1:', userProfile); } console.log('API test finished successfully.'); diff --git a/src/followback.ts b/src/followback.ts new file mode 100644 index 0000000..e70b3c5 --- /dev/null +++ b/src/followback.ts @@ -0,0 +1,59 @@ +import { AIdocClient } from './lib/AIdocClient'; +import { getAllModels } from './lib/utils'; + +async function followBackLogic() { + console.log('Starting follow-back process...'); + const allModels = await getAllModels(); + + for (const model of allModels) { + if (!model.email || !model.socialnetwork_id) { + console.log(`Skipping model ${model.name || model.id} due to missing email or socialnetwork_id.`); + continue; + } + + console.log(`\nProcessing model: ${model.name} (${model.email})`); + const client = new AIdocClient(model.email); + + try { + await client.authenticate(); + if (!client.user) { + console.error(`Authentication failed, user object not available for ${model.email}.`); + continue; + } + + const modelUserId = parseInt(model.socialnetwork_id, 10); + if (isNaN(modelUserId)) { + console.error(`Invalid socialnetwork_id for ${model.name}: ${model.socialnetwork_id}`); + continue; + } + + const followers = await client.getFollowers(modelUserId); + const following = await client.getFollowing(modelUserId); + const followingIds = new Set(following.map(f => f.id)); + + console.log(`Found ${followers.length} followers and ${following.length} following.`); + + for (const follower of followers) { + if (!followingIds.has(follower.id)) { + // Avoid following self, just in case + if (follower.id === modelUserId) { + continue; + } + console.log(`Following back user: ${follower.name} (ID: ${follower.id})`); + try { + const followResponse = await client.follow(follower.id); + console.log(` > ${followResponse.message}`); + } catch (followError) { + console.error(` > Failed to follow back user ${follower.id}:`, followError); + } + } + } + } catch (error) { + console.error(`An error occurred while processing model ${model.name}:`, error); + } + } + + console.log('\nFollow-back process finished.'); +} + +followBackLogic(); diff --git a/src/lib/AIdocClient.ts b/src/lib/AIdocClient.ts index d606e8e..9da8597 100644 --- a/src/lib/AIdocClient.ts +++ b/src/lib/AIdocClient.ts @@ -136,4 +136,9 @@ export class AIdocClient { const endpoint = `users/${userId}/follow`; return this._api<{ message: string }>(ApiMethod.DELETE, endpoint); } + + public async getUser(userId: number): Promise { + const endpoint = `users/${userId}/profile`; + return this._api(ApiMethod.GET, endpoint); + } } diff --git a/src/lib/utils.ts b/src/lib/utils.ts new file mode 100644 index 0000000..eeb14e3 --- /dev/null +++ b/src/lib/utils.ts @@ -0,0 +1,33 @@ +import fs from 'fs/promises'; +import path from 'path'; +import { ModelDetail } from './interfaces'; + +const MODELS_DIR = path.join(process.cwd(), './models'); + +export async function getAllModels(): Promise { + const models: ModelDetail[] = []; + try { + const entries = await fs.readdir(MODELS_DIR, { withFileTypes: true }); + const modelDirs = entries + .filter(dirent => dirent.isDirectory()) + .map(dirent => dirent.name); + + for (const modelId of modelDirs) { + const detailJsonPath = path.join(MODELS_DIR, modelId, 'detail.json'); + try { + const fileContent = await fs.readFile(detailJsonPath, 'utf-8'); + const modelDetail = JSON.parse(fileContent) as ModelDetail; + if (!modelDetail.id) { + modelDetail.id = modelId; + } + models.push(modelDetail); + } catch (error) { + console.error(`Could not read or parse detail.json for model ${modelId}:`, error); + // Continue to the next model + } + } + } catch (error) { + console.error(`Error reading model directories from ${MODELS_DIR}:`, error); + } + return models; +}