Files
aleesa-webapp-go/internal/obutts/obutts.go
eleksir f2fc168abe Replace prazdnikisegodnyaru with celebratoday, small underhood fixes
- Replace prazdnikisegodnyaru with celebratoday as first is banned in Russia
- Update .golangci.yml rule set
- Megatons of fixes for linter
- Update README.md
2026-02-10 20:05:13 +03:00

55 lines
1.2 KiB
Go

// Package obutts implements api.obutts.ru service client.
package obutts
import (
"aleesa-webapp-go/internal/config"
"context"
"fmt"
"math/rand/v2"
"github.com/carlmjohnson/requests"
)
type (
// Obutts структура, возвращаемая на запрос в obutts.ru.
Obutts struct { //nolint: govet // Идите в пень, мне так удобнее.
ID int `json:"id,omitempty"`
Author string `json:"author,omitempty"`
Rank int `json:"rank,omitempty"`
Model string `json:"model,omitempty"`
Preview string `json:"preview"`
}
)
// APIClient клиент сервиса obutts.ru.
func APIClient(cfg *config.MyConfig) (string, error) {
var (
ctx = context.Background()
userAgent = cfg.UserAgents[rand.IntN(len(cfg.UserAgents))]
url = "http://api.obutts.ru/butts/0/1/random"
butts []Obutts
)
err := requests.
URL(url).
UserAgent(userAgent).
ToJSON(&butts).
Fetch(ctx)
if err != nil {
return "", fmt.Errorf("unable to GET %s: %w", url, err)
}
if len(butts) == 0 {
err = fmt.Errorf("empty json array returned from %s", url)
return "", err
}
answer := "http://media.obutts.ru/" + butts[0].Preview
return answer, nil
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */