Skip to content

Commit

Permalink
fix(search): transform dates
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Jan 28, 2021
1 parent 1e01536 commit 52b585c
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 12 deletions.
139 changes: 131 additions & 8 deletions schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"YahooFinanceAutocResult": {
"AutocResult": {
"additionalProperties": false,
"properties": {
"exch": {
Expand Down Expand Up @@ -33,7 +33,7 @@
],
"type": "object"
},
"YahooFinanceAutocResultSet": {
"AutocResultSet": {
"additionalProperties": false,
"properties": {
"Query": {
Expand All @@ -42,7 +42,7 @@
"Result": {
"items": [
{
"$ref": "#/definitions/YahooFinanceAutocResult"
"$ref": "#/definitions/AutocResult"
}
],
"maxItems": 100,
Expand All @@ -56,7 +56,40 @@
],
"type": "object"
},
"YahooFinanceSearchNews": {
"SearchNews": {
"additionalProperties": false,
"properties": {
"link": {
"type": "string"
},
"providerPublishTime": {
"format": "date-time",
"type": "string"
},
"publisher": {
"type": "string"
},
"title": {
"type": "string"
},
"type": {
"type": "string"
},
"uuid": {
"type": "string"
}
},
"required": [
"link",
"providerPublishTime",
"publisher",
"title",
"type",
"uuid"
],
"type": "object"
},
"SearchNewsOrig": {
"additionalProperties": false,
"properties": {
"link": {
Expand Down Expand Up @@ -88,7 +121,7 @@
],
"type": "object"
},
"YahooFinanceSearchQuote": {
"SearchQuote": {
"additionalProperties": false,
"properties": {
"exchange": {
Expand Down Expand Up @@ -132,7 +165,97 @@
],
"type": "object"
},
"YahooFinanceSearchResult": {
"SearchResult": {
"additionalProperties": false,
"properties": {
"count": {
"type": "number"
},
"explains": {
"maxItems": 0,
"minItems": 0,
"type": "array"
},
"lists": {
"maxItems": 100,
"minItems": 0,
"type": "array"
},
"nav": {
"maxItems": 100,
"minItems": 0,
"type": "array"
},
"news": {
"items": [
{
"$ref": "#/definitions/SearchNews"
}
],
"maxItems": 1,
"minItems": 1,
"type": "array"
},
"quotes": {
"items": [
{
"$ref": "#/definitions/SearchQuote"
}
],
"maxItems": 100,
"minItems": 0,
"type": "array"
},
"researchReports": {
"maxItems": 100,
"minItems": 0,
"type": "array"
},
"timeTakenForAlgowatchlist": {
"type": "number"
},
"timeTakenForCrunchbase": {
"type": "number"
},
"timeTakenForNav": {
"type": "number"
},
"timeTakenForNews": {
"type": "number"
},
"timeTakenForPredefinedScreener": {
"type": "number"
},
"timeTakenForQuotes": {
"type": "number"
},
"timeTakenForResearchReports": {
"type": "number"
},
"totalTime": {
"type": "number"
}
},
"required": [
"count",
"explains",
"lists",
"nav",
"news",
"quotes",
"researchReports",
"timeTakenForAlgowatchlist",
"timeTakenForCrunchbase",
"timeTakenForNav",
"timeTakenForNews",
"timeTakenForPredefinedScreener",
"timeTakenForQuotes",
"timeTakenForResearchReports",
"totalTime"
],
"type": "object"
},
"SearchResultOrig": {
"additionalProperties": false,
"properties": {
"count": {
Expand All @@ -156,7 +279,7 @@
"news": {
"items": [
{
"$ref": "#/definitions/YahooFinanceSearchNews"
"$ref": "#/definitions/SearchNewsOrig"
}
],
"maxItems": 100,
Expand All @@ -166,7 +289,7 @@
"quotes": {
"items": [
{
"$ref": "#/definitions/YahooFinanceSearchQuote"
"$ref": "#/definitions/SearchQuote"
}
],
"maxItems": 100,
Expand Down
19 changes: 15 additions & 4 deletions src/modules/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import yahooFinanceFetch = require('../lib/yahooFinanceFetch');
import validate from '../lib/validate';

const QUERY_URL = 'https://query2.finance.yahoo.com/v1/finance/search';
const QUERY_SCHEMA_KEY = "#/definitions/YahooFinanceSearchResult";
const QUERY_SCHEMA_KEY = "#/definitions/SearchResultOrig";

export interface SearchQuote {
exchange: string; // "NYQ"
Expand All @@ -16,7 +16,7 @@ export interface SearchQuote {
isYahooFinance: boolean; // true
}

export interface SearchNews {
export interface SearchNewsOrig {
uuid: string; // "9aff624a-e84c-35f3-9c23-db39852006dc"
title: string; // "Analyst Report: Alibaba Group Holding Limited"
publisher: string; // "Morningstar Research"
Expand All @@ -25,7 +25,11 @@ export interface SearchNews {
type: string; // "STORY" TODO "STORY" | ???
}

export interface SearchResult {
export interface SearchNews extends Omit<SearchNewsOrig,'providerPublishTime'> {
providerPublishTime: Date; // Date(1611286342 * 1000)
}

export interface SearchResultOrig {
explains: [];
count: number;
/**
Expand All @@ -37,7 +41,7 @@ export interface SearchResult {
* @minItems 0
* @maxItems 100
*/
news: [SearchNews];
news: [SearchNewsOrig];
/**
* @minItems 0
* @maxItems 100
Expand All @@ -63,6 +67,10 @@ export interface SearchResult {
timeTakenForResearchReports: number; // 0
}

export interface SearchResult extends Omit<SearchResultOrig,'news'> {
news: [SearchNews];
}

interface SearchOptions {
lang?: string;
region?: string;
Expand Down Expand Up @@ -105,6 +113,9 @@ async function search(
const result = await yahooFinanceFetch(QUERY_URL, queryOptions, fetchOptions);
validate(result, QUERY_SCHEMA_KEY);

for (let news of result.news)
news.providerPublishTime = new Date(news.providerPublishTime * 1000);

return result;
}

Expand Down

0 comments on commit 52b585c

Please sign in to comment.