Procházet zdrojové kódy

refactor(processor): use URL parsing instead of a regex

Julien Voisin před 1 rokem
rodič
revize
3caa16ac31

+ 8 - 5
internal/reader/processor/nebula.go

@@ -7,7 +7,7 @@ import (
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
 	"log/slog"
 	"log/slog"
-	"regexp"
+	"net/url"
 	"strconv"
 	"strconv"
 
 
 	"github.com/PuerkitoBio/goquery"
 	"github.com/PuerkitoBio/goquery"
@@ -17,14 +17,17 @@ import (
 	"miniflux.app/v2/internal/reader/fetcher"
 	"miniflux.app/v2/internal/reader/fetcher"
 )
 )
 
 
-var nebulaRegex = regexp.MustCompile(`^https://nebula\.tv`)
-
 func shouldFetchNebulaWatchTime(entry *model.Entry) bool {
 func shouldFetchNebulaWatchTime(entry *model.Entry) bool {
 	if !config.Opts.FetchNebulaWatchTime() {
 	if !config.Opts.FetchNebulaWatchTime() {
 		return false
 		return false
 	}
 	}
-	matches := nebulaRegex.FindStringSubmatch(entry.URL)
-	return matches != nil
+
+	u, err := url.Parse(entry.URL)
+	if err != nil {
+		return false
+	}
+
+	return u.Hostname() == "nebula.tv"
 }
 }
 
 
 func fetchNebulaWatchTime(websiteURL string) (int, error) {
 func fetchNebulaWatchTime(websiteURL string) (int, error) {

+ 8 - 5
internal/reader/processor/odysee.go

@@ -7,7 +7,7 @@ import (
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
 	"log/slog"
 	"log/slog"
-	"regexp"
+	"net/url"
 	"strconv"
 	"strconv"
 
 
 	"github.com/PuerkitoBio/goquery"
 	"github.com/PuerkitoBio/goquery"
@@ -17,14 +17,17 @@ import (
 	"miniflux.app/v2/internal/reader/fetcher"
 	"miniflux.app/v2/internal/reader/fetcher"
 )
 )
 
 
-var odyseeRegex = regexp.MustCompile(`^https://odysee\.com`)
-
 func shouldFetchOdyseeWatchTime(entry *model.Entry) bool {
 func shouldFetchOdyseeWatchTime(entry *model.Entry) bool {
 	if !config.Opts.FetchOdyseeWatchTime() {
 	if !config.Opts.FetchOdyseeWatchTime() {
 		return false
 		return false
 	}
 	}
-	matches := odyseeRegex.FindStringSubmatch(entry.URL)
-	return matches != nil
+
+	u, err := url.Parse(entry.URL)
+	if err != nil {
+		return false
+	}
+
+	return u.Hostname() == "odysee.com"
 }
 }
 
 
 func fetchOdyseeWatchTime(websiteURL string) (int, error) {
 func fetchOdyseeWatchTime(websiteURL string) (int, error) {