youtube handling + special site handling framework
Some checks failed
docker / Docker (push) Has been cancelled
test / Test (push) Has been cancelled

This commit is contained in:
Failure 2024-08-06 08:50:35 -07:00
parent 6a88d9d0f4
commit 17d4d46d09
4 changed files with 97 additions and 8 deletions

View file

@ -9,7 +9,9 @@ import (
"github.com/gabriel-vasile/mimetype"
"go.uber.org/zap"
"io"
"net/url"
"os"
"regexp"
"strings"
)
@ -41,6 +43,37 @@ func (p *Processor) Shutdown(_ context.Context) error {
return nil
}
var youtubeIdRegex = regexp.MustCompile("[^\"&?\\/\\s\\.=]{11}")
func specialUrl(path string) (hostname string, specialHandler string, specialData map[string]string, err error) {
specialData = make(map[string]string)
components, err := url.Parse(path)
if err != nil {
return
}
hostname = components.Host
if trimmed, ok := strings.CutPrefix(hostname, "www."); ok {
hostname = trimmed
}
switch hostname {
case "youtu.be":
fallthrough
case "youtube.com":
fallthrough
case "m.youtube.com":
id := youtubeIdRegex.FindString(path)
println(id)
if id != "" {
specialHandler = "youtube"
specialData["id"] = id
}
}
return hostname, specialHandler, specialData, nil
}
func subThumbnail(url string) string {
key := os.Getenv("IMAGOR_SECRET")
params := imagorpath.Params{
@ -102,12 +135,17 @@ func (p *Processor) Process(ctx context.Context, in *imagor.Blob, params imagorp
return nil, err
}
hostname, specialHandler, specialData, _ := specialUrl(params.Image)
doc, err := htmlquery.Parse(strings.NewReader(string(all[:])))
meta := Metadata{
Format: strings.TrimPrefix(mime.Extension(), "."),
Title: "",
Description: "",
Image: "",
Format: strings.TrimPrefix(mime.Extension(), "."),
Title: "",
Description: "",
Image: "",
Hostname: hostname,
SpecialHandler: specialHandler,
SpecialData: specialData,
}
metaTags := htmlquery.Find(doc, "//meta[@property]")
for _, metaTag := range metaTags {
@ -148,10 +186,13 @@ func (p *Processor) Process(ctx context.Context, in *imagor.Blob, params imagorp
// Metadata imagorvideo metadata
type Metadata struct {
Format string `json:"format"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"image"`
Format string `json:"format"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"image"`
Hostname string `json:"hostname"`
SpecialHandler string `json:"special_handler"`
SpecialData map[string]string `json:"special_data"`
}
var transPixel = []byte("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B")