Browse Source

Simplify search (#828)

* Simplify search

* Update changelog
pull/831/head
Thomas Kaul 3 years ago
committed by GitHub
parent
commit
65e062ad26
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      CHANGELOG.md
  2. 28
      apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

4
CHANGELOG.md

@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support to export future activities (drafts) as `.ics` files - Added support to export future activities (drafts) as `.ics` files
### Changed
- Migrated the search functionality to `yahoo-finance2`
## 1.136.0 - 13.04.2022 ## 1.136.0 - 13.04.2022
### Changed ### Changed

28
apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

@ -16,7 +16,6 @@ import {
DataSource, DataSource,
SymbolProfile SymbolProfile
} from '@prisma/client'; } from '@prisma/client';
import * as bent from 'bent';
import Big from 'big.js'; import Big from 'big.js';
import { countries } from 'countries-list'; import { countries } from 'countries-list';
import { addDays, format, isSameDay } from 'date-fns'; import { addDays, format, isSameDay } from 'date-fns';
@ -25,8 +24,6 @@ import type { Price } from 'yahoo-finance2/dist/esm/src/modules/quoteSummary-ifa
@Injectable() @Injectable()
export class YahooFinanceService implements DataProviderInterface { export class YahooFinanceService implements DataProviderInterface {
private readonly yahooFinanceHostname = 'https://query1.finance.yahoo.com';
public constructor( public constructor(
private readonly cryptocurrencyService: CryptocurrencyService private readonly cryptocurrencyService: CryptocurrencyService
) {} ) {}
@ -244,16 +241,7 @@ export class YahooFinanceService implements DataProviderInterface {
const items: LookupItem[] = []; const items: LookupItem[] = [];
try { try {
const get = bent( const searchResult = await yahooFinance.search(aQuery);
`${this.yahooFinanceHostname}/v1/finance/search?q=${encodeURIComponent(
aQuery
)}&lang=en-US&region=US&quotesCount=8&newsCount=0&enableFuzzyQuery=false&quotesQueryId=tss_match_phrase_query&multiQuoteQueryId=multi_quote_single_token_query&newsQueryId=news_cie_vespa&enableCb=true&enableNavLinks=false&enableEnhancedTrivialQuery=true`,
'GET',
'json',
200
);
const searchResult = await get();
const quotes = searchResult.quotes const quotes = searchResult.quotes
.filter((quote) => { .filter((quote) => {
@ -279,20 +267,24 @@ export class YahooFinanceService implements DataProviderInterface {
return true; return true;
}); });
const marketData = await this.getQuotes( const marketData = await yahooFinance.quote(
quotes.map(({ symbol }) => { quotes.map(({ symbol }) => {
return symbol; return symbol;
}) })
); );
for (const [symbol, value] of Object.entries(marketData)) { for (const marketDataItem of marketData) {
const quote = quotes.find((currentQuote: any) => { const quote = quotes.find((currentQuote) => {
return currentQuote.symbol === symbol; return currentQuote.symbol === marketDataItem.symbol;
}); });
const symbol = this.convertFromYahooFinanceSymbol(
marketDataItem.symbol
);
items.push({ items.push({
symbol, symbol,
currency: value.currency, currency: marketDataItem.currency,
dataSource: this.getName(), dataSource: this.getName(),
name: quote?.longname || quote?.shortname || symbol name: quote?.longname || quote?.shortname || symbol
}); });

Loading…
Cancel
Save