Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(yahoo): YF_QUERY_HOST in URLs (user configurable, default query2) #109

Merged
merged 13 commits into from
Mar 6, 2022
Merged
17 changes: 16 additions & 1 deletion docs/other/setGlobalConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ yahooFinance.setGlobalConfig({
Notes:

- Config provided to this function is validated.

- Options are merged infinite levels deep:

```js
import yahooFinance from 'yahoo-finance2';

Expand All @@ -29,3 +29,18 @@ yahooFinance.setGlobalConfig({
}
});
```

## Options not documented elsewhere

* `YF_QUERY_HOST`

* Default: `query2.yahoo.finance.com`
* Description: the host to use to query Yahoo's API.
As per
[this stackoverflow post](https://stackoverflow.com/questions/44030983/yahoo-finance-url-not-working/47505102#47505102):

* `query1.finance.yahoo.com` serves `HTTP/1.0`
* `query2.finance.yahoo.com` serves `HTTP/1.1`
* [Differences between HTTP/1.0 and HTTP/1.1](https://stackoverflow.com/questions/246859/http-1-0-vs-1-1)

* This option in particular may also be set with an environment variable of the same name.
3 changes: 3 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"YahooFinanceOptions": {
"type": "object",
"properties": {
"YF_QUERY_HOST": {
"type": "string"
},
"queue": {
"$ref": "#/definitions/QueueOptions"
},
Expand Down
6 changes: 6 additions & 0 deletions src/lib/fetchDevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ async function fetchDevel(url, fetchOptions) {
if (process.env.FETCH_DEVEL === "nocache")
return await nodeFetch(url, fetchOptions);

// Use query2 for all our tests / fixtures / cache
url = url.replace(
/^https:\/\/query1.finance.yahoo.com/,
"https://query2.finance.yahoo.com"
);

// If devel===true, hash the url, otherwise use the value of devel
// This allows us to specify our own static filename vs url hash.
/*
Expand Down
2 changes: 2 additions & 0 deletions src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import type { ValidationOptions } from "./validateAndCoerceTypes.js";
import type { QueueOptions } from "./queue.js";

export interface YahooFinanceOptions {
YF_QUERY_HOST?: string;
queue?: QueueOptions;
validation?: ValidationOptions;
}

const options: YahooFinanceOptions = {
YF_QUERY_HOST: process.env.YF_QUERY_HOST || "query2.finance.yahoo.com",
queue: {
concurrency: 4, // Min: 1, Max: Infinity
timeout: 60,
Expand Down
35 changes: 32 additions & 3 deletions src/lib/yahooFinanceFetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as util from "util";
import { jest } from "@jest/globals";

import Queue from "./queue.js";
import _yahooFinanceFetch from "./yahooFinanceFetch.js";
import _yahooFinanceFetch, {
substituteVariables,
} from "./yahooFinanceFetch.js";
import errors from "./errors.js";

import _env from "../env-node.js";
Expand Down Expand Up @@ -44,7 +46,7 @@ describe("yahooFinanceFetch", () => {
it("throws HTTPError if !res.ok and no error in json result", () => {
return expect(
yahooFinanceFetch(
"https://query1.finance.yahoo.com/nonExistingURL-CACHED",
"https://query2.finance.yahoo.com/nonExistingURL-CACHED",
{},
{ devel: "pageWith404andJson.fake.json" }
)
Expand All @@ -54,7 +56,7 @@ describe("yahooFinanceFetch", () => {
it("throws Error if we receive unknown error from json result", () => {
return expect(
yahooFinanceFetch(
"https://query1.finance.yahoo.com/nonExistingURL-CACHED",
"https://query2.finance.yahoo.com/nonExistingURL-CACHED",
{},
{ devel: "pageWithUnknownError.json" }
)
Expand Down Expand Up @@ -170,4 +172,31 @@ describe("yahooFinanceFetch", () => {

// TODO, timeout test
});

describe("URL variable substitution", () => {
const that = { _opts };

it("subs YF_QUERY_HOST from _opts", () => {
const origUrl = "https://${YF_QUERY_HOST}/v8/something";
// @ts-ignore: partial This for testing
const newUrl = substituteVariables.call(that, origUrl);
expect(newUrl).toBe("https://query2.finance.yahoo.com/v8/something");
});

it("subs query2 if no option", () => {
const that = { _env, _opts: { ..._opts } };
delete that._opts.YF_QUERY_HOST;
const origUrl = "https://${YF_QUERY_HOST}/v8/something";
// @ts-ignore: partial This for testing
const newUrl = substituteVariables.call(that, origUrl);
expect(newUrl).toBe("https://query2.finance.yahoo.com/v8/something");
});

it("leaves non-matches", () => {
const origUrl = "${something}";
// @ts-ignore: partial This for testing
const newUrl = substituteVariables.call(that, origUrl);
expect(newUrl).toBe("${something}");
});
});
});
17 changes: 16 additions & 1 deletion src/lib/yahooFinanceFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ function assertQueueOptions(queue: any, opts: any) {
queue.timeout = opts.timeout;
}

function substituteVariables(this: YahooFinanceFetchThis, urlBase: string) {
return urlBase.replace(/\$\{([^\}]+)\}/g, (match, varName) => {
if (varName === "YF_QUERY_HOST") {
// const hosts = ["query1.finance.yahoo.com", "query2.finance.yahoo.com"];
// return hosts[Math.floor(Math.random() * hosts.length)];
return this._opts.YF_QUERY_HOST || "query2.finance.yahoo.com";
} else {
// i.e. return unsubstituted original variable expression ${VAR}
return match;
}
});
}

async function yahooFinanceFetch(
this: YahooFinanceFetchThis,
urlBase: string,
Expand All @@ -62,7 +75,8 @@ async function yahooFinanceFetch(

// @ts-ignore TODO copy interface? @types lib?
const urlSearchParams = new URLSearchParams(params);
const url = urlBase + "?" + urlSearchParams.toString();
const url =
substituteVariables.call(this, urlBase) + "?" + urlSearchParams.toString();

/* istanbul ignore next */
// no need to force coverage on real network request.
Expand Down Expand Up @@ -114,4 +128,5 @@ async function yahooFinanceFetch(
return result;
}

export { substituteVariables };
export default yahooFinanceFetch;
2 changes: 1 addition & 1 deletion src/modules/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export default async function _chart(

query: {
assertSymbol: symbol,
url: "https://query1.finance.yahoo.com/v8/finance/chart/" + symbol,
url: "https://${YF_QUERY_HOST}/v8/finance/chart/" + symbol,
schemaKey: "#/definitions/ChartOptions",
defaults: queryOptionsDefaults,
overrides: queryOptionsOverrides,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/historical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function historical(

query: {
assertSymbol: symbol,
url: "https://query1.finance.yahoo.com/v7/finance/download/" + symbol,
url: "https://${YF_QUERY_HOST}/v7/finance/download/" + symbol,
schemaKey: "#/definitions/HistoricalOptions",
defaults: queryOptionsDefaults,
overrides: queryOptionsOverrides,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default function trendingSymbols(
moduleName: "insights",
query: {
assertSymbol: symbol,
url: "https://query2.finance.yahoo.com/ws/insights/v2/finance/insights",
url: "https://${YF_QUERY_HOST}/ws/insights/v2/finance/insights",
schemaKey: "#/definitions/InsightsOptions",
defaults: queryOptionsDefaults,
overrides: queryOptionsOverrides,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default function options(

query: {
assertSymbol: symbol,
url: "https://query1.finance.yahoo.com/v7/finance/options/" + symbol,
url: "https://${YF_QUERY_HOST}/v7/finance/options/" + symbol,
schemaKey: "#/definitions/OptionsOptions",
defaults: queryOptionsDefaults,
overrides: queryOptionsOverrides,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export default async function quote(
moduleName: "quote",

query: {
url: "https://query2.finance.yahoo.com/v7/finance/quote",
url: "https://${YF_QUERY_HOST}/v7/finance/quote",
schemaKey: "#/definitions/QuoteOptions",
defaults: queryOptionsDefaults,
runtime: { symbols },
Expand Down
3 changes: 1 addition & 2 deletions src/modules/quoteSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ export default function quoteSummary(

query: {
assertSymbol: symbol,
url:
"https://query2.finance.yahoo.com/v10/finance/quoteSummary/" + symbol,
url: "https://${YF_QUERY_HOST}/v10/finance/quoteSummary/" + symbol,
schemaKey: "#/definitions/QuoteSummaryOptions",
defaults: queryOptionsDefaults,
overrides: queryOptionsOverrides,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/recommendationsBySymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export default function recommendationsBySymbol(
moduleName: "recommendationsBySymbol",

query: {
url: `https://query2.finance.yahoo.com/v6/finance/recommendationsbysymbol/${symbols}`,
url:
"https://${YF_QUERY_HOST}/v6/finance/recommendationsbysymbol/" +
symbols,
schemaKey: "#/definitions/RecommendationsBySymbolOptions",
defaults: queryOptionsDefaults,
overrides: queryOptionsOverrides,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default function search(
moduleName: "search",

query: {
url: "https://query2.finance.yahoo.com/v1/finance/search",
url: "https://${YF_QUERY_HOST}/v1/finance/search",
schemaKey: "#/definitions/SearchOptions",
defaults: queryOptionsDefaults,
runtime: { q: query },
Expand Down
2 changes: 1 addition & 1 deletion src/modules/trendingSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function trendingSymbols(
return this._moduleExec({
moduleName: "trendingSymbols",
query: {
url: `https://query1.finance.yahoo.com/v1/finance/trending/${query}`,
url: "https://${YF_QUERY_HOST}/v1/finance/trending/" + query,
schemaKey: "#/definitions/TrendingSymbolsOptions",
defaults: queryOptionsDefaults,
overrides: queryOptionsOverrides,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"url": "https://query1.finance.yahoo.com/v7/finance/download/BEKE?interval=1d&events=history&includeAdjustedClose=true&period1=1577836800&period2=1578009600"
"url": "https://query2.finance.yahoo.com/v7/finance/download/BEKE?interval=1d&events=history&includeAdjustedClose=true&period1=1577836800&period2=1578009600"
},
"response": {
"ok": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"url": "https://query1.finance.yahoo.com/v7/finance/download/BFLY?interval=1d&events=history&includeAdjustedClose=true&period1=1577836800&period2=1578009600"
"url": "https://query2.finance.yahoo.com/v7/finance/download/BFLY?interval=1d&events=history&includeAdjustedClose=true&period1=1577836800&period2=1578009600"
},
"response": {
"ok": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"url": "https://query1.finance.yahoo.com/v7/finance/download/BRKS?interval=1d&events=history&includeAdjustedClose=true&period1=1577836800&period2=1578009600"
"url": "https://query2.finance.yahoo.com/v7/finance/download/BRKS?interval=1d&events=history&includeAdjustedClose=true&period1=1577836800&period2=1578009600"
},
"response": {
"ok": true,
Expand Down
73 changes: 0 additions & 73 deletions tests/http/4-7-2021/historical-CRON-2020-01-01-to-2020-01-03.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"url": "https://query1.finance.yahoo.com/v7/finance/download/SIMP?interval=1d&events=history&includeAdjustedClose=true&period1=1577836800&period2=1578009600"
"url": "https://query2.finance.yahoo.com/v7/finance/download/SIMP?interval=1d&events=history&includeAdjustedClose=true&period1=1577836800&period2=1578009600"
},
"response": {
"ok": false,
Expand Down
2 changes: 1 addition & 1 deletion tests/http/4-7-2021/trendingSymbols-BRKS.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"url": "https://query1.finance.yahoo.com/v7/finance/options/BRKS?formatted=false&lang=en-US&region=US"
"url": "https://query2.finance.yahoo.com/v7/finance/options/BRKS?formatted=false&lang=en-US&region=US"
},
"response": {
"ok": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"url": "https://query1.finance.yahoo.com/v8/finance/chart/0P000071W8.TO?useYfid=true&interval=1d&includePrePost=true&events=div%7Csplit%7Cearn&lang=en-US&period1=1577836800&period2=1578009600"
"url": "https://query2.finance.yahoo.com/v8/finance/chart/0P000071W8.TO?useYfid=true&interval=1d&includePrePost=true&events=div%7Csplit%7Cearn&lang=en-US&period1=1577836800&period2=1578009600"
},
"response": {
"ok": true,
Expand Down
2 changes: 1 addition & 1 deletion tests/http/chart-AAPL-2020-01-01-to-2020-01-03.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"url": "https://query1.finance.yahoo.com/v8/finance/chart/AAPL?useYfid=true&interval=1d&includePrePost=true&events=div%7Csplit%7Cearn&lang=en-US&period1=1577836800&period2=1578009600"
"url": "https://query2.finance.yahoo.com/v8/finance/chart/AAPL?useYfid=true&interval=1d&includePrePost=true&events=div%7Csplit%7Cearn&lang=en-US&period1=1577836800&period2=1578009600"
},
"response": {
"ok": true,
Expand Down
2 changes: 1 addition & 1 deletion tests/http/chart-AAPL-2020-08-31-to-2020-11-07.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"url": "https://query1.finance.yahoo.com/v8/finance/chart/AAPL?useYfid=true&interval=1d&includePrePost=true&events=div%7Csplit%7Cearn&lang=en-US&period1=1598832000&period2=1604707200"
"url": "https://query2.finance.yahoo.com/v8/finance/chart/AAPL?useYfid=true&interval=1d&includePrePost=true&events=div%7Csplit%7Cearn&lang=en-US&period1=1598832000&period2=1604707200"
},
"response": {
"ok": true,
Expand Down
2 changes: 1 addition & 1 deletion tests/http/chart-AAPL.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"url": "https://query1.finance.yahoo.com/v8/finance/chart/AAPL?useYfid=true&interval=1d&includePrePost=true&events=div%7Csplit%7Cearn&lang=en-US&period1=1645488000&period2=1645574400"
"url": "https://query2.finance.yahoo.com/v8/finance/chart/AAPL?useYfid=true&interval=1d&includePrePost=true&events=div%7Csplit%7Cearn&lang=en-US&period1=1645488000&period2=1645574400"
},
"response": {
"ok": true,
Expand Down
Loading