From 55a6453f03b54b3ebca9f3da2d8468bdc08ed9b4 Mon Sep 17 00:00:00 2001 From: Gadi Cohen Date: Mon, 18 Sep 2023 09:55:47 +0100 Subject: [PATCH] store crumb in cookie store too (#670) --- bin/yahoo-finance.js | 4 +--- src/lib/getCrumb.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/bin/yahoo-finance.js b/bin/yahoo-finance.js index 8b1fae05..826d8c54 100755 --- a/bin/yahoo-finance.js +++ b/bin/yahoo-finance.js @@ -8,7 +8,7 @@ import { ExtendedCookieJar } from "../dist/esm/src/lib/cookieJar.js"; const cookiePath = path.join(os.homedir(), ".yf2-cookies.json"); const cookieJar = new ExtendedCookieJar(new FileCookieStore(cookiePath)); -// yahooFinance.setGlobalConfig({ cookieJar }); +yahooFinance.setGlobalConfig({ cookieJar }); const moduleNames = Object.keys(yahooFinance).filter((n) => !n.startsWith("_")); // moduleNames.push("_chart"); // modules in development @@ -67,6 +67,4 @@ function decodeArgs(stringArgs) { if (process.stdout.isTTY) console.dir(result, { depth: null, colors: true }); else console.log(JSON.stringify(result, null, 2)); - - console.dir(await yahooFinance[moduleName](...args)); })(); diff --git a/src/lib/getCrumb.ts b/src/lib/getCrumb.ts index 80607ac1..6d8f1a1a 100644 --- a/src/lib/getCrumb.ts +++ b/src/lib/getCrumb.ts @@ -1,5 +1,8 @@ import type { RequestInfo, RequestInit, Response } from "node-fetch"; import type { ExtendedCookieJar } from "./cookieJar"; +import { Cookie } from "tough-cookie"; + +const CONFIG_FAKE_URL = "http://config.yf2/"; let crumb: string | null = null; // let crumbFetchTime = 0; @@ -20,6 +23,17 @@ export async function _getCrumb( ): Promise { // if (crumb && crumbFetchTime + MAX_CRUMB_CACHE_TIME > Date.now()) return crumb; + if (!crumb) { + const cookies = await cookieJar.getCookies(CONFIG_FAKE_URL); + for (const cookie of cookies) { + if (cookie.key === "crumb") { + crumb = cookie.value; + console.log("Retrieved crumb from cookie store: " + crumb); + break; + } + } + } + if (crumb && !noCache) { // If we still have a valid (non-expired) cookie, return the existing crumb. const existingCookies = await cookieJar.getCookies(url, { expire: true }); @@ -265,6 +279,14 @@ export async function _getCrumb( ); // crumbFetchTime = Date.now(); + console.log("New crumb: " + crumb); + await cookieJar.setCookie( + new Cookie({ + key: "crumb", + value: crumb, + }), + CONFIG_FAKE_URL + ); return crumb; }