Created
March 24, 2024 02:36
-
-
Save sheepla/c7768194ed82e2f52ea9fb394b0fbf30 to your computer and use it in GitHub Desktop.
This file contains 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 { passwordGenerator } from "https://deno.land/x/password_generator/mod.ts"; | |
const usage = ` | |
About: | |
Issue abema.tv onetime password | |
Usage: | |
deno run --allow-net main.ts <user> <token> | |
See: | |
https://abema.tv/account | |
`; | |
// TODO Get token from user name automatically | |
async function issueOneTimePassword( | |
token: string, | |
user: string, | |
password: string, | |
): Promise<string> { | |
const payload = JSON.stringify({ | |
"password": password, | |
}); | |
return await fetch( | |
`https://api.p-c3-e.abema-tv.com/v1/users/${user}/oneTimePassword`, | |
{ | |
"method": "PUT", | |
"credentials": "include", | |
"headers": { | |
"User-Agent": | |
"Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0", | |
"Accept": "*/*", | |
"Accept-Language": "ja,en-US;q=0.7,en;q=0.3", | |
"Content-Type": "application/json", | |
"Sec-Fetch-Dest": "empty", | |
"Sec-Fetch-Mode": "cors", | |
"Sec-Fetch-Site": "cross-site", | |
"Sec-GPC": "1", | |
"Authorization": `bearer ${token}`, | |
}, | |
"referrer": "https://abema.tv/", | |
"body": payload, | |
"mode": "cors", | |
}, | |
).then((resp) => { | |
if (resp.ok) { | |
return password; | |
} else { | |
throw new Error(`HTTP error: ${Array.from(resp.headers)}`); | |
} | |
}); | |
} | |
//async function readLine(prompt: string): Promise<string> { | |
// console.log(prompt); | |
// | |
// const buf = new Uint8Array(1024); | |
// const n = await Deno.stdin.read(buf); | |
// | |
// return new TextDecoder().decode(buf.subarray(0, n)).trim(); | |
//} | |
async function setRetry<T>( | |
operation: () => Promise<T>, | |
maxRetries: number, | |
delay: number, | |
): Promise<T> { | |
try { | |
return await operation(); | |
} catch (error) { | |
console.error(`Error: ${error}`); | |
if (maxRetries > 0) { | |
console.log(`Retrying after ${delay}msec...`); | |
await new Promise((resolve) => setTimeout(resolve, delay)); | |
return setRetry(operation, maxRetries - 1, delay); | |
} else { | |
throw new Error("Max retries exceeded. Operation failed."); | |
} | |
} | |
} | |
if (Deno.args.length !== 2) { | |
throw new Error(`Invalid arguments: (${Deno.args})\n${usage}`); | |
} | |
const user = Deno.args[0]; | |
const token = Deno.args[1]; | |
const issuedPassword = await setRetry( | |
() => { | |
const password = passwordGenerator( | |
/* pattern: */ "Aa0", | |
/* length: */ 10, | |
); | |
console.log(`Generated password: ${password}`); | |
return issueOneTimePassword(token, user, password); | |
}, | |
3, | |
3000, | |
); | |
console.log(`Password issued: ${issuedPassword}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment