diff options
| author | Christian Cleberg <[email protected]> | 2026-07-14 20:23:47 -0500 |
|---|---|---|
| committer | Christian Cleberg <[email protected]> | 2026-07-14 20:23:47 -0500 |
| commit | 5756a37bc14bab81cef2e305c504a396995dcf18 (patch) | |
| tree | e8d99d6cc87a17e83f90be8c30ee3a73003f04be /app/api.go | |
| parent | f4afe364c1885b55d3bd92d1aa2f00c4bb7c8ee2 (diff) | |
| parent | 369fd17e696699b9b1aa1ac8293e0122c7e3a41e (diff) | |
| download | skunky-art-5756a37bc14bab81cef2e305c504a396995dcf18.tar.gz skunky-art-5756a37bc14bab81cef2e305c504a396995dcf18.tar.bz2 skunky-art-5756a37bc14bab81cef2e305c504a396995dcf18.zip | |
Merge dev: devianter v0.3.1 migration, DA throttle, fixes
Migrate to github.com/zerolabsco/devianter v0.3.1 and adapt to its
reordered return values, throttle outbound DeviantArt requests, harden
the random-art retry loop, and fix the darwin build.
Diffstat (limited to 'app/api.go')
| -rwxr-xr-x | app/api.go | 24 |
1 files changed, 16 insertions, 8 deletions
@@ -3,9 +3,10 @@ package app import ( "encoding/json" "math/rand" + "strconv" "strings" - "git.macaw.me/skunky/devianter" + "github.com/zerolabsco/devianter" ) type API struct { @@ -58,25 +59,32 @@ func (a API) sendMedia(d *devianter.Deviation) { // TODO: сделать фильтры func (a API) Random() { - for attempt := 1; ; { - if attempt > 3 { - a.Error("Sorry, butt NSFW on this are disabled, and the instance failed to find a random art without NSFW", 500) - } + // Bounded retries: the loop used to be unbounded, and the DeviantArt-error + // path never incremented attempt, so a single request could spin forever + // hammering the API (and get this instance's egress IP banned). + const maxAttempts = 3 - s, err, daErr := devianter.PerformSearch(string(rand.Intn(999)), rand.Intn(30), 'a') + for attempt := 0; attempt < maxAttempts; attempt++ { + // strconv.Itoa, not string(): string(65) is "A", not "65". + s, daErr, err := devianter.PerformSearch(strconv.Itoa(rand.Intn(999)), rand.Intn(30), 'a') try(err) if daErr.RAW != nil { continue } - deviation := &s.Results[rand.Intn(len(s.Results))] + // rand.Intn panics on 0, so an empty result set must be skipped. + if len(s.Results) == 0 { + continue + } + deviation := &s.Results[rand.Intn(len(s.Results))] if deviation.NSFW && !CFG.Nsfw { - attempt++ continue } a.sendMedia(deviation) return } + + a.Error("Sorry, butt NSFW on this are disabled, and the instance failed to find a random art without NSFW", 500) } |
