fix: get the actual id of s.musescore.com urls

using pregenerated sid2id map
This commit is contained in:
Xmader 2020-12-16 22:11:26 -05:00
parent 8e6992ab27
commit 2df57606e3
No known key found for this signature in database
GPG key ID: A20B97FB9EB730E4

View file

@ -1,5 +1,6 @@
import { getFetch, escapeFilename } from './utils' import { getFetch, escapeFilename, assertRes } from './utils'
import { getMainCid } from './mscz'
export abstract class ScoreInfo { export abstract class ScoreInfo {
private readonly RADIX = 20; private readonly RADIX = 20;
@ -117,18 +118,24 @@ export const getActualId = async (scoreinfo: ScoreInfoInPage | ScoreInfoHtml, _f
return scoreinfo.id return scoreinfo.id
} }
const jsonPUrl = new URL(`${scoreinfo.baseUrl}space.jsonp`) const mainCid = await getMainCid(scoreinfo, _fetch)
jsonPUrl.hostname = 's.musescore.com' const ref = `${mainCid}/sid2id/${scoreinfo.id}`
const url = `https://ipfs.infura.io:5001/api/v0/dag/get?arg=${ref}`
const r = await _fetch(jsonPUrl.href) const r0 = await _fetch(url)
const text = await r.text() if (r0.status !== 500) {
assertRes(r0)
const m = text.match(/^jsonp(\d+)/) as RegExpMatchArray }
const id = +m[1] const res: { Message: string } | number = await r0.json()
if (typeof res !== 'number') {
// read further error msg
throw new Error(res.Message)
}
// assign the actual id back to scoreinfo
Object.defineProperty(scoreinfo, 'id', { Object.defineProperty(scoreinfo, 'id', {
get () { return id }, get () { return res },
}) })
return id return res
} }