({
type,
uid,
csrftoken,
progressCallback,
limit = 0,
})
| 90 | } |
| 91 | } |
| 92 | export async function getAllFollow({ |
| 93 | type, |
| 94 | uid, |
| 95 | csrftoken, |
| 96 | progressCallback, |
| 97 | limit = 0, |
| 98 | }) { |
| 99 | if (!(type in CACHED)) throw Error(`Invalid type: ${type}`); |
| 100 | |
| 101 | async function _getFollow(uid, end_cursor) { |
| 102 | let url = new URL("https://www.instagram.com/graphql/query/"); |
| 103 | Object.entries({ |
| 104 | query_hash: CACHED[type].hash, |
| 105 | variables: JSON.stringify({ |
| 106 | id: uid, |
| 107 | after: end_cursor || "", |
| 108 | first: 50, |
| 109 | }), |
| 110 | }).forEach(([k, v]) => url.searchParams.append(k, v)); |
| 111 | |
| 112 | let res = await fetch(url, { |
| 113 | headers: { |
| 114 | "Content-Type": "application/x-www-form-urlencoded", |
| 115 | "X-CSRFToken": csrftoken, |
| 116 | "x-requested-with": "XMLHttpRequest", |
| 117 | "x-instagram-ajax": 1, |
| 118 | }, |
| 119 | }); |
| 120 | return await res.json(); |
| 121 | } |
| 122 | |
| 123 | let cursor = ""; |
| 124 | let total = 0; |
| 125 | let users = []; |
| 126 | while (true) { |
| 127 | try { |
| 128 | let json = await _getFollow(uid, cursor); |
| 129 | let { |
| 130 | edges = [], |
| 131 | count = 0, |
| 132 | page_info, |
| 133 | } = json?.data?.user?.[CACHED[type].edge] || {}; |
| 134 | |
| 135 | if (!total) total = limit <= 0 ? count : Math.min(count, limit); |
| 136 | |
| 137 | edges.forEach((e) => users.push(e.node)); |
| 138 | |
| 139 | progressCallback?.({ |
| 140 | total, |
| 141 | current: users.length, |
| 142 | data: users, |
| 143 | }); |
| 144 | |
| 145 | if ( |
| 146 | !page_info?.has_next_page || |
| 147 | !edges.length || |
| 148 | (limit > 0 && users.length >= limit) |
| 149 | ) |
no test coverage detected