save current state
This commit is contained in:
49
src/apitest.ts
Normal file
49
src/apitest.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { AIdocClient } from './lib/AIdocClient';
|
||||
|
||||
const email = 'clodagh.byrne@100girls.club';
|
||||
|
||||
async function main() {
|
||||
console.log(`Starting API test for email: ${email}`);
|
||||
const client = new AIdocClient(email);
|
||||
|
||||
try {
|
||||
await client.authenticate();
|
||||
console.log('Authenticated user:', client.user);
|
||||
|
||||
if (client.user) {
|
||||
console.log('--- Testing Follow/Unfollow ---');
|
||||
const userIdToTest = 2; // Assuming user with ID 1 exists for testing
|
||||
|
||||
// Don't try to follow/unfollow self
|
||||
if (client.user.id !== userIdToTest) {
|
||||
console.log(`Following user ${userIdToTest}...`);
|
||||
const followResponse = await client.follow(userIdToTest);
|
||||
console.log(followResponse.message);
|
||||
|
||||
let following = await client.getFollowing(client.user.id);
|
||||
console.log('Current following:', following.map(u => u.id));
|
||||
|
||||
console.log(`Unfollowing user ${userIdToTest}...`);
|
||||
const unfollowResponse = await client.unfollow(userIdToTest);
|
||||
console.log(unfollowResponse.message);
|
||||
|
||||
following = await client.getFollowing(client.user.id);
|
||||
console.log('Current following:', following.map(u => u.id));
|
||||
} else {
|
||||
console.log('Skipping follow/unfollow test to avoid self-action.');
|
||||
}
|
||||
|
||||
const followers = await client.getFollowers(client.user.id);
|
||||
console.log('Followers:', followers.length);
|
||||
|
||||
const following = await client.getFollowing(client.user.id);
|
||||
console.log('Following:', following.length);
|
||||
}
|
||||
|
||||
console.log('API test finished successfully.');
|
||||
} catch (error) {
|
||||
console.error('API test failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
139
src/lib/AIdocClient.ts
Normal file
139
src/lib/AIdocClient.ts
Normal file
@ -0,0 +1,139 @@
|
||||
import axios from 'axios';
|
||||
import * as dotenv from 'dotenv';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import FormData from 'form-data';
|
||||
import { ApiMethod, Follower, FollowersResponse, FollowingResponse, SignInResponse, User, VerifyResponse } from './interfaces';
|
||||
|
||||
// Load environment variables from .env file
|
||||
dotenv.config();
|
||||
|
||||
const API_BASE_URL = 'https://aidol.club/api';
|
||||
const SECRET_KEY = 'ECUp5AQgnHlt5qn8U2HUfzjxsyLVQSUX';
|
||||
|
||||
export class AIdocClient {
|
||||
private email: string;
|
||||
private token: string | null = null;
|
||||
public user: User | null = null;
|
||||
|
||||
constructor(email: string) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
private async _api<T>(method: ApiMethod, endpoint: string, data?: any): Promise<T> {
|
||||
if (!this.token) {
|
||||
throw new Error('Authentication token not found. Please authenticate first.');
|
||||
}
|
||||
|
||||
const url = `${API_BASE_URL}/${endpoint}`;
|
||||
const headers: any = {
|
||||
'Authorization': `Bearer ${this.token}`,
|
||||
};
|
||||
|
||||
if (data instanceof FormData) {
|
||||
Object.assign(headers, data.getHeaders());
|
||||
} else {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios({
|
||||
method,
|
||||
url,
|
||||
data,
|
||||
headers,
|
||||
});
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const errorMessage = error.response?.data?.message || error.message;
|
||||
console.error(`API call failed for ${method} ${endpoint}:`, errorMessage);
|
||||
throw new Error(`API call failed: ${errorMessage}`);
|
||||
}
|
||||
}
|
||||
|
||||
public async authenticate(): Promise<void> {
|
||||
try {
|
||||
console.log(`Authenticating ${this.email}...`);
|
||||
|
||||
// 1. Sign In
|
||||
const signInResponse = await axios.post<SignInResponse>(`${API_BASE_URL}/auth/signin`, {
|
||||
email: this.email,
|
||||
secret_key: SECRET_KEY,
|
||||
});
|
||||
|
||||
const signInKey = signInResponse.data.signin_key;
|
||||
if (!signInKey) {
|
||||
throw new Error(`Sign-in failed for ${this.email}: No signin_key received.`);
|
||||
}
|
||||
console.log(`Sign-in successful, received sign-in key.`);
|
||||
|
||||
// 2. Verify
|
||||
const verifyResponse = await axios.post<VerifyResponse>(`${API_BASE_URL}/auth/verify`, {
|
||||
email: this.email,
|
||||
key: signInKey,
|
||||
});
|
||||
|
||||
this.token = verifyResponse.data.token;
|
||||
|
||||
if (!this.token) {
|
||||
throw new Error(`Verification failed for ${this.email}: Missing token.`);
|
||||
}
|
||||
|
||||
this.user = await this.me();
|
||||
|
||||
console.log(`Authentication successful for ${this.email}.`);
|
||||
|
||||
|
||||
} catch (error: any) {
|
||||
const errorMessage = error.response?.data?.message || error.message;
|
||||
console.error(`Authentication failed for ${this.email}:`, errorMessage);
|
||||
throw new Error(`Authentication failed: ${errorMessage}`);
|
||||
}
|
||||
}
|
||||
|
||||
public async me(): Promise<User> {
|
||||
return this._api<User>(ApiMethod.GET, 'auth/me');
|
||||
}
|
||||
|
||||
public async getFollowers(userId: number): Promise<Follower[]> {
|
||||
let page = 1;
|
||||
const allFollowers: Follower[] = [];
|
||||
let totalPages = 1;
|
||||
|
||||
while (page <= totalPages) {
|
||||
const endpoint = `users/${userId}/followers?page=${page}`;
|
||||
const response = await this._api<FollowersResponse>(ApiMethod.GET, endpoint);
|
||||
allFollowers.push(...response.followers);
|
||||
totalPages = response.totalPages;
|
||||
page++;
|
||||
}
|
||||
|
||||
return allFollowers;
|
||||
}
|
||||
|
||||
public async getFollowing(userId: number): Promise<Follower[]> {
|
||||
let page = 1;
|
||||
const allFollowing: Follower[] = [];
|
||||
let totalPages = 1;
|
||||
|
||||
while (page <= totalPages) {
|
||||
const endpoint = `users/${userId}/following?page=${page}`;
|
||||
const response = await this._api<FollowingResponse>(ApiMethod.GET, endpoint);
|
||||
allFollowing.push(...response.following);
|
||||
totalPages = response.totalPages;
|
||||
page++;
|
||||
}
|
||||
|
||||
return allFollowing;
|
||||
}
|
||||
|
||||
public async follow(userId: number): Promise<{ message: string }> {
|
||||
const endpoint = `users/${userId}/follow`;
|
||||
return this._api<{ message: string }>(ApiMethod.POST, endpoint);
|
||||
}
|
||||
|
||||
public async unfollow(userId: number): Promise<{ message: string }> {
|
||||
const endpoint = `users/${userId}/follow`;
|
||||
return this._api<{ message: string }>(ApiMethod.DELETE, endpoint);
|
||||
}
|
||||
}
|
||||
71
src/lib/interfaces.ts
Normal file
71
src/lib/interfaces.ts
Normal file
@ -0,0 +1,71 @@
|
||||
export interface ModelDetail {
|
||||
id: string; // Assuming 'id' is the directory name and used for photo lookup
|
||||
email: string;
|
||||
name: string;
|
||||
gender: string;
|
||||
description_text: string;
|
||||
socialnetwork_id?: string; // Will be added by the script
|
||||
[key: string]: any; // Allow other properties
|
||||
}
|
||||
|
||||
export interface SignInResponse {
|
||||
signin_key: string;
|
||||
}
|
||||
|
||||
export interface User {
|
||||
id: number;
|
||||
email: string;
|
||||
name: string;
|
||||
avatar_url: string;
|
||||
gender: string;
|
||||
description_text: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
credit: string;
|
||||
purchasedBooks: any[];
|
||||
isAdmin: boolean;
|
||||
}
|
||||
|
||||
export interface VerifyResponse {
|
||||
message: string;
|
||||
token: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
export interface FileUploadResponse {
|
||||
data: {
|
||||
files: Array<{ url: string;[key: string]: any }>;
|
||||
filename: string;
|
||||
}
|
||||
}
|
||||
|
||||
export enum ApiMethod {
|
||||
GET = 'get',
|
||||
POST = 'post',
|
||||
PUT = 'put',
|
||||
DELETE = 'delete',
|
||||
}
|
||||
|
||||
export interface Follower {
|
||||
id: number;
|
||||
email: string;
|
||||
name: string;
|
||||
avatar_url: string;
|
||||
followed_at: string;
|
||||
}
|
||||
|
||||
export interface FollowersResponse {
|
||||
followers: Follower[];
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
totalPages: number;
|
||||
}
|
||||
|
||||
export interface FollowingResponse {
|
||||
following: Follower[];
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
totalPages: number;
|
||||
}
|
||||
Reference in New Issue
Block a user