Fix chaturbate search URL (swapped format args) and stale cache branch
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MUYdCh2nCVsQYgJAT7gwSi
This commit is contained in:
@@ -138,6 +138,34 @@ impl ChaturbateProvider {
|
||||
Ok(video_items)
|
||||
}
|
||||
|
||||
/// `keywords=` search on the room list. (The base URL and query used to be
|
||||
/// passed in swapped order, producing `milf/api/...?keywords=https://...`.)
|
||||
fn build_search_url(base: &str, query: &str, page: u8) -> String {
|
||||
let keywords = query
|
||||
.trim()
|
||||
.chars()
|
||||
.map(|c| match c {
|
||||
' ' => "+".to_string(),
|
||||
c if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '~') => {
|
||||
c.to_string()
|
||||
}
|
||||
c => {
|
||||
let mut buf = [0u8; 4];
|
||||
c.encode_utf8(&mut buf)
|
||||
.bytes()
|
||||
.map(|b| format!("%{b:02X}"))
|
||||
.collect::<String>()
|
||||
}
|
||||
})
|
||||
.collect::<String>();
|
||||
format!(
|
||||
"{}/api/ts/roomlist/room-list/?keywords={}&limit=90&offset={}",
|
||||
base,
|
||||
keywords,
|
||||
90 * (page.max(1) as u32 - 1)
|
||||
)
|
||||
}
|
||||
|
||||
async fn query(
|
||||
&self,
|
||||
cache: VideoCache,
|
||||
@@ -145,22 +173,15 @@ impl ChaturbateProvider {
|
||||
query: &str,
|
||||
options: ServerOptions,
|
||||
) -> Result<Vec<VideoItem>> {
|
||||
let mut video_url = format!(
|
||||
"{}/api/ts/roomlist/room-list/?keywords={}&limit=90&offset={}",
|
||||
query,
|
||||
self.url,
|
||||
90 * (page - 1)
|
||||
);
|
||||
video_url = video_url.replace(" ", "+");
|
||||
let video_url = Self::build_search_url(&self.url, query, page);
|
||||
// Check our Video Cache. If the result is younger than 1 hour, we return it.
|
||||
let old_items = match cache.get(&video_url) {
|
||||
Some((time, items)) => {
|
||||
if time.elapsed().unwrap_or_default().as_secs() < 60 * 1 {
|
||||
return Ok(items.clone());
|
||||
} else {
|
||||
let _ = cache.check().await;
|
||||
return Ok(items.clone());
|
||||
}
|
||||
// Expired: refetch, keeping the stale list as the fallback.
|
||||
items.clone()
|
||||
}
|
||||
None => {
|
||||
vec![]
|
||||
@@ -347,3 +368,18 @@ impl Provider for ChaturbateProvider {
|
||||
Some(self.build_channel(clientversion))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ChaturbateProvider;
|
||||
|
||||
#[test]
|
||||
fn builds_search_url_with_base_first() {
|
||||
assert_eq!(
|
||||
ChaturbateProvider::build_search_url("https://chaturbate.com", "big tits", 2),
|
||||
"https://chaturbate.com/api/ts/roomlist/room-list/?keywords=big+tits&limit=90&offset=90"
|
||||
);
|
||||
assert!(ChaturbateProvider::build_search_url("https://chaturbate.com", "a&b", 1)
|
||||
.contains("keywords=a%26b&"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user