From 5d50c2337cc33d3b943425bdc158f68cac9ae2fe Mon Sep 17 00:00:00 2001 From: Xmader Date: Tue, 24 Nov 2020 05:35:23 -0500 Subject: [PATCH] feat: request & parse scoreinfo from html --- src/scoreinfo.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/scoreinfo.ts b/src/scoreinfo.ts index 3356d6a..a0849fd 100644 --- a/src/scoreinfo.ts +++ b/src/scoreinfo.ts @@ -1,4 +1,6 @@ +import { getFetch } from './utils' + export abstract class ScoreInfo { private readonly IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP'; private readonly RADIX = 20; @@ -58,6 +60,33 @@ export class ScoreInfoInPage extends ScoreInfo { } } +export class ScoreInfoHtml extends ScoreInfo { + private readonly ID_REG = // + private readonly TITLE_REG = // + + constructor (private html: string) { super() } + + get id (): number { + const m = this.html.match(this.ID_REG) + if (!m) return 0 + return +m[1] + } + + get title (): string { + const m = this.html.match(this.TITLE_REG) + if (!m) return '' + return m[1] + } + + static async request (url: string, _fetch = getFetch()): Promise { + const r = await _fetch(url) + if (!r.ok) return new ScoreInfoHtml('') + + const html = await r.text() + return new ScoreInfoHtml(html) + } +} + export abstract class SheetInfo { abstract pageCount: number; abstract thumbnailUrl: string;