Skip to content

Add support for clips from m.twitch.tv domain #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- YouTube: Removed dislike count from rich tooltips since YouTube removed it. (#243)
- Twitter: Blacklist special pages from being resolved as user pages. (#220)
- Twitch: Handle Twitch clips from `m.twitch.tv` domain. (#239)
- Updated Facebook & Instagram endpoints to oembed v10. (#201)

## 1.2.1
Expand Down
20 changes: 16 additions & 4 deletions internal/resolvers/twitch/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,34 @@ import (
)

var (
clipSlugRegex = regexp.MustCompile(`^\/(\w{2,25}\/clip\/)?([a-zA-Z0-9]+(?:-[-\w]{16})?)$`)
clipSlugRegex = regexp.MustCompile(`^\/(\w{2,25}\/clip\/)?(clip\/)?([a-zA-Z0-9]+(?:-[-\w]{16})?)$`)
)

func check(url *url.URL) bool {
// Regardless of domain path needs to match anyway, so we do it here to avoid duplication
matches := clipSlugRegex.FindStringSubmatch(url.Path)

if len(matches) != 3 {
match, domain := resolver.MatchesHosts(url, domains)
if !match {
return false
}

match, domain := resolver.MatchesHosts(url, domains)
if !match {
if len(matches) != 4 {
return false
}

if domain == "m.twitch.tv" {
// Find clips that look like https://m.twitch.tv/clip/SlugHere
// matches[2] contains "clip/" - both this and matches[1] cannot be non-empty at the same time
if matches[2] == "clip/" {
return matches[1] == ""
}

// Find clips that look like https://m.twitch.tv/StreamerName/clip/SlugHere
// matches[1] contains "StreamerName/clip/" - we need it in this check
return matches[1] != ""
}

// Find clips that look like https://clips.twitch.tv/SlugHere
if domain == "clips.twitch.tv" {
// matches[1] contains "StreamerName/clip/" - we don't want it in this check though
Expand Down
10 changes: 10 additions & 0 deletions internal/resolvers/twitch/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ var validClips = []string{
"https://twitch.tv/pajlada/clip/" + goodSlugV2,
"https://twitch.tv/zneix/clip/" + goodSlugV1,
"https://twitch.tv/zneix/clip/" + goodSlugV2,
"https://m.twitch.tv/pajlada/clip/" + goodSlugV1,
"https://m.twitch.tv/pajlada/clip/" + goodSlugV2,
"https://m.twitch.tv/zneix/clip/" + goodSlugV1,
"https://m.twitch.tv/zneix/clip/" + goodSlugV2,
"https://m.twitch.tv/clip/" + goodSlugV1,
"https://m.twitch.tv/clip/" + goodSlugV2,
"https://m.twitch.tv/clip/clip/" + goodSlugV1,
"https://m.twitch.tv/clip/clip/" + goodSlugV2,
}

var invalidClips = []string{
Expand All @@ -33,10 +41,12 @@ var invalidClips = []string{
"https://twitch.tv/supinic/clip/",
"https://twitch.tv/pajlada/clips/VastBitterVultureMau5",
"https://twitch.tv/zneix/clip/ImpossibleOilyAlpacaTF2John-jIlgtnSAQ52BThHhifyouseethisvivon",
"https://twitch.tv/clip/slug",
"https://gql.twitch.tv/VastBitterVultureMau5",
"https://gql.twitch.tv/ThreeLetterAPI/clip/VastBitterVultureMau5",
"https://m.twitch.tv/VastBitterVultureMau5",
"https://m.twitch.tv/username/clip/clip/slug",
"https://m.twitch.tv/username/notclip/slug",
}

func testCheck(c *qt.C, urlString string) bool {
Expand Down
4 changes: 2 additions & 2 deletions internal/resolvers/twitch/parse_clip_slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import "net/url"
func parseClipSlug(url *url.URL) (string, error) {
matches := clipSlugRegex.FindStringSubmatch(url.Path)

if len(matches) != 3 {
if len(matches) != 4 {
return "", errInvalidTwitchClip
}

return matches[2], nil
return matches[3], nil
}
2 changes: 0 additions & 2 deletions internal/resolvers/twitch/parse_clip_slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ func testParseClipSlug(urlString string) (string, error) {
return parseClipSlug(u)
}

// TODO: Test m.twitch.tv/username/clip/clip/slug and make sure it doesn't work

func TestParseClipSlug(t *testing.T) {
c := qt.New(t)

Expand Down
1 change: 1 addition & 0 deletions internal/resolvers/twitch/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
domains = map[string]struct{}{
"twitch.tv": {},
"www.twitch.tv": {},
"m.twitch.tv": {},
"clips.twitch.tv": {},
}

Expand Down