From 368f2b100f1c20c6ad36394ce4d50840d1d52903 Mon Sep 17 00:00:00 2001 From: Chip Wasson Date: Thu, 5 Dec 2024 16:39:05 -0700 Subject: [PATCH] Hoard lichess stats --- src/config/configuration.ts | 5 +++++ src/hoarding/hoarding.service.ts | 35 +++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 6758aea..5e9e3fa 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -50,4 +50,9 @@ export default () => ({ mailgun: { hooliKey: process.env.MAILGUN_SEND_KEY_HOOLI ?? '', }, + thirdPartyServices: { + lichess: { + token: process.env.SVC_LICHESS_TOKEN ?? '', + }, + }, }); diff --git a/src/hoarding/hoarding.service.ts b/src/hoarding/hoarding.service.ts index ee3a5f9..8f2c90f 100644 --- a/src/hoarding/hoarding.service.ts +++ b/src/hoarding/hoarding.service.ts @@ -7,7 +7,10 @@ import { MinioService } from 'src/minio/minio.service'; @Injectable() export class HoardingService { private readonly logger: Logger = new Logger(HoardingService.name); - constructor(public readonly minioService: MinioService) { + constructor( + public readonly minioService: MinioService, + private readonly configService: ConfigService, + ) { this.hoardChessStats(); } @@ -23,6 +26,36 @@ export class HoardingService { } catch (e) { this.logger.error(e); } + try { + await this.hoardLichessStats('sentientcrouton'); + } catch (e) { + this.logger.error(e); + } + } + + async hoardLichessStats(user: string) { + if (!this.configService.get('thirdPartyServices.lichess.token')) { + this.logger.error('No lichess token found'); + return; + } + const token = this.configService.get('thirdPartyServices.lichess.token'); + this.logger.log(`Hoarding lichess stats for ${user}`); + const lichessState = (id: string) => `https://lichess.org/api/user/${id}`; + const stats = await axios.get(lichessState(user), { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + await this.minioService.uploadBuffer( + this.minioService.defaultBucketName, + `hoarding/lichess-stats/${user}/${new Date().getTime()}-${user}.json`, + Buffer.from( + JSON.stringify({ + time: new Date().getTime(), + data: stats.data, + }), + ), + ); } async hoardUserChessStats(user: string) {