Refactor Instagram login process to handle session data and improve error handling

This commit is contained in:
암냥 2025-11-28 22:08:42 +09:00
commit 48f66773c7
4 changed files with 79 additions and 88 deletions

View file

@ -7,38 +7,59 @@ export async function Login() {
ig.state.generateDevice(process.env.IG_USERNAME!);
ig.state.appUserAgent
ig.request.end$.subscribe(async () => {
const serialized = await ig.state.serialize();
delete serialized.constants; // this deletes the version info, so you'll always use the version provided by the library
await Bun.write('./temp/session.json', JSON.stringify(serialized));
});
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();
let loggedInUser;
try {
return { loggedInUser, currentUser };
if (await Bun.file('./temp/session.json').exists()) {
const sessionData = await Bun.file('./temp/session.json').json();
await ig.state.deserialize(sessionData);
loggedInUser = await ig.account.login(
process.env.IG_USERNAME!,
process.env.IG_PASSWORD!
);
} else {
loggedInUser = await ig.account.login(
process.env.IG_USERNAME!,
process.env.IG_PASSWORD!
);
await ig.simulate.preLoginFlow();
} 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);
process.nextTick(async () => await ig.simulate.postLoginFlow());
}
} 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;
}
throw error;
const currentUser = await ig.account.currentUser();
return { loggedInUser, currentUser };
} catch (error) {
console.warn("⚠️ | Failed to load session data:", error);
}
}
export class Upload {
static async Post(filePath: string, caption: string) {
try {
const ImagePath = path.resolve(`${filePath}`);
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,
@ -47,13 +68,13 @@ export class Upload {
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}일자 급식`,