2021-08-14 11:06:21 +02:00
|
|
|
import { LookupItem } from '@ghostfolio/api/app/symbol/interfaces/lookup-item.interface';
|
2021-08-14 16:55:40 +02:00
|
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
2022-02-28 20:42:14 +01:00
|
|
|
import { DataProviderInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface';
|
2022-02-27 17:03:00 +01:00
|
|
|
import {
|
|
|
|
IDataProviderHistoricalResponse,
|
2022-05-08 22:16:47 +02:00
|
|
|
IDataProviderResponse
|
2022-02-27 17:03:00 +01:00
|
|
|
} from '@ghostfolio/api/services/interfaces/interfaces';
|
2021-09-11 11:14:55 +02:00
|
|
|
import { ghostfolioFearAndGreedIndexSymbol } from '@ghostfolio/common/config';
|
2022-12-23 11:06:52 +01:00
|
|
|
import { DATE_FORMAT, getYesterday } from '@ghostfolio/common/helper';
|
2021-05-16 22:11:14 +02:00
|
|
|
import { Granularity } from '@ghostfolio/common/types';
|
2021-11-07 21:25:18 +01:00
|
|
|
import { Injectable, Logger } from '@nestjs/common';
|
2022-02-27 17:03:00 +01:00
|
|
|
import { DataSource, SymbolProfile } from '@prisma/client';
|
2022-05-29 15:37:40 +02:00
|
|
|
import bent from 'bent';
|
2022-12-23 11:06:52 +01:00
|
|
|
import { format } from 'date-fns';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Injectable()
|
2022-10-15 10:31:46 +02:00
|
|
|
export class RapidApiService implements DataProviderInterface {
|
2021-04-18 19:06:54 +02:00
|
|
|
public constructor(
|
2022-12-23 11:06:52 +01:00
|
|
|
private readonly configurationService: ConfigurationService
|
2021-04-18 19:06:54 +02:00
|
|
|
) {}
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-05-27 20:50:10 +02:00
|
|
|
public canHandle(symbol: string) {
|
2022-10-15 10:31:46 +02:00
|
|
|
return !!this.configurationService.get('RAPID_API_API_KEY');
|
2021-05-27 20:50:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-27 17:03:00 +01:00
|
|
|
public async getAssetProfile(
|
|
|
|
aSymbol: string
|
|
|
|
): Promise<Partial<SymbolProfile>> {
|
|
|
|
return {
|
|
|
|
dataSource: this.getName()
|
|
|
|
};
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:20:02 +01:00
|
|
|
public async getDividends({
|
|
|
|
from,
|
|
|
|
granularity = 'day',
|
|
|
|
symbol,
|
|
|
|
to
|
|
|
|
}: {
|
|
|
|
from: Date;
|
|
|
|
granularity: Granularity;
|
|
|
|
symbol: string;
|
|
|
|
to: Date;
|
|
|
|
}) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
public async getHistorical(
|
2022-02-27 17:03:00 +01:00
|
|
|
aSymbol: string,
|
2021-04-13 21:53:58 +02:00
|
|
|
aGranularity: Granularity = 'day',
|
|
|
|
from: Date,
|
|
|
|
to: Date
|
|
|
|
): Promise<{
|
|
|
|
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse };
|
|
|
|
}> {
|
|
|
|
try {
|
2022-02-27 17:03:00 +01:00
|
|
|
const symbol = aSymbol;
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-09-11 11:14:55 +02:00
|
|
|
if (symbol === ghostfolioFearAndGreedIndexSymbol) {
|
2021-04-13 21:53:58 +02:00
|
|
|
const fgi = await this.getFearAndGreedIndex();
|
|
|
|
|
|
|
|
return {
|
2021-09-11 11:14:55 +02:00
|
|
|
[ghostfolioFearAndGreedIndexSymbol]: {
|
2021-07-28 16:11:19 +02:00
|
|
|
[format(getYesterday(), DATE_FORMAT)]: {
|
2021-04-13 21:53:58 +02:00
|
|
|
marketPrice: fgi.previousClose.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-06-11 13:40:15 +02:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(
|
|
|
|
`Could not get historical market data for ${aSymbol} (${this.getName()}) from ${format(
|
|
|
|
from,
|
|
|
|
DATE_FORMAT
|
|
|
|
)} to ${format(to, DATE_FORMAT)}: [${error.name}] ${error.message}`
|
|
|
|
);
|
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-10-20 15:41:40 -04:00
|
|
|
public getName(): DataSource {
|
2022-10-15 10:31:46 +02:00
|
|
|
return DataSource.RAPID_API;
|
2021-05-20 20:36:08 +02:00
|
|
|
}
|
|
|
|
|
2022-02-27 17:03:00 +01:00
|
|
|
public async getQuotes(
|
|
|
|
aSymbols: string[]
|
|
|
|
): Promise<{ [symbol: string]: IDataProviderResponse }> {
|
|
|
|
if (aSymbols.length <= 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const symbol = aSymbols[0];
|
|
|
|
|
|
|
|
if (symbol === ghostfolioFearAndGreedIndexSymbol) {
|
|
|
|
const fgi = await this.getFearAndGreedIndex();
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ghostfolioFearAndGreedIndexSymbol]: {
|
|
|
|
currency: undefined,
|
|
|
|
dataSource: this.getName(),
|
|
|
|
marketPrice: fgi.now.value,
|
2022-05-08 22:16:47 +02:00
|
|
|
marketState: 'open'
|
2022-02-27 17:03:00 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-10-15 10:31:46 +02:00
|
|
|
Logger.error(error, 'RapidApiService');
|
2022-02-27 17:03:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-01-08 18:19:25 +01:00
|
|
|
public async search(aQuery: string): Promise<{ items: LookupItem[] }> {
|
2021-10-20 15:41:40 -04:00
|
|
|
return { items: [] };
|
2021-05-20 20:36:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
private async getFearAndGreedIndex(): Promise<{
|
|
|
|
now: { value: number; valueText: string };
|
|
|
|
previousClose: { value: number; valueText: string };
|
|
|
|
oneWeekAgo: { value: number; valueText: string };
|
|
|
|
oneMonthAgo: { value: number; valueText: string };
|
|
|
|
oneYearAgo: { value: number; valueText: string };
|
|
|
|
}> {
|
|
|
|
try {
|
|
|
|
const get = bent(
|
|
|
|
`https://fear-and-greed-index.p.rapidapi.com/v1/fgi`,
|
|
|
|
'GET',
|
|
|
|
'json',
|
|
|
|
200,
|
|
|
|
{
|
|
|
|
useQueryString: true,
|
|
|
|
'x-rapidapi-host': 'fear-and-greed-index.p.rapidapi.com',
|
2022-10-15 10:31:46 +02:00
|
|
|
'x-rapidapi-key': this.configurationService.get('RAPID_API_API_KEY')
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const { fgi } = await get();
|
|
|
|
return fgi;
|
|
|
|
} catch (error) {
|
2022-10-15 10:31:46 +02:00
|
|
|
Logger.error(error, 'RapidApiService');
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|