From 58c3174e953125fd97585db0451ce200823b3355 Mon Sep 17 00:00:00 2001 From: CptPie <23438606+CptPie@users.noreply.github.com> Date: Mon, 25 Jan 2021 02:15:31 +0100 Subject: [PATCH 1/2] Added a new function in link.go This function is used to clean up mobile modifiers in (for now) imdb links. I created the function to later adapt it for more urls (now it just uses strings.Replace). This fixes #91 --- common/link.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/common/link.go b/common/link.go index 04d9782..9d04bf6 100644 --- a/common/link.go +++ b/common/link.go @@ -49,6 +49,7 @@ func (l *Link) validateLink() error { url := l.Url if re_validLink.MatchString(url) { url = stripRefFromLink(url) + url = stripMobileFromLink(url) if len(url) <= 8 { // lets be stupid when the link is too short @@ -76,6 +77,12 @@ func stripRefFromLink(link string) string { return link } +// for now we just replace it for imdb, if we need others we need to adapt that +// is it stupid, YES, does it work, i guess? do i have a better idea, no +func stripMobileFromLink(link string) string { + return strings.Replace(link, "m.imdb.com", "imdb.com", 1) +} + func (l *Link) determineLinkType() error { if strings.Contains(l.Url, "imdb") { l.Type = "IMDb" From 99a33fcb348633376a30abcc7012e84b28f9bd80 Mon Sep 17 00:00:00 2001 From: CptPie <23438606+CptPie@users.noreply.github.com> Date: Sat, 30 Jan 2021 23:59:26 +0100 Subject: [PATCH 2/2] Tidied up link cleanup code according to zorchs review --- common/link.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/common/link.go b/common/link.go index 9d04bf6..68f810c 100644 --- a/common/link.go +++ b/common/link.go @@ -47,9 +47,8 @@ var re_validLink = *regexp.MustCompile(`[a-zA-Z0-9:._\+]{1,256}\.[a-zA-Z0-9()]{1 func (l *Link) validateLink() error { url := l.Url + url = cleanupLink(url) if re_validLink.MatchString(url) { - url = stripRefFromLink(url) - url = stripMobileFromLink(url) if len(url) <= 8 { // lets be stupid when the link is too short @@ -69,18 +68,20 @@ func (l *Link) validateLink() error { return fmt.Errorf("Invalid link: %v", l.Url) } -func stripRefFromLink(link string) string { +var replacements = map[string]string{ + "m.imdb.com": "imdb.com", +} + +func cleanupLink(link string) string { idx := strings.Index(link, "/?") if idx != -1 { - return link[:idx] + link = link[:idx] + } + for from, to := range replacements { + link = strings.Replace(link, from, to, 1) } - return link -} -// for now we just replace it for imdb, if we need others we need to adapt that -// is it stupid, YES, does it work, i guess? do i have a better idea, no -func stripMobileFromLink(link string) string { - return strings.Replace(link, "m.imdb.com", "imdb.com", 1) + return link } func (l *Link) determineLinkType() error {