save changes
This commit is contained in:
5
.vscode/launch.json
vendored
5
.vscode/launch.json
vendored
@ -1,4 +1,4 @@
|
|||||||
/*{
|
{
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
@ -15,7 +15,7 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
*/
|
/*
|
||||||
{
|
{
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
@ -33,3 +33,4 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
@ -38,6 +38,10 @@ async function main() {
|
|||||||
|
|
||||||
const following = await client.getFollowing(client.user.id);
|
const following = await client.getFollowing(client.user.id);
|
||||||
console.log('Following:', following.length);
|
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.');
|
console.log('API test finished successfully.');
|
||||||
|
|||||||
59
src/followback.ts
Normal file
59
src/followback.ts
Normal file
@ -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();
|
||||||
@ -136,4 +136,9 @@ export class AIdocClient {
|
|||||||
const endpoint = `users/${userId}/follow`;
|
const endpoint = `users/${userId}/follow`;
|
||||||
return this._api<{ message: string }>(ApiMethod.DELETE, endpoint);
|
return this._api<{ message: string }>(ApiMethod.DELETE, endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getUser(userId: number): Promise<User> {
|
||||||
|
const endpoint = `users/${userId}/profile`;
|
||||||
|
return this._api<User>(ApiMethod.GET, endpoint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/lib/utils.ts
Normal file
33
src/lib/utils.ts
Normal file
@ -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<ModelDetail[]> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user