feat: request & parse scoreinfo from html
This commit is contained in:
		
							parent
							
								
									36a05aac75
								
							
						
					
					
						commit
						5d50c2337c
					
				
					 1 changed files with 29 additions and 0 deletions
				
			
		|  | @ -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 = /<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 { | ||||
|   abstract pageCount: number; | ||||
|   abstract thumbnailUrl: string; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue