refactor: use fancy-regex only for backtracking

This commit is contained in:
ThetaDev 2023-01-23 15:33:05 +01:00
parent 4cc069fba2
commit 92a358a079
12 changed files with 69 additions and 108 deletions

View file

@ -25,10 +25,10 @@ mod channel_rss;
use std::sync::Arc;
use std::{borrow::Cow, fmt::Debug};
use fancy_regex::Regex;
use log::{debug, error, warn};
use once_cell::sync::Lazy;
use rand::Rng;
use regex::Regex;
use reqwest::{header, Client, ClientBuilder, Request, RequestBuilder, Response};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use time::{Duration, OffsetDateTime};

View file

@ -1,8 +1,8 @@
use std::{borrow::Cow, rc::Rc};
use fancy_regex::Regex;
use futures::{stream, StreamExt};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Serialize;
use crate::{
@ -268,8 +268,6 @@ fn map_artist_page(
let wikipedia_url = header.description.as_deref().and_then(|h| {
WIKIPEDIA_REGEX
.captures(h)
.ok()
.flatten()
.and_then(|c| c.get(0))
.map(|m| m.as_str().to_owned())
});

View file

@ -3,8 +3,8 @@ use std::{
collections::{BTreeMap, HashMap},
};
use fancy_regex::Regex;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Serialize;
use url::Url;
@ -530,8 +530,6 @@ fn map_audio_stream(
Some(t) => {
let lang = LANG_PATTERN
.captures(&t.id)
.ok()
.flatten()
.map(|m| m.get(1).unwrap().as_str().to_owned());
Some(AudioTrack {
@ -557,7 +555,7 @@ fn parse_mime(mime: &str) -> Option<(&str, Vec<&str>)> {
static PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"(\w+/\w+);\scodecs="([a-zA-Z-0-9.,\s]*)""#).unwrap());
let captures = some_or_bail!(PATTERN.captures(mime).ok().flatten(), None);
let captures = some_or_bail!(PATTERN.captures(mime), None);
Some((
captures.get(1).unwrap().as_str(),
captures

View file

@ -97,7 +97,7 @@ impl From<ChannelRss> for crate::model::ChannelRss {
.uri
.strip_prefix("https://www.youtube.com/channel/")
.and_then(|id| {
if util::CHANNEL_ID_REGEX.is_match(id).unwrap_or_default() {
if util::CHANNEL_ID_REGEX.is_match(id) {
Some(id.to_owned())
} else {
None

View file

@ -1,5 +1,5 @@
use fancy_regex::Regex;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Deserialize;
use serde_with::{
json::JsonString, rust::deserialize_ignore_any, serde_as, DefaultOnError, VecSkipError,
@ -503,20 +503,12 @@ impl<T> YouTubeListMapper<T> {
id: video.video_id,
name: video.headline,
length: video.accessibility.and_then(|acc| {
ACCESSIBILITY_SEP_REGEX
.captures(&acc)
.ok()
.flatten()
.and_then(|cap| {
cap.get(1).and_then(|c| {
timeago::parse_timeago_or_warn(
self.lang,
c.as_str(),
&mut self.warnings,
)
ACCESSIBILITY_SEP_REGEX.captures(&acc).and_then(|cap| {
cap.get(1).and_then(|c| {
timeago::parse_timeago_or_warn(self.lang, c.as_str(), &mut self.warnings)
.map(|ta| Duration::from(ta).whole_seconds() as u32)
})
})
})
}),
thumbnail: video.thumbnail.into(),
channel: self.channel.clone(),

View file

@ -112,9 +112,9 @@ impl RustyPipeQuery {
// Album or channel
Some("browse") => match path_split.next() {
Some(id) => {
if util::CHANNEL_ID_REGEX.is_match(id).unwrap_or_default() {
if util::CHANNEL_ID_REGEX.is_match(id) {
Ok(UrlTarget::Channel { id: id.to_owned() })
} else if util::ALBUM_ID_REGEX.is_match(id).unwrap_or_default() {
} else if util::ALBUM_ID_REGEX.is_match(id) {
Ok(UrlTarget::Album { id: id.to_owned() })
} else {
Err(Error::Other("invalid url: no browse id".into()))
@ -153,10 +153,7 @@ impl RustyPipeQuery {
// If there is a timestamp parameter, it has to be a video
// First check the innertube API if this is a channel vanity url
// If no channel is found and the identifier has the video ID format, assume it is a video
if !params.contains_key("t")
&& util::VANITY_PATH_REGEX
.is_match(url.path())
.unwrap_or_default()
if !params.contains_key("t") && util::VANITY_PATH_REGEX.is_match(url.path())
{
match self
._navigation_resolve_url(url.path(), ClientType::Desktop)
@ -164,7 +161,7 @@ impl RustyPipeQuery {
{
Ok(target) => Ok(target),
Err(Error::Extraction(ExtractionError::ContentUnavailable(e))) => {
match util::VIDEO_ID_REGEX.is_match(id).unwrap_or_default() {
match util::VIDEO_ID_REGEX.is_match(id) {
true => Ok(UrlTarget::Video {
id: id.to_owned(),
start_time: get_start_time(),
@ -176,7 +173,7 @@ impl RustyPipeQuery {
}
Err(e) => Err(e),
}
} else if util::VIDEO_ID_REGEX.is_match(id).unwrap_or_default() {
} else if util::VIDEO_ID_REGEX.is_match(id) {
Ok(UrlTarget::Video {
id: id.to_owned(),
start_time: get_start_time(),
@ -232,16 +229,16 @@ impl RustyPipeQuery {
.await
}
// ID only
else if util::VIDEO_ID_REGEX.is_match(string).unwrap_or_default() {
else if util::VIDEO_ID_REGEX.is_match(string) {
Ok(UrlTarget::Video {
id: string.to_owned(),
start_time: 0,
})
} else if util::CHANNEL_ID_REGEX.is_match(string).unwrap_or_default() {
} else if util::CHANNEL_ID_REGEX.is_match(string) {
Ok(UrlTarget::Channel {
id: string.to_owned(),
})
} else if util::PLAYLIST_ID_REGEX.is_match(string).unwrap_or_default() {
} else if util::PLAYLIST_ID_REGEX.is_match(string) {
if resolve_albums && string.starts_with(util::PLAYLIST_ID_ALBUM_PREFIX) {
self._navigation_resolve_url(
&format!("/playlist?list={}", string),
@ -253,13 +250,13 @@ impl RustyPipeQuery {
id: string.to_owned(),
})
}
} else if util::ALBUM_ID_REGEX.is_match(string).unwrap_or_default() {
} else if util::ALBUM_ID_REGEX.is_match(string) {
Ok(UrlTarget::Album {
id: string.to_owned(),
})
}
// Channel name only
else if util::VANITY_PATH_REGEX.is_match(string).unwrap_or_default() {
else if util::VANITY_PATH_REGEX.is_match(string) {
self._navigation_resolve_url(
&format!("/{}", string.trim_start_matches('/')),
ClientType::Desktop,