| 25 | fn html_string(&'a self) -> &'a str; |
| 26 | |
| 27 | fn caption(&'a self, from: &str, to: &str) -> Result<Caption, error::Error> { |
| 28 | let html = self.html_string(); |
| 29 | let start = html |
| 30 | .split_once(from) |
| 31 | .ok_or_else(|| error::Error::ParseError(format!("Cannot parse html for: {}", from)))? |
| 32 | .1; |
| 33 | let actual_json = start |
| 34 | .split_once(to) |
| 35 | .ok_or_else(|| error::Error::ParseError(format!("Cannot parse html to: {}", to)))? |
| 36 | .0; |
| 37 | let value: Captions = serde_json::from_str(actual_json) |
| 38 | .map_err(|x| error::Error::ParseError(format!("{}", x)))?; |
| 39 | let caption = value |
| 40 | .caption_tracks |
| 41 | .into_iter() |
| 42 | .filter(|x| x.lang_code == "en") |
| 43 | .next() |
| 44 | .ok_or(error::Error::ParseError("Cannot find lang en".into()))?; |
| 45 | Ok(caption) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl<'a> HTMLParser<'a> for String { |