Skip to content

Commit

Permalink
fix: support command-line tool
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterNjeim committed Apr 16, 2024
1 parent 83b2036 commit 402b391
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
18 changes: 15 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,20 +603,32 @@ void (async () => {
switch (type) {
case "midi": {
fileExt = "mid";
const fileUrl = await getFileUrl(scoreinfo.id, "midi");
const fileUrl = await getFileUrl(
scoreinfo.id,
"midi",
argv.input
);
fileData = await fetchBuffer(fileUrl);
break;
}
case "mp3": {
fileExt = "mp3";
const fileUrl = await getFileUrl(scoreinfo.id, "mp3");
const fileUrl = await getFileUrl(
scoreinfo.id,
"mp3",
argv.input
);
fileData = await fetchBuffer(fileUrl);
break;
}
case "pdf": {
fileExt = "pdf";
fileData = Buffer.from(
await exportPDF(scoreinfo, scoreinfo.sheet)
await exportPDF(
scoreinfo,
scoreinfo.sheet,
argv.input
)
);
break;
}
Expand Down
33 changes: 21 additions & 12 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ import { auths } from "./file-magics";

export type FileType = "img" | "mp3" | "midi";

const getSuffix = async (): Promise<string> => {
const suffixElement =
(document.head.querySelector(
"link[href^='https://musescore.com/static/public/build/musescore_es6/20']"
) as HTMLLinkElement) ??
(document.head.querySelector(
"link[href^='https://musescore.com/static/public/build/musescore/20']"
) as HTMLLinkElement);
const suffixUrl = suffixElement?.href;
const getSuffix = async (scoreUrl: string): Promise<string> => {
let suffixUrl;
if (scoreUrl !== "") {
suffixUrl = (await (await fetch(scoreUrl)).text()).match(
'<link href="(https://musescore.com/static/public/build/musescore(?:_es6)?/20.+?.js)"'
)?.[1]!;
} else {
const suffixElement =
(document.head.querySelector(
"link[href^='https://musescore.com/static/public/build/musescore_es6/20']"
) as HTMLLinkElement) ??
(document.head.querySelector(
"link[href^='https://musescore.com/static/public/build/musescore/20']"
) as HTMLLinkElement);
suffixUrl = suffixElement?.href;
}
const suffixJs = await fetch(suffixUrl);
return (await suffixJs.text()).match(
'(?:.*)"(.+)"\\)\\.substr\\(0,4\\)'
Expand All @@ -29,9 +36,10 @@ const getApiUrl = (id: number, type: FileType, index: number): string => {
const getApiAuth = async (
id: number,
type: FileType,
index: number
index: number,
scoreUrl: string
): Promise<string> => {
const code = `${id}${type}${index}${await getSuffix()}`;
const code = `${id}${type}${index}${await getSuffix(scoreUrl)}`;
return md5(code).slice(0, 4);
};

Expand Down Expand Up @@ -137,11 +145,12 @@ const getApiAuthNetwork = async (
export const getFileUrl = async (
id: number,
type: FileType,
scoreUrl = "",
index = 0,
_fetch = getFetch()
): Promise<string> => {
const url = getApiUrl(id, type, index);
let auth = await getApiAuth(id, type, index);
let auth = await getApiAuth(id, type, index, scoreUrl);

let r = await _fetch(url, {
headers: {
Expand Down
5 changes: 3 additions & 2 deletions src/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const _exportPDFNode: _ExFn = async (imgURLs, imgType, dimensions) => {

export const exportPDF = async (
scoreinfo: ScoreInfo,
sheet: SheetInfo
sheet: SheetInfo,
scoreUrl = ""
): Promise<ArrayBuffer> => {
const imgType = sheet.imgType;
const pageCount = sheet.pageCount;
Expand All @@ -50,7 +51,7 @@ export const exportPDF = async (
return sheet.thumbnailUrl;
} else {
// obtain image urls using the API
return getFileUrl(scoreinfo.id, "img", i);
return getFileUrl(scoreinfo.id, "img", scoreUrl, i);
}
});
const sheetImgURLs = await Promise.all(rs);
Expand Down

0 comments on commit 402b391

Please sign in to comment.