Skip to content
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

Add youtube shorts URLs #299

Merged
merged 4 commits into from
Apr 9, 2022
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 @@

- Breaking: Go version 1.17 is now the minimum required version to build this. (#292)
- Breaking: Resolver caches are now stored in PostgreSQL. See [docs/build.md](./docs/build.md) for prerequisite instructions. (#271)
- YouTube: Added support for 'YouTube shorts' URLs. (#299)
- Fix: SevenTV emotes now resolve correctly. (#281, #288)
- Fix: YouTube videos are no longer resolved as channels. (#284)
- Dev: Improve BetterTTV emote tests. (#282)
Expand Down
5 changes: 5 additions & 0 deletions internal/resolvers/youtube/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func getYoutubeVideoIDFromURL(url *url.URL) string {
return path.Base(url.Path)
}

// ex: https://www.youtube.com/shorts/nSW6scUfnFw
if base, rest := path.Split(url.Path); base == "/shorts/" {
return rest
}

return url.Query().Get("v")
}

Expand Down
19 changes: 19 additions & 0 deletions internal/resolvers/youtube/video_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func TestVideoResolver(t *testing.T) {
input: utils.MustParseURL("https://youtube.com/embed/foobar"),
expected: true,
},
{
label: "Correct domain, shorts path",
input: utils.MustParseURL("https://youtube.com/shorts/foobar"),
expected: true,
},
{
label: "Correct (sub)domain, correct path",
input: utils.MustParseURL("https://www.youtube.com/watch?v=foobar"),
Expand Down Expand Up @@ -155,6 +160,13 @@ func TestVideoResolver(t *testing.T) {
inputReq: nil,
expectedBytes: []byte(`{"status":200,"thumbnail":"https://example.com/thumbnail.png","tooltip":"%3Cdiv%20style=%22text-align:%20left%3B%22%3E%0A%3Cb%3EVideo%20Title%3C%2Fb%3E%0A%3Cbr%3E%3Cb%3EChannel:%3C%2Fb%3E%20Channel%20Title%0A%3Cbr%3E%3Cb%3EDuration:%3C%2Fb%3E%2000:00:00%0A%3Cbr%3E%3Cb%3EPublished:%3C%2Fb%3E%2012%20Oct%202019%0A%3Cbr%3E%3Cb%3EViews:%3C%2Fb%3E%2050%0A%3Cbr%3E%3Cb%3E%3Cspan%20style=%22color:%20red%3B%22%3EAGE%20RESTRICTED%3C%2Fspan%3E%3C%2Fb%3E%0A%3Cbr%3E%3Cspan%20style=%22color:%20%232ecc71%3B%22%3E10%20likes%3C%2Fspan%3E\u0026nbsp%3B%E2%80%A2\u0026nbsp%3B%3Cspan%20style=%22color:%20%23808892%3B%22%3E5%20comments%3C%2Fspan%3E%0A%3C%2Fdiv%3E%0A"}`),
},
{
label: "Video (Short)",
inputURL: utils.MustParseURL("https://youtube.com/shorts/foobar"),
inputVideoID: "foobar",
inputReq: nil,
expectedBytes: []byte(`{"status":200,"thumbnail":"https://example.com/thumbnail.png","tooltip":"%3Cdiv%20style=%22text-align:%20left%3B%22%3E%0A%3Cb%3EVideo%20Title%3C%2Fb%3E%0A%3Cbr%3E%3Cb%3EChannel:%3C%2Fb%3E%20Channel%20Title%0A%3Cbr%3E%3Cb%3EDuration:%3C%2Fb%3E%2000:00:00%0A%3Cbr%3E%3Cb%3EPublished:%3C%2Fb%3E%2012%20Oct%202019%0A%3Cbr%3E%3Cb%3EViews:%3C%2Fb%3E%2050%0A%3Cbr%3E%3Cb%3E%3Cspan%20style=%22color:%20red%3B%22%3EAGE%20RESTRICTED%3C%2Fspan%3E%3C%2Fb%3E%0A%3Cbr%3E%3Cspan%20style=%22color:%20%232ecc71%3B%22%3E10%20likes%3C%2Fspan%3E\u0026nbsp%3B%E2%80%A2\u0026nbsp%3B%3Cspan%20style=%22color:%20%23808892%3B%22%3E5%20comments%3C%2Fspan%3E%0A%3C%2Fdiv%3E%0A"}`),
},
{
label: "404",
inputURL: utils.MustParseURL("https://youtube.com/watch?v=404"),
Expand All @@ -169,6 +181,13 @@ func TestVideoResolver(t *testing.T) {
inputReq: nil,
expectedBytes: []byte(`{"status":500,"message":"YouTube API returned more than 2 videos"}`),
},
{
label: "Too many videos (Short)",
inputURL: utils.MustParseURL("https://youtube.com/shorts/toomany"),
inputVideoID: "toomany",
inputReq: nil,
expectedBytes: []byte(`{"status":500,"message":"YouTube API returned more than 2 videos"}`),
},
{
label: "Unavailable",
inputURL: utils.MustParseURL("https://youtube.com/watch?v=unavailable"),
Expand Down