API and many small fixes

This commit is contained in:
Laptop
2025-06-20 21:52:25 +03:00
parent a0551d742b
commit 5e96cee22e
10 changed files with 161 additions and 62 deletions

58
lib/api/init.go Normal file
View File

@@ -0,0 +1,58 @@
package api
import (
"log"
"net/url"
"git.maid.zone/stuff/soundcloak/lib/cfg"
"git.maid.zone/stuff/soundcloak/lib/preferences"
"git.maid.zone/stuff/soundcloak/lib/sc"
"github.com/gofiber/fiber/v3"
)
func Load(r *fiber.App) {
r.Get("/_/api/search", func(c fiber.Ctx) error {
prefs, err := preferences.Get(c)
if err != nil {
return err
}
q := cfg.B2s(c.RequestCtx().QueryArgs().Peek("q"))
t := cfg.B2s(c.RequestCtx().QueryArgs().Peek("type"))
args := cfg.B2s(c.RequestCtx().QueryArgs().Peek("pagination"))
if args == "" {
args = "?q=" + url.QueryEscape(q)
}
switch t {
case "tracks":
p, err := sc.SearchTracks("", prefs, args)
if err != nil {
log.Printf("[API] error getting tracks for %s: %s\n", q, err)
return err
}
return c.JSON(p)
case "users":
p, err := sc.SearchUsers("", prefs, args)
if err != nil {
log.Printf("[API] error getting users for %s: %s\n", q, err)
return err
}
return c.JSON(p)
case "playlists":
p, err := sc.SearchPlaylists("", prefs, args)
if err != nil {
log.Printf("[API] error getting playlists for %s: %s\n", q, err)
return err
}
return c.JSON(p)
}
return c.SendStatus(404)
})
}

View File

@@ -69,6 +69,9 @@ var UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (K
// time-to-live for dns cache
var DNSCacheTTL = 60 * time.Minute
// enab;e api
var EnableAPI = false
// // // some webserver configuration, put here to make it easier to configure what you need // // //
// more info can be found here: https://docs.gofiber.io/api/fiber#config
@@ -387,6 +390,11 @@ func fromEnv() error {
DNSCacheTTL = time.Duration(num) * time.Second
}
env = os.Getenv("ENABLE_API")
if env != "" {
EnableAPI = boolean(env)
}
env = os.Getenv("NETWORK")
if env != "" {
Network = env
@@ -480,6 +488,7 @@ func init() {
PlaylistCacheCleanDelay *time.Duration
UserAgent *string
DNSCacheTTL *time.Duration
EnableAPI *bool
Network *string
Addr *string
UnixSocketPerms *string
@@ -547,6 +556,9 @@ func init() {
if config.DNSCacheTTL != nil {
DNSCacheTTL = *config.DNSCacheTTL * time.Second
}
if config.EnableAPI != nil {
EnableAPI = *config.EnableAPI
}
if config.Network != nil {
Network = *config.Network
}

View File

@@ -54,7 +54,6 @@ func Load(r *fiber.App) {
req.SetURI(parsed)
req.Header.SetUserAgent(cfg.UserAgent)
//req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd") images not big enough to be compressed
resp := fasthttp.AcquireResponse()
//defer fasthttp.ReleaseResponse(resp) moved to proxyreader!!!

View File

@@ -42,7 +42,6 @@ func Load(a *fiber.App) {
req.SetURI(parsed)
req.Header.SetUserAgent(cfg.UserAgent)
req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
resp := fasthttp.AcquireResponse()
//defer fasthttp.ReleaseResponse(resp)
@@ -81,7 +80,6 @@ func Load(a *fiber.App) {
req.SetURI(parsed)
req.Header.SetUserAgent(cfg.UserAgent)
req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
resp := fasthttp.AcquireResponse()
@@ -119,7 +117,6 @@ func Load(a *fiber.App) {
req.SetURI(parsed)
req.Header.SetUserAgent(cfg.UserAgent)
req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
@@ -170,7 +167,6 @@ func Load(a *fiber.App) {
req.SetURI(parsed)
req.Header.SetUserAgent(cfg.UserAgent)
req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
@@ -180,12 +176,7 @@ func Load(a *fiber.App) {
return err
}
data, err := resp.BodyUncompressed()
if err != nil {
data = resp.Body()
}
var sp = bytes.Split(data, newline)
var sp = bytes.Split(resp.Body(), newline)
for i, l := range sp {
if len(l) == 0 {
continue

View File

@@ -48,14 +48,19 @@ func Load(r *fiber.App) {
}
var isDownload = string(c.RequestCtx().QueryArgs().Peek("metadata")) == "true"
var quality *string
if isDownload {
quality = p.DownloadAudio
var forcedQuality = c.RequestCtx().QueryArgs().Peek("audio")
var quality string
if len(forcedQuality) != 0 {
quality = cfg.B2s(forcedQuality)
} else {
quality = p.RestreamAudio
if isDownload {
quality = *p.DownloadAudio
} else {
quality = *p.RestreamAudio
}
}
tr, audio := t.Media.SelectCompatible(*quality, true)
tr, audio := t.Media.SelectCompatible(quality, true)
if tr == nil {
return fiber.ErrExpectationFailed
}
@@ -92,7 +97,6 @@ func Load(r *fiber.App) {
if t.Artwork != "" {
r.req.SetRequestURI(t.Artwork)
r.req.Header.Del("Accept-Encoding")
err := sc.DoWithRetry(misc.ImageClient, r.req, r.resp)
if err != nil {
@@ -100,7 +104,6 @@ func Load(r *fiber.App) {
}
tag.AddAttachedPicture(id3v2.PictureFrame{MimeType: cfg.B2s(r.req.Header.ContentType()), Picture: r.req.Body(), PictureType: id3v2.PTFrontCover, Encoding: id3v2.EncodingUTF8})
r.req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
}
var col collector
@@ -115,17 +118,13 @@ func Load(r *fiber.App) {
req.SetRequestURI(u)
req.Header.SetUserAgent(cfg.UserAgent)
req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
err := sc.DoWithRetry(misc.HlsClient, req, resp)
if err != nil {
return err
}
data, err := resp.BodyUncompressed()
if err != nil {
data = resp.Body()
}
data := resp.Body()
res := make([]byte, 0, 1024*1024*1)
for _, s := range bytes.Split(data, []byte{'\n'}) {
@@ -139,10 +138,7 @@ func Load(r *fiber.App) {
return err
}
data, err = resp.BodyUncompressed()
if err != nil {
data = resp.Body()
}
data = resp.Body()
res = append(res, data...)
}
@@ -161,7 +157,6 @@ func Load(r *fiber.App) {
if t.Artwork != "" {
req.SetRequestURI(t.Artwork)
req.Header.Del("Accept-Encoding")
err := sc.DoWithRetry(misc.ImageClient, req, resp)
if err != nil {
@@ -207,7 +202,6 @@ func Load(r *fiber.App) {
if t.Artwork != "" {
r.req.SetRequestURI(t.Artwork)
r.req.Header.Del("Accept-Encoding")
err := sc.DoWithRetry(misc.ImageClient, r.req, r.resp)
if err != nil {
@@ -221,7 +215,6 @@ func Load(r *fiber.App) {
}
tag.SetCoverArt(&parsed)
r.req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
}
var col collector

View File

@@ -66,7 +66,6 @@ func (r *reader) Setup(url string, aac bool, duration *uint32) error {
r.req.SetRequestURI(url)
r.req.Header.SetUserAgent(cfg.UserAgent)
r.req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
if aac {
r.client = misc.HlsAacClient
@@ -80,11 +79,6 @@ func (r *reader) Setup(url string, aac bool, duration *uint32) error {
return err
}
data, err := r.resp.BodyUncompressed()
if err != nil {
data = r.resp.Body()
}
if r.parts == nil {
misc.Log("make() r.parts")
r.parts = make([][]byte, 0, defaultPartsCapacity)
@@ -93,7 +87,7 @@ func (r *reader) Setup(url string, aac bool, duration *uint32) error {
}
if aac {
// clone needed to mitigate memory skill issues here
for _, s := range bytes.Split(data, []byte{'\n'}) {
for _, s := range bytes.Split(r.resp.Body(), []byte{'\n'}) {
if len(s) == 0 {
continue
}
@@ -108,7 +102,7 @@ func (r *reader) Setup(url string, aac bool, duration *uint32) error {
r.parts = append(r.parts, clone(s))
}
} else {
for _, s := range bytes.Split(data, []byte{'\n'}) {
for _, s := range bytes.Split(r.resp.Body(), []byte{'\n'}) {
if len(s) == 0 || s[0] == '#' {
continue
}
@@ -169,11 +163,7 @@ func (r *reader) Read(buf []byte) (n int, err error) {
return
}
data, err := r.resp.BodyUncompressed()
if err != nil {
data = r.resp.Body()
}
data := r.resp.Body()
if r.index == 0 && r.duration != nil {
fixDuration(data, r.duration) // I'm guessing that mvhd will always be in first part
}