Skip to content

Commit

Permalink
check follower response for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hfxbse committed May 1, 2024
1 parent add1473 commit df24dea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/instagram/follower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SessionData, {sessionToCookie} from "./session-data";
import {RandomDelayLimit, Limits} from "./limits";
import {User, UserGraph} from "./user";
import {ReadableStream} from "node:stream/web";
import {hasJsonBody} from "./request";

export enum FollowerFetcherEventTypes {
UPDATE, RATE_LIMIT_BATCH, RATE_LIMIT_DAILY, DEPTH_LIMIT_FOLLOWER, DEPTH_LIMIT_FOLLOWING
Expand Down Expand Up @@ -337,6 +338,24 @@ async function fetchFollowers({session, targetUser, nextPage, direction}: {
}
})

if (!response.ok) {
if (hasJsonBody(response)) {
const data = (await response.json()) as {
message?: string,
require_login?: boolean
}

if (data.require_login) throw Error("Authentication failure while querying followers. Check your session id again.")

throw Error(
data.message ??
`Received status code ${response.status} (${response.statusText}) while querying followers. ` +
`The response contained the following: ${data}`)
} else {
throw Error(await response.text() ?? 'Failed to load followers.')
}
}

const page = (await response.json()) as {
users: {
id: string,
Expand Down
4 changes: 1 addition & 3 deletions src/instagram/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hexToArrayBuffer from "hex-to-array-buffer";
import sealBox from "tweetnacl-sealedbox-js";
import SessionData from "./session-data";
import {hasJsonBody} from "./request";

const crypto = globalThis.crypto
const encoder = new TextEncoder()
Expand Down Expand Up @@ -123,9 +124,6 @@ function getSessionId(response: Response): string {
.substring(identifier.length)
}

function hasJsonBody(response: Response): boolean {
return response.headers.get("Content-Type").startsWith("application/json;")
}

export async function login({user, password, verification}: {
user: string,
Expand Down
3 changes: 3 additions & 0 deletions src/instagram/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hasJsonBody(response: Response): boolean {
return response.headers.get("Content-Type").startsWith("application/json;")
}

0 comments on commit df24dea

Please sign in to comment.