✨ Expand links API
This commit is contained in:
12
pkg/internal/models/link.go
Normal file
12
pkg/internal/models/link.go
Normal file
@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
type LinkMeta struct {
|
||||
BaseModel
|
||||
|
||||
Icon string `json:"icon"`
|
||||
URL string `json:"url"`
|
||||
Image *string `json:"image"`
|
||||
Title *string `json:"title"`
|
||||
Video *string `json:"video"`
|
||||
Description *string `json:"description"`
|
||||
}
|
@ -19,6 +19,8 @@ func MapAPIs(app *fiber.App) {
|
||||
|
||||
api := app.Group("/api").Name("API")
|
||||
{
|
||||
api.Get("/links/:target", getLinkMeta)
|
||||
|
||||
api.Use(func(c *fiber.Ctx) error {
|
||||
if err := exts.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
|
18
pkg/internal/server/api/link_expander.go
Normal file
18
pkg/internal/server/api/link_expander.go
Normal file
@ -0,0 +1,18 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func getLinkMeta(c *fiber.Ctx) error {
|
||||
targetEncoded := c.Params("target")
|
||||
targetRaw, _ := base64.StdEncoding.DecodeString(targetEncoded)
|
||||
|
||||
if meta, err := services.LinkExpand(string(targetRaw)); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(meta)
|
||||
}
|
||||
}
|
82
pkg/internal/services/link_expander.go
Normal file
82
pkg/internal/services/link_expander.go
Normal file
@ -0,0 +1,82 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/internal/models"
|
||||
"github.com/gocolly/colly"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func LinkExpand(target string) (*models.LinkMeta, error) {
|
||||
c := colly.NewCollector(
|
||||
colly.UserAgent("SolarBot/1.0"),
|
||||
colly.MaxDepth(3),
|
||||
)
|
||||
|
||||
c.WithTransport(&http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 60 * time.Second,
|
||||
KeepAlive: 360 * time.Second,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
})
|
||||
|
||||
meta := &models.LinkMeta{
|
||||
URL: target,
|
||||
}
|
||||
|
||||
c.OnHTML("title", func(e *colly.HTMLElement) {
|
||||
meta.Title = &e.Text
|
||||
})
|
||||
c.OnHTML("meta[name]", func(e *colly.HTMLElement) {
|
||||
switch e.Attr("name") {
|
||||
case "description":
|
||||
meta.Description = lo.ToPtr(e.Attr("content"))
|
||||
}
|
||||
})
|
||||
c.OnHTML("meta[property]", func(e *colly.HTMLElement) {
|
||||
switch e.Attr("property") {
|
||||
case "og:title":
|
||||
meta.Title = lo.ToPtr(e.Attr("content"))
|
||||
case "og:description":
|
||||
meta.Description = lo.ToPtr(e.Attr("content"))
|
||||
case "og:image":
|
||||
meta.Image = lo.ToPtr(e.Attr("content"))
|
||||
case "og:video":
|
||||
meta.Video = lo.ToPtr(e.Attr("content"))
|
||||
}
|
||||
})
|
||||
c.OnHTML("link[rel]", func(e *colly.HTMLElement) {
|
||||
if e.Attr("rel") == "icon" {
|
||||
meta.Icon = e.Request.AbsoluteURL(e.Attr("href"))
|
||||
}
|
||||
})
|
||||
|
||||
c.OnRequest(func(r *colly.Request) {
|
||||
log.Debug().Str("url", target).Msg("Expanding link... requesting")
|
||||
})
|
||||
c.RedirectHandler = func(req *http.Request, via []*http.Request) error {
|
||||
log.Debug().Str("url", req.URL.String()).Msg("Expanding link... redirecting")
|
||||
return nil
|
||||
}
|
||||
|
||||
c.OnResponse(func(r *colly.Response) {
|
||||
log.Debug().Str("url", target).Msg("Expanding link... analyzing")
|
||||
})
|
||||
c.OnError(func(r *colly.Response, err error) {
|
||||
log.Warn().Err(err).Str("url", target).Str("resp", string(r.Body)).Msg("Expanding link... failed")
|
||||
})
|
||||
|
||||
c.OnScraped(func(r *colly.Response) {
|
||||
log.Debug().Str("url", target).Msg("Expanding link... finished")
|
||||
})
|
||||
|
||||
return meta, c.Visit(target)
|
||||
}
|
Reference in New Issue
Block a user