-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (69 loc) · 2.31 KB
/
Copy pathindex.js
File metadata and controls
82 lines (69 loc) · 2.31 KB
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
import express from "express";
import fetch from "node-fetch";
import fs from "fs";
import path from "path";
const app = express();
const PORT = process.env.PORT || 3000;
const clientIdStatic = "KKzJxmw11tYpCs6T24P4uUYhqmjalG6M";
const clientIdFile = path.join(process.cwd(), "soundcloud_client_id.txt");
const apiBase = "https://api-v2.soundcloud.com/";
const baseUrl = "https://soundcloud.com/";
function getClientId() {
if (fs.existsSync(clientIdFile)) {
const id = fs.readFileSync(clientIdFile, "utf8").trim();
if (/^[a-zA-Z0-9]{32}$/.test(id)) return id;
}
return clientIdStatic;
}
async function makeRequest(url) {
const res = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0" } });
const text = await res.text();
try {
return JSON.parse(text);
} catch {
return null;
}
}
async function callApi(endpoint, params = {}) {
const client_id = getClientId();
const query = new URLSearchParams({ ...params, client_id }).toString();
const fullUrl = `${apiBase}${endpoint}?${query}`;
return await makeRequest(fullUrl);
}
app.get("/", (req, res) => {
res.send(`
<h2>🎵 SoundCloud API Proxy</h2>
<p>Use like this:</p>
<pre>/track?url=https://soundcloud.com/user/track</pre>
`);
});
app.get("/beta", async (req, res) => {
const trackUrl = req.query.url;
if (!trackUrl) return res.status(400).json({ error: "Missing URL" });
const resolveUrl = `resolve?url=${encodeURIComponent(trackUrl)}`;
const data = await callApi(resolveUrl);
if (!data?.id) return res.status(404).json({ error: "Track not found" });
let downloadUrl = null;
if (data.media?.transcodings) {
for (const t of data.media.transcodings) {
if (t.format?.protocol === "progressive") {
const stream = await makeRequest(`${t.url}?client_id=${getClientId()}`);
if (stream?.url) {
downloadUrl = stream.url;
break;
}
}
}
}
const thumb = (data.artwork_url || data.user?.avatar_url || "").replace("-large", "-t500x500");
res.json({
title: data.title,
artist: data.user?.username,
url: data.permalink_url,
thumbnail: thumb,
download: downloadUrl,
duration: data.duration / 1000,
developer: { name: "Ehsan Fazli", username: "@abj0o" }
});
});
app.listen(PORT, () => console.log(`🚀 SoundCloud Proxy running on port ${PORT}`));