Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

9.0 KB
Raw Blame
import { query } from '@ghostfolio/api/helper/object.helper';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import {
  DataProviderInterface,
  GetAssetProfileParams,
  GetDividendsParams,
  GetHistoricalParams,
  GetQuotesParams,
  GetSearchParams
} from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface';
import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service';
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service';
import {
  DATE_FORMAT,
  extractNumberFromString,
  getStartOfUtcDateOfYesterday
} from '@ghostfolio/common/helper';
import {
  DataProviderHistoricalResponse,
  DataProviderInfo,
  DataProviderResponse,
  LookupResponse,
  ScraperConfiguration
} from '@ghostfolio/common/interfaces';

import { utc } from '@date-fns/utc';
import { Injectable, Logger } from '@nestjs/common';
import { DataSource, SymbolProfile } from '@prisma/client';
import * as cheerio from 'cheerio';
import { addDays, format, isBefore } from 'date-fns';

@Injectable()
export class ManualService implements DataProviderInterface {
  private readonly logger = new Logger(ManualService.name);

  public constructor(
    private readonly configurationService: ConfigurationService,
    private readonly fetchService: FetchService,
    private readonly prismaService: PrismaService,
    private readonly symbolProfileService: SymbolProfileService
  ) {}

  public canHandle() {
    return true;
  }

  public async getAssetProfile({
    symbol
  }: GetAssetProfileParams): Promise<Partial<SymbolProfile>> {
    const [symbolProfile] = await this.symbolProfileService.getSymbolProfiles([
      { symbol, dataSource: this.getName() }
    ]);

    if (!symbolProfile) {
      return undefined;
    }

    return {
      symbol,
      currency: symbolProfile.currency,
      dataSource: this.getName(),
      name: symbolProfile.name
    };
  }

  public getDataProviderInfo(): DataProviderInfo {
    return {
      dataSource: DataSource.MANUAL,
      isPremium: false
    };
  }

  public async getDividends({}: GetDividendsParams) {
    return {};
  }

  public async getHistorical({
    from,
    symbol,
    to
  }: GetHistoricalParams): Promise<{
    [date: string]: DataProviderHistoricalResponse;
  }> {
    try {
      const [symbolProfile] = await this.symbolProfileService.getSymbolProfiles(
        [{ symbol, dataSource: this.getName() }]
      );
      const { defaultMarketPrice, selector, url } =
        symbolProfile?.scraperConfiguration ?? {};

      if (defaultMarketPrice) {
        const historical: {
          [date: string]: DataProviderHistoricalResponse;
        } = {};

        let date = from;

        while (isBefore(date, to)) {
          historical[format(date, DATE_FORMAT, { in: utc })] = {
            marketPrice: defaultMarketPrice
          };

          date = addDays(date, 1, { in: utc });
        }

        return historical;
      } else if (!selector || !url) {
        return {};
      }

      const value = await this.scrape({
        symbol,
        scraperConfiguration: symbolProfile.scraperConfiguration
      });

      return {
        [format(getStartOfUtcDateOfYesterday(), DATE_FORMAT, { in: utc })]: {
          marketPrice: value
        }
      };
    } catch (error) {
      throw new Error(
        `Could not get historical market data for ${symbol} (${this.getName()}) from ${format(
          from,
          DATE_FORMAT
        )} to ${format(to, DATE_FORMAT)}: [${error.name}] ${error.message}`
      );
    }
  }

  public getName(): DataSource {
    return DataSource.MANUAL;
  }

  public async getQuotes({
    symbols,
    useCache = true
  }: GetQuotesParams): Promise<{ [symbol: string]: DataProviderResponse }> {
    const response: { [symbol: string]: DataProviderResponse } = {};

    if (symbols.length <= 0) {
      return response;
    }

    try {
      const symbolProfiles = await this.symbolProfileService.getSymbolProfiles(
        symbols.map((symbol) => {
          return { symbol, dataSource: this.getName() };
        })
      );

      // Query the latest market data per symbol because distinct loads all
      // the market data of the symbols into memory
      const marketData = await Promise.all(
        symbols.map((symbol) => {
          return this.prismaService.marketData.findFirst({
            orderBy: {
              date: 'desc'
            },
            where: {
              symbol,
              dataSource: this.getName()
            }
          });
        })
      );

      const symbolProfilesToScrape = symbolProfiles.filter(
        ({ scraperConfiguration }) => {
          return (
            (scraperConfiguration?.mode === 'instant' || !useCache) &&
            scraperConfiguration?.selector &&
            scraperConfiguration?.url
          );
        }
      );

      const scraperResultPromises = symbolProfilesToScrape.map(
        async ({ scraperConfiguration, symbol }) => {
          try {
            const marketPrice = await this.scrape({
              scraperConfiguration,
              symbol
            });
            return { marketPrice, symbol };
          } catch (error) {
            this.logger.error(
              `Could not get quote for ${symbol} (${this.getName()}): [${error.name}] ${error.message}`
            );
            return { symbol, marketPrice: undefined };
          }
        }
      );

      // Wait for all scraping requests to complete concurrently
      const scraperResults = await Promise.all(scraperResultPromises);

      for (const { currency, symbol } of symbolProfiles) {
        let { marketPrice } =
          scraperResults.find((result) => {
            return result.symbol === symbol;
          }) ?? {};

        marketPrice =
          marketPrice ??
          marketData.find((marketDataItem) => {
            return marketDataItem?.symbol === symbol;
          })?.marketPrice ??
          0;

        response[symbol] = {
          currency,
          marketPrice,
          dataSource: this.getName(),
          marketState: 'delayed'
        };
      }

      return response;
    } catch (error) {
      this.logger.error(error.message);
    }

    return {};
  }

  public getTestSymbol() {
    return undefined;
  }

  public async search({
    query,
    userId
  }: GetSearchParams): Promise<LookupResponse> {
    const items = await this.prismaService.symbolProfile.findMany({
      select: {
        assetClass: true,
        assetSubClass: true,
        currency: true,
        dataSource: true,
        name: true,
        symbol: true,
        userId: true
      },
      where: {
        AND: [
          {
            dataSource: this.getName()
          },
          {
            OR: [
              {
                name: {
                  mode: 'insensitive',
                  startsWith: query
                }
              },
              {
                symbol: {
                  mode: 'insensitive',
                  startsWith: query
                }
              }
            ]
          },
          {
            OR: [{ userId }, { userId: null }]
          }
        ]
      }
    });

    return {
      items: items.map((item) => {
        return { ...item, dataProviderInfo: this.getDataProviderInfo() };
      })
    };
  }

  public async test({
    scraperConfiguration,
    symbol
  }: {
    scraperConfiguration: ScraperConfiguration;
    symbol: string;
  }) {
    return this.scrape({ scraperConfiguration, symbol });
  }

  private async scrape({
    scraperConfiguration,
    symbol
  }: {
    scraperConfiguration: ScraperConfiguration;
    symbol: string;
  }): Promise<number> {
    let locale = scraperConfiguration.locale;

    const response = await this.fetchService.fetch(scraperConfiguration.url, {
      headers: scraperConfiguration.headers as HeadersInit,
      signal: AbortSignal.timeout(
        this.configurationService.get('REQUEST_TIMEOUT')
      )
    });

    if (!response.ok) {
      throw new Error(
        `Failed to scrape the market price for ${symbol} (${this.getName()}): ${response.status} ${response.statusText} at ${scraperConfiguration.url}`
      );
    }

    let value: string;

    if (response.headers.get('content-type')?.includes('application/json')) {
      const object = await response.json();

      value = String(
        query({
          object,
          pathExpression: scraperConfiguration.selector
        })[0]
      );
    } else {
      const $ = cheerio.load(await response.text());

      if (!locale) {
        try {
          locale = $('html').attr('lang');
        } catch {}
      }

      value = $(scraperConfiguration.selector).first().text();

      const lines = value?.split('\n') ?? [];

      const lineWithDigits = lines.find((line) => {
        return /\d/.test(line);
      });

      if (lineWithDigits) {
        value = lineWithDigits;
      }

      return extractNumberFromString({
        locale,
        value
      });
    }

    return extractNumberFromString({ locale, value });
  }
}