Skip to content

Commit

Permalink
Normalize url
Browse files Browse the repository at this point in the history
  • Loading branch information
rroller committed Nov 16, 2024
1 parent 8ff36ca commit bdcbc8d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/media/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/dustin/go-humanize"
"html/template"
"media-roller/src/utils"
"net/http"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -94,6 +95,8 @@ func getMediaResults(url string) ([]Media, string, error) {
return nil, "", errors.New("missing URL")
}

url = utils.NormalizeUrl(url)

// NOTE: This system is for a simple use case, meant to run at home. This is not a great design for a robust system.
// We are hashing the URL here and writing files to disk to a consistent directory based on the ID. You can imagine
// concurrent users would break this for the same URL. That's fine given this is for a simple home system.
Expand Down Expand Up @@ -128,7 +131,7 @@ func downloadMedia(url string) (string, string, error) {
cmd := exec.Command("yt-dlp",
"--format", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
"--merge-output-format", "mp4",
"--trim-filenames", "200",
"--trim-filenames", "200",
"--restrict-filenames",
"--write-info-json",
"--verbose",
Expand Down
18 changes: 18 additions & 0 deletions src/utils/urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import "strings"

func NormalizeUrl(url string) string {
url = strings.TrimSpace(url)
parts := strings.Split(url, " ")

for _, part := range parts {
// Take the firs string that looks like a URL.
// TODO: We could try to parse the url, but will save that for later
if strings.HasPrefix(part, "http") || strings.HasPrefix(part, "www") {
return part
}
}

return url
}
21 changes: 21 additions & 0 deletions src/utils/urls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils

import "testing"

func TestNormalizeUrl(t *testing.T) {
tests := []struct {
url string
want string
}{
{url: "example.com", want: "example.com"},
{url: "https://example.com", want: "https://example.com"},
{url: "https://example.com this is an example", want: "https://example.com"},
}
for _, tt := range tests {
t.Run(tt.url, func(t *testing.T) {
if got := NormalizeUrl(tt.url); got != tt.want {
t.Errorf("NormalizeUrl() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit bdcbc8d

Please sign in to comment.