From 8f007bbe046a5d5898b378a5200437e3d280d14c Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 13 Oct 2023 21:56:14 -0300 Subject: [PATCH] fix image and url matching by reusing the utils.go regexes. --- data.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/data.go b/data.go index 0fc5447..d308fb3 100644 --- a/data.go +++ b/data.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "regexp" "strings" "time" @@ -177,26 +176,27 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e } kindNIP := kindNIPs[event.Kind] - imageMatch := regexp.MustCompile(`https:\/\/[^ ]*\.(gif|jpe?g|png|webp)`).FindStringSubmatch(event.Content) + urls := urlMatcher.FindAllString(event.Content, -1) var image string - if len(imageMatch) > 0 { - image = imageMatch[0] - } - - videoMatch := regexp.MustCompile(`https:\/\/[^ ]*\.(mp4|mov|webm)`).FindStringSubmatch(event.Content) var video string - if len(videoMatch) > 0 { - video = videoMatch[0] - } - var videoType string - if video != "" { - if strings.HasSuffix(video, "mp4") { - videoType = "mp4" - } else if strings.HasSuffix(video, "mov") { - videoType = "mov" - } else { - videoType = "webm" + for _, url := range urls { + switch { + case imageExtensionMatcher.MatchString(url): + if image == "" { + image = url + } + case videoExtensionMatcher.MatchString(url): + if video == "" { + video = url + if strings.HasSuffix(video, "mp4") { + videoType = "mp4" + } else if strings.HasSuffix(video, "mov") { + videoType = "mov" + } else { + videoType = "webm" + } + } } }