import { Command } from "commander"; import { app as config } from "../../src/config/default"; import * as akashicModule from "../../src/modules/akashic"; import "dotenv/config"; import * as admin from "firebase-admin"; const msgpack = require("msgpack-lite"); const serviceAccount = require(process.env.GOOGLE_APPLICATION_CREDENTIALS!); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), }); const akashicClient = akashicModule.createWrapper({ mode: config.akashic.mode, baseUrl: config.akashic.baseUrl, apiKey: config.akashic.apiKey, refreshToken: config.akashic.refreshToken, refreshTokenUrl: config.akashic.refreshTokenUrl, }); // 一応、これでplaylog出力できるが、告原環境だと以下をやる必要あり // OPENSSL_CONF=/home/tsuge/openssl.unsafe.cnf npx ts-node scripts/akashic/playlog.ts > playlog.json async function testPlaylog(akashicPlayId: string) { try { const result = await akashicClient.getPlaylog(akashicPlayId); console.log(JSON.stringify(msgpack.decode(Buffer.from(result.playlog, "base64")), undefined, "\t")); } catch (error) { console.error(error); } } async function executeByPlayId(playId: string) { const playDocRef = admin.firestore().doc(`/plays/${playId}`); const playDoc = await playDocRef.get(); const playData = playDoc.data(); if (playData == null) { throw new Error(`対象のplayId: ${playId}が存在しません`); } const akashicPlayId = playData.akashicPlayId as string | undefined; if (akashicPlayId == null) { throw new Error(`対象のplayId: ${playId}にAkashicのPlayIdが存在しません`); } await testPlaylog(akashicPlayId); // await testPlaylog("2234"); } function execute(url: string) { const parsedUrl = new URL(url); const paths = parsedUrl.pathname.split("/"); const playId = paths[2]; return executeByPlayId(playId).catch((error: Error) => { console.log(`対象のurlが存在しません`, url, error.message); }); } const program = new Command(); program.option("-u, --url ", "target url").option("-p, --play-id ", "target playId").parse(process.argv); const url = program.opts().url as string | undefined; const playId = program.opts().playId as string | undefined; if (url != null) { execute(url); } else if (playId != null) { executeByPlayId(playId); } else { console.log("-uとかで指定してください"); }