Created
November 13, 2024 12:03
-
-
Save prenaissance/58f6e4efc65d1a252c49c592fd25cc6e to your computer and use it in GitHub Desktop.
BlueSky check handle availability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { promisify } from "node:util"; | |
| const BATCH_SIZE = 1; | |
| const TIMEOUT = 2000; | |
| const verifyHandle = async (handle: string) => { | |
| const url = `https://bsky.social/xrpc/com.atproto.server.createAccount`; | |
| const response = await fetch(url, { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| }, | |
| body: JSON.stringify({ | |
| email: "[email protected]", | |
| password: "Password1", | |
| handle: `${handle}.bsky.social`, | |
| inviteCode: "", | |
| }), | |
| }); | |
| try { | |
| const json = await response.text(); | |
| const data = JSON.parse(json); | |
| if (data.error === "InvalidPhoneVerification") { | |
| return { handle, available: true }; | |
| } | |
| return { handle, available: false }; | |
| } catch (error) { | |
| return { handle, available: false }; | |
| } | |
| }; | |
| const sleep = promisify(setTimeout); | |
| const main = async () => { | |
| const validHandles: string[] = []; | |
| const handles: string[] = [ | |
| /* Add your hardcoded handles to check or fetch/compute them */ | |
| ]; | |
| const batches: string[][] = []; | |
| for (let i = 0; i < handles.length; i += BATCH_SIZE) { | |
| batches.push(handles.slice(i, i + BATCH_SIZE)); | |
| } | |
| for (let i = 0; i < batches.length; i++) { | |
| const promises = batches[i].map((handle) => verifyHandle(handle)); | |
| const responses = await Promise.all(promises); | |
| validHandles.push( | |
| ...responses | |
| .filter((response) => response.available) | |
| .map((response) => response.handle) | |
| ); | |
| console.log(`${i + 1}/${batches.length} batches completed`); | |
| await sleep(TIMEOUT); | |
| } | |
| console.log("Valid handles:\n"); | |
| console.log(validHandles.join("\n")); | |
| }; | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment