Files
aleesa-webapp-go/internal/bunicomic/bunicomic.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

88 lines
1.9 KiB
Go

// Package bunicomic implements client for bunicomic.com service.
package bunicomic
import (
"aleesa-webapp-go/internal/config"
"context"
"errors"
"fmt"
"math/rand/v2"
"regexp"
"strings"
"github.com/carlmjohnson/requests"
"golang.org/x/net/html"
)
// APIClient клиент сервиса bunicomic.com.
func APIClient(cfg *config.MyConfig) (string, error) {
var (
ctx = context.Background()
body string
userAgent = cfg.UserAgents[rand.IntN(len(cfg.UserAgents))]
url = "https://www.bunicomic.com/?random&nocache=1"
)
err := requests.
URL(url).
UserAgent(userAgent).
ToString(&body).
Fetch(ctx)
if err != nil {
return "", fmt.Errorf("unable to GET %s: %w", url, err)
}
// Парсим body ответа от сервера.
doc, err := html.Parse(strings.NewReader(body))
if err != nil {
return "", err
}
var (
metaTags []string
meta func(*html.Node)
)
meta = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "meta" {
for _, meta := range n.Attr {
if meta.Key == "content" {
// Adds a new link entry when the attribute matches.
metaTags = append(metaTags, meta.Val)
}
}
}
// Traverses the HTML of the webpage from the first child node.
for c := n.FirstChild; c != nil; c = c.NextSibling {
meta(c)
}
}
meta(doc)
// Возвращаем ссылку на картинку buni comic.
if len(metaTags) != 0 {
// А теперь среди полученнного мусора найдём ссылку на урл.
for _, buni := range metaTags {
match, err := regexp.MatchString(`wp-content/uploads`, buni) //nolint: staticcheck
if err != nil {
return "", err
}
if match {
return buni, nil
}
}
}
// Картинки не выпарсилось, это точно ошибка.
err = errors.New("unable to get link to buni comic strip image")
return "", err
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */