Skip to content

Commit

Permalink
initial work to use file cookie store in cli (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Sep 15, 2023
1 parent f65f69c commit 04797f3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
13 changes: 13 additions & 0 deletions bin/yahoo-finance.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ExtendedCookieJar } from "./cookieJar.js";

export interface YahooFinanceOptions {
YF_QUERY_HOST?: string;
cookieJar: ExtendedCookieJar;
cookieJar?: ExtendedCookieJar;
queue?: QueueOptions;
validation?: ValidationOptions;
}
Expand Down
14 changes: 12 additions & 2 deletions src/lib/setGlobalConfig.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
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",
type: "options",
options: this._opts.validation,
schemaKey: "#/definitions/YahooFinanceOptions",
});
// @ts-expect-error: TODO

mergeObjects(this._opts, config as Obj);

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

type Obj = Record<string, string | ObjRecurse>;
Expand Down
8 changes: 7 additions & 1 deletion src/lib/yahooFinanceFetch.ts
Original file line number Diff line number Diff line change
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 @@ -108,6 +110,8 @@ async function yahooFinanceFetch(

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

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

const fetchOptions = {
...fetchOptionsBase,
headers: {
Expand All @@ -126,8 +130,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
Original file line number Diff line number Diff line change
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 04797f3

Please sign in to comment.