today.isangjeong/app/lib/instagram.ts
2025-05-09 20:11:41 +09:00

65 lines
No EOL
1.9 KiB
TypeScript

import path from 'path';
import { IgApiClient } from "igramapi";
const ig = new IgApiClient();
export async function Login() {
ig.state.generateDevice(process.env.IG_USERNAME!);
ig.state.appUserAgent
try {
await ig.simulate.preLoginFlow();
const loggedInUser = await ig.account.login(
process.env.IG_USERNAME!,
process.env.IG_PASSWORD!
);
process.nextTick(async () => await ig.simulate.postLoginFlow());
const currentUser = await ig.account.currentUser();
return { loggedInUser, currentUser };
} catch (error: any) {
if (error.name === "IgCheckpointError") {
console.error(
"Instagram checkpoint required. Please verify your account in the Instagram app or handle the challenge."
);
// Optionally, you can trigger challenge handling here
// await ig.challenge.auto(true); // Requesting sms-code or click "It was me" button
} else {
console.error("🔒 | Login failed:", error);
}
throw error;
}
}
export class Upload {
static async Post(filePath: string, caption: string) {
try {
const ImagePath = path.resolve(`${filePath}`);
const bunFile = Bun.file(ImagePath);
const ImageBuffer = Buffer.from(await bunFile.arrayBuffer());
await ig.publish.photo({
file: ImageBuffer,
caption: caption,
});
} catch (error) {
console.error("Error uploading post:", error);
}
}
static async Story(filePath: string, MLSV_YMD: string) {
try {
const ImagePath = path.resolve(`${filePath}`);
const bunFile = Bun.file(ImagePath);
const ImageBuffer = Buffer.from(await bunFile.arrayBuffer());
await ig.publish.story({
file: ImageBuffer,
caption: `#인천상정중학교 #상정중학교 #급식 \n${MLSV_YMD}일자 급식`,
});
} catch (error) {
console.error("Error uploading post:", error);
}
}
}