File size: 1,830 Bytes
aa81892
 
 
 
 
4ed7e45
aa81892
 
4fc4471
 
63e5f15
 
 
129aefa
0171940
129aefa
 
63e5f15
 
0171940
63e5f15
 
 
 
 
 
 
aa81892
 
63e5f15
 
 
 
 
 
09c48f9
 
 
 
63e5f15
 
 
 
 
 
 
 
 
 
2aa2abc
 
 
 
63e5f15
 
 
 
 
 
4fc4471
63e5f15
4fc4471
092166c
63e5f15
 
aa81892
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
package api

import (
	"git.maid.zone/stuff/soundcloak/lib/cfg"
	"git.maid.zone/stuff/soundcloak/lib/sc"
	"github.com/gofiber/fiber/v3"
)

func Load(a *fiber.App) {
	r := a.Group("/_/api")
	r.Use("/v2", func(c fiber.Ctx) error {
		req := c.Request()
		p := req.URI().Path()[len("/_/api/v2"):]
		if string(p) == "/tracks" ||
			string(p) == "/search" ||
			string(p) == "/resolve" ||
			string(p) == "/mixed-selections" ||
			string(p) == "/charts/selections" ||
			(len(p) > len("/users/") &&
				(string(p[:len("/users/")]) == "/users/" || string(p[:len("/media/")]) == "/media/")) ||
			(len(p) > len("/tracks/") &&
				(string(p[:len("/tracks/")]) == "/tracks/" || string(p[:len("/search/")]) == "/search/")) ||
			(len(p) > len("/playlists/") &&
				string(p[:len("/playlists/")]) == "/playlists/") ||
			(len(p) > len("/featured_tracks/") &&
				string(p[:len("/featured_tracks/")]) == "/featured_tracks/") {
			goto great
		}
		return c.SendStatus(404)
	great:
		// api-v2 only has gzip, so we use only gzip
		gzip := req.Header.HasAcceptEncoding("gzip")
		req.Header.Reset()
		req.ResetBody()
		req.Header.SetUserAgent(cfg.UserAgent)
		req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
		if cfg.UseTokensInAPI {
			sc.Authorize(req)
		}

		req.URI().SetScheme("https")
		req.URI().SetHost("api-v2.soundcloud.com")
		if !req.URI().QueryArgs().Has("client_id") {
			req.URI().QueryArgs().Set("client_id", sc.ClientID)
		}
		req.URI().SetPathBytes(p)

		resp := c.Response()
		err := sc.DoWithRetry(sc.Httpc, req, resp)
		if err != nil {
			return err
		}

		// very big shame on you for not even implementing gzip compression
		if !gzip && string(resp.Header.ContentEncoding()) == "gzip" {
			p, err = resp.BodyGunzip()
			if err == nil {
				resp.SetBody(p)
			}
		}
		return err
	})

	// DEPRECATED
	legacy(r)
}