Skip to content

Commit

Permalink
fix: youtube short url path with timestamp parsing (#545)
Browse files Browse the repository at this point in the history
Links like https://youtu.be/qeUAHHPt-LY&t=225 previously did not parse
the correct video ID, we now use the same fields func method as with
long youtube links but with ? and &
  • Loading branch information
pajlada authored Oct 11, 2023
1 parent 1d8ed20 commit a332411
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- Minor: Use Twitter OG tags if no Twitter credentials are configured. (#522)
- Minor: Support `x.com` for tweets. (#527)
- Minor: Increase tweet cache duration for non-credentialed requests to 24h. Currently not configurable. (#528)
- Fix: We do some more YouTube video ID parsing to ensure broken links (such as `youtube.com/watch?v=foobar?feature=share`) still return the actual video ID, since this is how the browser operates. (#488)
- Fix: We do some more YouTube video ID parsing to ensure broken links (such as `youtube.com/watch?v=foobar?feature=share`) still return the actual video ID, since this is how the browser operates. (#488, #545)
- Dev: Document the `log-development` setting. (#491)
- Dev: Document the `log-level` setting. (#490)
- Dev: Add some `pkg/utils/url.go` tests. (#525)
Expand Down
7 changes: 6 additions & 1 deletion internal/resolvers/youtube/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@ func getYoutubeVideoIDFromURL(url *url.URL) string {
}

func getYoutubeVideoIDFromURL2(url *url.URL) string {
return path.Base(url.Path)
v := path.Base(url.Path)
fields := strings.FieldsFunc(v, func(r rune) bool {
return r == '?' || r == '&'
})

return fields[0]
}
11 changes: 11 additions & 0 deletions internal/resolvers/youtube/video_shorturl_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ func TestVideoShortURLResolver(t *testing.T) {
ContentType: "application/json",
},
},
{
label: "Video with path semi-broken that we can fix",
inputURL: utils.MustParseURL("https://youtu.be/foobar&t=123"),
inputVideoID: "foobar",
inputReq: nil,
expectedResponse: &cache.Response{
Payload: []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"}`),
StatusCode: http.StatusOK,
ContentType: "application/json",
},
},
{
label: "404",
inputURL: utils.MustParseURL("https://youtu.be/404"),
Expand Down

0 comments on commit a332411

Please sign in to comment.