mirror of
https://github.com/Hopiu/lychee.git
synced 2026-04-29 09:24:46 +00:00
Properly handle youtu.be shortlinks (#908)
Previously those were not correctly rewritten to thumbnail URLs. This should be fixed now by splitting up the logic for normal YouTube links and shortlinks. Fixes #906
This commit is contained in:
parent
1a393fc8f1
commit
b620fc99f7
1 changed files with 27 additions and 1 deletions
|
|
@ -53,7 +53,7 @@ impl Default for Quirks {
|
|||
// (https://img.youtube.com/vi/{video_id}/0.jpg).
|
||||
// This works for all known video visibilities.
|
||||
// See https://github.com/lycheeverse/lychee/issues/214#issuecomment-819103393)
|
||||
pattern: Regex::new(r"^(https?://)?(www\.)?(youtube\.com|youtu\.?be)").unwrap(),
|
||||
pattern: Regex::new(r"^(https?://)?(www\.)?(youtube\.com)").unwrap(),
|
||||
rewrite: |mut request| {
|
||||
if request.url().path() != "/watch" {
|
||||
return request;
|
||||
|
|
@ -65,6 +65,19 @@ impl Default for Quirks {
|
|||
request
|
||||
},
|
||||
},
|
||||
Quirk {
|
||||
pattern: Regex::new(r"^(https?://)?(www\.)?(youtu\.?be)").unwrap(),
|
||||
rewrite: |mut request| {
|
||||
// Short links use the path as video id
|
||||
let id = request.url().path().trim_start_matches('/');
|
||||
if id.is_empty() {
|
||||
return request;
|
||||
}
|
||||
*request.url_mut() =
|
||||
Url::parse(&format!("https://img.youtube.com/vi/{id}/0.jpg")).unwrap();
|
||||
request
|
||||
},
|
||||
},
|
||||
];
|
||||
Self { quirks }
|
||||
}
|
||||
|
|
@ -161,6 +174,19 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_youtube_video_shortlink_request() {
|
||||
let url = Url::parse("https://youtu.be/Rvu7N4wyFpk?t=42").unwrap();
|
||||
let request = Request::new(Method::GET, url);
|
||||
let modified = Quirks::default().apply(request);
|
||||
let expected_url = Url::parse("https://img.youtube.com/vi/Rvu7N4wyFpk/0.jpg").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
MockRequest(modified),
|
||||
MockRequest::new(Method::GET, expected_url)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_non_video_youtube_url_untouched() {
|
||||
let url = Url::parse("https://www.youtube.com/channel/UCaYhcUwRBNscFNUKTjgPFiA").unwrap();
|
||||
|
|
|
|||
Loading…
Reference in a new issue