zorchenhimer / MoviePolls

Voting to decide on a movie to watch with MovieNight
https://discord.gg/F2VSgjJ
16 stars 6 forks source link

Cleaning up mobile link fragments from inserted links #91 #101

Closed CptPie closed 3 years ago

CptPie commented 3 years ago

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

zorchenhimer commented 3 years ago

With the way it's setup now I can see it becoming a mess of string.Replace(). We could use a combination of a for loop and a map to make it a bit more maintainable.

Also, there will now be two separate functions that are used to cleanup a link. Why not combine these two into one function?

With that in mind, thoughts?

var replacements map[string]string{
    "m.imdb.com": "imdb.com",
}

func linkCleanup(link string) string {
    // strip ref
    idx := strings.Index(link, "?")
    if idx != -1 {
        link = link[:idx]
    }

    // remove mobile
    for from, to := range replacements {
        link = strings.Replace(link, from, to, 1)
    }

    return link
}
CptPie commented 3 years ago

@zorchenhimer i implemented the requested changes in 99a33fc