Skip to content

Commit

Permalink
store crumb in cookie store too (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Sep 18, 2023
1 parent 04797f3 commit 55a6453
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 1 addition & 3 deletions bin/yahoo-finance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
})();
22 changes: 22 additions & 0 deletions src/lib/getCrumb.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,6 +23,17 @@ export async function _getCrumb(
): Promise<string | null> {
// 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 });
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 55a6453

Please sign in to comment.