Thomas
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
16 additions and
6 deletions
-
apps/api/src/services/data-provider/ghostfolio-scraper-api/ghostfolio-scraper-api.service.ts
|
|
@ -15,6 +15,8 @@ import { PrismaService } from '../../prisma.service'; |
|
|
|
|
|
|
|
@Injectable() |
|
|
|
export class GhostfolioScraperApiService implements DataProviderInterface { |
|
|
|
private static NUMERIC_REGEXP = /[-]{0,1}[\d]*[.,]{0,1}[\d]+/g; |
|
|
|
|
|
|
|
public constructor(private prisma: PrismaService) {} |
|
|
|
|
|
|
|
public async get( |
|
|
@ -75,12 +77,9 @@ export class GhostfolioScraperApiService implements DataProviderInterface { |
|
|
|
const html = await get(); |
|
|
|
const $ = cheerio.load(html); |
|
|
|
|
|
|
|
const string = $(scraperConfig?.selector) |
|
|
|
.text() |
|
|
|
.replace('CHF', '') |
|
|
|
.trim(); |
|
|
|
|
|
|
|
const value = parseFloat(string); |
|
|
|
const value = this.extractNumberFromString( |
|
|
|
$(scraperConfig?.selector).text() |
|
|
|
); |
|
|
|
|
|
|
|
return { |
|
|
|
[symbol]: { |
|
|
@ -96,6 +95,17 @@ export class GhostfolioScraperApiService implements DataProviderInterface { |
|
|
|
return {}; |
|
|
|
} |
|
|
|
|
|
|
|
private extractNumberFromString(aString: string): number { |
|
|
|
try { |
|
|
|
const [numberString] = aString.match( |
|
|
|
GhostfolioScraperApiService.NUMERIC_REGEXP |
|
|
|
); |
|
|
|
return parseFloat(numberString.trim()); |
|
|
|
} catch { |
|
|
|
return undefined; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private async getScraperConfig(aSymbol: string) { |
|
|
|
try { |
|
|
|
const { |
|
|
|