From 52b585c50eabb064ac248e57629f7a7f8352e1da Mon Sep 17 00:00:00 2001
From: Gadi Cohen <dragon@wastelands.net>
Date: Thu, 28 Jan 2021 17:58:44 +0200
Subject: [PATCH] fix(search): transform dates

---
 schema.json           | 139 +++++++++++++++++++++++++++++++++++++++---
 src/modules/search.ts |  19 ++++--
 2 files changed, 146 insertions(+), 12 deletions(-)

diff --git a/schema.json b/schema.json
index c653f934..0042bdee 100644
--- a/schema.json
+++ b/schema.json
@@ -1,7 +1,7 @@
 {
   "$schema": "http://json-schema.org/draft-07/schema#",
   "definitions": {
-    "YahooFinanceAutocResult": {
+    "AutocResult": {
       "additionalProperties": false,
       "properties": {
         "exch": {
@@ -33,7 +33,7 @@
       ],
       "type": "object"
     },
-    "YahooFinanceAutocResultSet": {
+    "AutocResultSet": {
       "additionalProperties": false,
       "properties": {
         "Query": {
@@ -42,7 +42,7 @@
         "Result": {
           "items": [
             {
-              "$ref": "#/definitions/YahooFinanceAutocResult"
+              "$ref": "#/definitions/AutocResult"
             }
           ],
           "maxItems": 100,
@@ -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": {
@@ -88,7 +121,7 @@
       ],
       "type": "object"
     },
-    "YahooFinanceSearchQuote": {
+    "SearchQuote": {
       "additionalProperties": false,
       "properties": {
         "exchange": {
@@ -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": {
@@ -156,7 +279,7 @@
         "news": {
           "items": [
             {
-              "$ref": "#/definitions/YahooFinanceSearchNews"
+              "$ref": "#/definitions/SearchNewsOrig"
             }
           ],
           "maxItems": 100,
@@ -166,7 +289,7 @@
         "quotes": {
           "items": [
             {
-              "$ref": "#/definitions/YahooFinanceSearchQuote"
+              "$ref": "#/definitions/SearchQuote"
             }
           ],
           "maxItems": 100,
diff --git a/src/modules/search.ts b/src/modules/search.ts
index 5b4abb2a..6f23638c 100644
--- a/src/modules/search.ts
+++ b/src/modules/search.ts
@@ -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"
@@ -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"
@@ -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;
   /**
@@ -37,7 +41,7 @@ export interface SearchResult {
    * @minItems 0
    * @maxItems 100
    */
-  news: [SearchNews];
+  news: [SearchNewsOrig];
   /**
    * @minItems 0
    * @maxItems 100
@@ -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;
@@ -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;
 }