aboutsummaryrefslogtreecommitdiff
path: root/src/scoreinfo.ts
blob: 9a537e9901c24f6e37c15be7ae66fffae5cdcc56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { getFetch, escapeFilename } from "./utils";
import i18nextInit, { i18next } from "./i18n/index";

(async () => {
    await i18nextInit;
})();

export abstract class ScoreInfo {
    abstract id: number;
    abstract title: string;

    public store = new Map<symbol, any>();

    get fileName(): string {
        return escapeFilename(this.title);
    }
}

export class ScoreInfoObj extends ScoreInfo {
    constructor(public id: number = 0, public title: string = "") {
        super();
    }
}

export class ScoreInfoInPage extends ScoreInfo {
    constructor(private document: Document) {
        super();
    }

    get id(): number {
        const el = this.document.querySelector(
            "meta[property='al:ios:url']"
        ) as HTMLMetaElement;
        const m = el.content.match(/(\d+)$/) as RegExpMatchArray;
        return +m[1];
    }

    get title(): string {
        const el = this.document.querySelector(
            "meta[property='og:title']"
        ) as HTMLMetaElement;
        return el.content;
    }

    get baseUrl(): string {
        const el = this.document.querySelector(
            "meta[property='og:image']"
        ) as HTMLMetaElement;
        const m = el.content.match(/^(.+\/)score_/) as RegExpMatchArray;
        return m[1];
    }
}

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="(.*)">/;
    private readonly BASEURL_REG =
        /<meta property="og:image" content="(.+\/)score_.*">/;

    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];
    }

    get baseUrl(): string {
        const m = this.html.match(this.BASEURL_REG);
        if (!m) return "";
        return m[1];
    }

    get sheet(): SheetInfo {
        return new SheetInfoHtml(this.html);
    }

    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 type Dimensions = { width: number; height: number };

export abstract class SheetInfo {
    abstract pageCount: number;

    /** url to the image of the first page */
    abstract thumbnailUrl: string;

    abstract dimensions: Dimensions;

    get imgType(): "svg" | "png" {
        const thumbnail = this.thumbnailUrl;
        const imgtype = thumbnail.match(/score_0\.(\w+)/)![1];
        return imgtype as "svg" | "png";
    }
}

export class SheetInfoInPage extends SheetInfo {
    constructor(private document: Document) {
        super();
    }

    private get sheet0Img(): HTMLImageElement | null {
        return this.document.querySelector("img[src*=score_]");
    }

    get pageCount(): number {
        const sheet0Div = this.sheet0Img?.parentElement;
        if (!sheet0Div) {
            throw new Error(i18next.t("no_sheet_images_error"));
        }
        return this.document.getElementsByClassName(sheet0Div.className).length;
    }

    get thumbnailUrl(): string {
        const el =
            this.document.querySelector<HTMLLinkElement>("link[as=image]");
        const url = (el?.href || this.sheet0Img?.src) as string;
        return url.split("@")[0];
    }

    get dimensions(): Dimensions {
        const { naturalWidth: width, naturalHeight: height } = this
            .sheet0Img as HTMLImageElement;
        return { width, height };
    }
}

export class SheetInfoHtml extends SheetInfo {
    private readonly PAGE_COUNT_REG = /pages(?:&quot;|"):(\d+)/;
    private readonly THUMBNAIL_REG =
        /<link (?:.*) href="(.*)" rel="preload" as="image"/;

    private readonly DIMENSIONS_REG =
        /dimensions(?:&quot;|"):(?:&quot;|")(\d+)x(\d+)(?:&quot;|"),/;

    constructor(private html: string) {
        super();
    }

    get pageCount(): number {
        const m = this.html.match(this.PAGE_COUNT_REG);
        if (!m) return NaN;
        return +m[1];
    }

    get thumbnailUrl(): string {
        const m = this.html.match(this.THUMBNAIL_REG);
        if (!m) return "";
        return m[1].split("@")[0];
    }

    get dimensions(): Dimensions {
        const m = this.html.match(this.DIMENSIONS_REG);
        if (!m) return { width: NaN, height: NaN };
        return { width: +m[1], height: +m[2] };
    }
}