Skip to content

Commit

Permalink
feat(cli): use a file-cookie-store (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Sep 18, 2023
1 parent 6cde96d commit e2239b4
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
13 changes: 13 additions & 0 deletions bin/yahoo-finance.js
@@ -1,6 +1,15 @@
#!/usr/bin/env -S node --experimental-json-modules --experimental-vm-modules

import os from "os";
import path from "path";
import { FileCookieStore } from "tough-cookie-file-store";
import yahooFinance from "../dist/esm/src/index-node.js";
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 });

const moduleNames = Object.keys(yahooFinance).filter((n) => !n.startsWith("_"));
// moduleNames.push("_chart"); // modules in development

Expand Down Expand Up @@ -32,6 +41,8 @@ if (!moduleNames.includes(moduleName)) {
process.exit();
}

console.log("Storing cookies in " + cookiePath);

function decodeArgs(stringArgs) {
return stringArgs.map((arg) => {
if (arg[0] === "{") return JSON.parse(arg);
Expand All @@ -56,4 +67,6 @@ 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));
})();
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -63,7 +63,8 @@
"ajv": "8.10.0",
"ajv-formats": "2.1.1",
"node-fetch": "^2.6.1",
"tough-cookie": "^4.1.2"
"tough-cookie": "^4.1.2",
"tough-cookie-file-store": "^2.0.3"
},
"devDependencies": {
"@semantic-release/changelog": "6.0.3",
Expand Down
3 changes: 0 additions & 3 deletions schema.json
Expand Up @@ -18,9 +18,6 @@
"$ref": "#/definitions/ValidationOptions"
}
},
"required": [
"cookieJar"
],
"additionalProperties": false
},
"ExtendedCookieJar": {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/options.ts
Expand Up @@ -12,7 +12,7 @@ export interface Logger {

export interface YahooFinanceOptions {
YF_QUERY_HOST?: string;
cookieJar: ExtendedCookieJar;
cookieJar?: ExtendedCookieJar;
queue?: QueueOptions;
validation?: ValidationOptions;
logger: Logger;
Expand Down
12 changes: 11 additions & 1 deletion src/lib/setGlobalConfig.ts
@@ -1,11 +1,15 @@
import type { YahooFinanceOptions } from "./options.js";
import type { ModuleThis } from "./moduleCommon.js";
import validateAndCoerceTypes from "./validateAndCoerceTypes.js";
import { ExtendedCookieJar } from "./cookieJar.js";

export default function setGlobalConfig(
this: ModuleThis,
config: YahooFinanceOptions
_config: YahooFinanceOptions
): void {
// Instances (e.g. cookieJar) don't validate well :)
const { cookieJar, ...config } = _config;

validateAndCoerceTypes({
object: config,
source: "setGlobalConfig",
Expand All @@ -14,6 +18,12 @@ export default function setGlobalConfig(
schemaKey: "#/definitions/YahooFinanceOptions",
});
mergeObjects(this._opts, config);

if (cookieJar) {
if (!(cookieJar instanceof ExtendedCookieJar))
throw new Error("cookieJar must be an instance of ExtendedCookieJar");
this._opts.cookieJar = cookieJar;
}
}

type Obj = Record<string, any>;
Expand Down
8 changes: 7 additions & 1 deletion src/lib/yahooFinanceFetch.ts
Expand Up @@ -92,6 +92,8 @@ async function yahooFinanceFetch(
};

if (needsCrumb) {
if (!this._opts.cookieJar) throw new Error("No cookieJar set");

const crumb = await getCrumb(
this._opts.cookieJar,
fetchFunc,
Expand All @@ -109,6 +111,8 @@ async function yahooFinanceFetch(

// console.log(cookieJar.serializeSync());

if (!this._opts.cookieJar) throw new Error("No cookieJar set");

const fetchOptions = {
...fetchOptionsBase,
headers: {
Expand All @@ -127,8 +131,10 @@ async function yahooFinanceFetch(
const response = (await queue.add(() => fetchFunc(url, fetchOptions))) as any;

const setCookieHeaders = response.headers.raw()["set-cookie"];
if (setCookieHeaders)
if (setCookieHeaders) {
if (!this._opts.cookieJar) throw new Error("No cookieJar set");
this._opts.cookieJar.setFromSetCookieHeaders(setCookieHeaders, url);
}

const result = await response[func]();

Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Expand Up @@ -6307,7 +6307,14 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"

tough-cookie@^4.1.2:
tough-cookie-file-store@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/tough-cookie-file-store/-/tough-cookie-file-store-2.0.3.tgz#788f7a6fe5cd8f61a1afb71b2f0b964ebf914b80"
integrity sha512-sMpZVcmFf6EYFHFFl+SYH4W1/OnXBYMGDsv2IlbQ2caHyFElW/UR/gpj/KYU1JwmP4dE9xqwv2+vWcmlXHojSw==
dependencies:
tough-cookie "^4.0.0"

tough-cookie@^4.0.0, tough-cookie@^4.1.2:
version "4.1.3"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-4.1.3.tgz#97b9adb0728b42280aa3d814b6b999b2ff0318bf"
integrity sha512-aX/y5pVRkfRnfmuX+OdbSdXvPe6ieKX/G2s7e98f4poJHnqH3281gDPm/metm6E/WRamfx7WC4HUqkWHfQHprw==
Expand Down

0 comments on commit e2239b4

Please sign in to comment.