feat: request & parse scoreinfo from html

This commit is contained in:
Xmader 2020-11-24 05:35:23 -05:00
parent 36a05aac75
commit 5d50c2337c
No known key found for this signature in database
GPG key ID: A20B97FB9EB730E4

View file

@ -1,4 +1,6 @@
import { getFetch } from './utils'
export abstract class ScoreInfo { export abstract class ScoreInfo {
private readonly IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP'; private readonly IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP';
private readonly RADIX = 20; private readonly RADIX = 20;
@ -58,6 +60,33 @@ export class ScoreInfoInPage extends ScoreInfo {
} }
} }
export class ScoreInfoHtml extends ScoreInfo {
private readonly ID_REG = /<meta property="al:ios:url" content="musescore:\/\/score\/(\d+)">/
private readonly TITLE_REG = /<meta property="og:title" content="(.*)">/
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<ScoreInfoHtml> {
const r = await _fetch(url)
if (!r.ok) return new ScoreInfoHtml('')
const html = await r.text()
return new ScoreInfoHtml(html)
}
}
export abstract class SheetInfo { export abstract class SheetInfo {
abstract pageCount: number; abstract pageCount: number;
abstract thumbnailUrl: string; abstract thumbnailUrl: string;