diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-23 10:26:49 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-23 10:26:49 +0300 |
| commit | 772cf965f15bf06c7cf91095629f6b7f26848f83 (patch) | |
| tree | 58f94cceb51273a896b16518aec6466c458f9ca7 /internal | |
| parent | 09144495a49cd04df87f467ee045899cfeae3a30 (diff) | |
add url textract for linkedin
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/platforms/linkedin/escapes.go | 15 | ||||
| -rw-r--r-- | internal/platforms/linkedin/escapes_test.go | 43 |
2 files changed, 58 insertions, 0 deletions
diff --git a/internal/platforms/linkedin/escapes.go b/internal/platforms/linkedin/escapes.go index 5f803a5..21b4c56 100644 --- a/internal/platforms/linkedin/escapes.go +++ b/internal/platforms/linkedin/escapes.go @@ -1,6 +1,7 @@ package linkedin import ( + "regexp" "strings" ) @@ -37,3 +38,17 @@ func escapeLinkedInText(input string) string { return builder.String() } + +// extractURLs finds all occurrences of URLs starting with "http://" or "https://" in a given string. +func extractURLs(input string) []string { + // Regular expression pattern to match URLs starting with http:// or https:// + urlPattern := `(http://|https://)[^\s]+` + + // Compile the regular expression + re := regexp.MustCompile(urlPattern) + + // Find all matches in the input string + urls := re.FindAllString(input, -1) + + return urls +} diff --git a/internal/platforms/linkedin/escapes_test.go b/internal/platforms/linkedin/escapes_test.go index b1f9203..2074312 100644 --- a/internal/platforms/linkedin/escapes_test.go +++ b/internal/platforms/linkedin/escapes_test.go @@ -1,6 +1,8 @@ package linkedin import ( + "fmt" + "slices" "testing" ) @@ -13,3 +15,44 @@ func TestLinkedInEscapes(t *testing.T) { t.Errorf("expected '%s' but got '%s'", expected, escaped) } } + +func TestLinkedInTwoURLsExtract(t *testing.T) { + text := `Hello world https://foo.zone + Hello universe http://world.universe test 123` + + urls := extractURLs(text) + if len(urls) != 2 { + t.Errorf("expected 2 URLs, but got %d", len(urls)) + } + + if !slices.Contains(urls, "https://foo.zone") { + t.Errorf("expected 'https://foo.zone' in the URL list, but got %v", urls) + } + if !slices.Contains(urls, "http://world.universe") { + t.Errorf("expected 'http://world.universe' in the URL list, but got %v", urls) + } +} + +// TODO: Use Fuzzing here! +func TestLinkedInURLExtract(t *testing.T) { + urls := []string{ + "http://foo.zone", + "http://foo.zone/", + "http://foo.zone?foo=bar", + "http://foo.zone/?foo=bar", + "http://foo.zone/?foo=bar", + "http://foo.zone/hurs?foo=bar", + "http://foo.zone?foo=bar&baz=bay", + } + + for _, url := range urls { + text := fmt.Sprintf("Hello world %s Hello World", url) + found := extractURLs(text) + if len(found) != 1 { + t.Errorf("expected 1 URL, but got %d for text '%s'", len(found), text) + } + if found[0] != url { + t.Errorf("expected URL '%s', but got '%s' for text '%s'", url, found[0], text) + } + } +} |
