File size: 4,867 Bytes
de4b571
11fcc5a
de4b571
 
 
 
 
 
 
 
11fcc5a
 
de4b571
11fcc5a
 
 
 
 
de4b571
 
 
11fcc5a
de4b571
 
 
 
 
11fcc5a
 
de4b571
 
 
 
 
 
 
 
 
 
 
 
 
11fcc5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de4b571
11fcc5a
de4b571
 
 
11fcc5a
 
 
 
 
 
 
 
de4b571
 
11fcc5a
 
 
 
 
de4b571
 
 
 
 
11fcc5a
 
 
de4b571
11fcc5a
de4b571
 
11fcc5a
 
 
 
 
de4b571
 
 
11fcc5a
de4b571
 
 
11fcc5a
de4b571
 
 
 
11fcc5a
 
 
de4b571
 
 
 
 
 
 
 
 
 
 
11fcc5a
 
 
de4b571
11fcc5a
 
 
de4b571
11fcc5a
 
 
 
 
 
 
 
 
 
 
 
de4b571
 
11fcc5a
 
 
 
 
 
de4b571
11fcc5a
 
 
de4b571
 
 
11fcc5a
 
de4b571
 
 
11fcc5a
de4b571
 
11fcc5a
 
de4b571
 
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
import { env } from "../../config.js";
import { resolveRedirectingURL } from "../url.js";

const cachedID = {
    version: '',
    id: ''
}

async function findClientID() {
    try {
        const sc = await fetch('https://soundcloud.com/').then(r => r.text()).catch(() => {});
        const scVersion = String(sc.match(/<script>window\.__sc_version="[0-9]{10}"<\/script>/)[0].match(/[0-9]{10}/));

        if (cachedID.version === scVersion) {
            return cachedID.id;
        }

        const scripts = sc.matchAll(/<script.+src="(.+)">/g);

        let clientid;
        for (let script of scripts) {
            const url = script[1];

            if (!url?.startsWith('https://a-v2.sndcdn.com/')) {
                return;
            }

            const scrf = await fetch(url).then(r => r.text()).catch(() => {});
            const id = scrf.match(/\("client_id=[A-Za-z0-9]{32}"\)/);

            if (id && typeof id[0] === 'string') {
                clientid = id[0].match(/[A-Za-z0-9]{32}/)[0];
                break;
            }
        }
        cachedID.version = scVersion;
        cachedID.id = clientid;

        return clientid;
    } catch {}
}

const findBestForPreset = (transcodings, preset) => {
    let inferior;
    for (const entry of transcodings) {
        const protocol = entry?.format?.protocol;

        if (entry.snipped || protocol?.includes('encrypted')) {
            continue;
        }

        if (entry?.preset?.startsWith(`${preset}_`)) {
            if (protocol === 'progressive') {
                return entry;
            }

            inferior = entry;
        }
    }

    return inferior;
}

export default async function(obj) {
    const clientId = await findClientID();
    if (!clientId) return { error: "fetch.fail" };

    let link;

    if (obj.shortLink) {
        obj = {
            ...obj,
            ...await resolveRedirectingURL(
                `https://on.soundcloud.com/${obj.shortLink}`
            )
        }
    }

    if (obj.author && obj.song) {
        link = `https://soundcloud.com/${obj.author}/${obj.song}`;
        if (obj.accessKey) {
            link += `/s-${obj.accessKey}`;
        }
    }

    if (!link && obj.shortLink) return { error: "fetch.short_link" };
    if (!link) return { error: "link.unsupported" };

    const resolveURL = new URL("https://api-v2.soundcloud.com/resolve");
    resolveURL.searchParams.set("url", link);
    resolveURL.searchParams.set("client_id", clientId);

    const json = await fetch(resolveURL).then(r => r.json()).catch(() => {});
    if (!json) return { error: "fetch.fail" };

    if (json.duration > env.durationLimit * 1000) {
        return { error: "content.too_long" };
    }

    if (json.policy === "BLOCK") {
        return { error: "content.region" };
    }

    if (json.policy === "SNIP") {
        return { error: "content.paid" };
    }

    if (!json.media?.transcodings || !json.media?.transcodings.length === 0) {
        return { error: "fetch.empty" };
    }

    let bestAudio = "opus",
        selectedStream = findBestForPreset(json.media.transcodings, "opus");

    const mp3Media = findBestForPreset(json.media.transcodings, "mp3");

    // use mp3 if present if user prefers it or if opus isn't available
    if (mp3Media && (obj.format === "mp3" || !selectedStream)) {
        selectedStream = mp3Media;
        bestAudio = "mp3"
    }

    if (!selectedStream) {
        return { error: "fetch.empty" };
    }

    const fileUrl = new URL(selectedStream.url);
    fileUrl.searchParams.set("client_id", clientId);
    fileUrl.searchParams.set("track_authorization", json.track_authorization);

    const file = await fetch(fileUrl)
                     .then(async r => new URL((await r.json()).url))
                     .catch(() => {});

    if (!file) return { error: "fetch.empty" };

    const artist = json.user?.username?.trim();
    const fileMetadata = {
        title: json.title?.trim(),
        album: json.publisher_metadata?.album_title?.trim(),
        artist,
        album_artist: artist,
        composer: json.publisher_metadata?.writer_composer?.trim(),
        genre: json.genre?.trim(),
        date: json.display_date?.trim().slice(0, 10),
        copyright: json.license?.trim(),
    }

    let cover;
    if (json.artwork_url) {
        const coverUrl = json.artwork_url.replace(/-large/, "-t1080x1080");
        const testCover = await fetch(coverUrl)
            .then(r => r.status === 200)
            .catch(() => {});

        if (testCover) {
            cover = coverUrl;
        }
    }

    return {
        urls: file.toString(),
        cover,
        filenameAttributes: {
            service: "soundcloud",
            id: json.id,
            ...fileMetadata
        },
        bestAudio,
        fileMetadata,
        isHLS: file.pathname.endsWith('.m3u8'),
    }
}