|
| 1 | +//! The `sepiasearch` module handles the fetching of results from the SepiaSearch video search |
| 2 | +//! engine (PeerTube search index) by querying its JSON API with the user provided query. |
| 3 | +
|
| 4 | +use reqwest::{Client, header::HeaderMap}; |
| 5 | +use serde::Deserialize; |
| 6 | +use std::collections::HashMap; |
| 7 | + |
| 8 | +use crate::models::aggregation::SearchResult; |
| 9 | +use crate::models::engine::{EngineError, SearchEngine}; |
| 10 | +use error_stack::{Report, Result, ResultExt}; |
| 11 | + |
| 12 | +/// A new SepiaSearch engine type defined in-order to implement the `SearchEngine` trait. |
| 13 | +pub struct SepiaSearch; |
| 14 | + |
| 15 | +/// The JSON response structure returned by the SepiaSearch API. |
| 16 | +#[derive(Deserialize)] |
| 17 | +struct SepiaSearchResponse { |
| 18 | + /// The list of video results returned by the API. |
| 19 | + data: Vec<SepiaSearchVideo>, |
| 20 | + /// The total number of results available. |
| 21 | + total: Option<u32>, |
| 22 | + /// An error message, if the API returned one. |
| 23 | + error: Option<String>, |
| 24 | +} |
| 25 | + |
| 26 | +/// A single video result from the SepiaSearch API. |
| 27 | +#[derive(Deserialize)] |
| 28 | +struct SepiaSearchVideo { |
| 29 | + /// The title of the video. |
| 30 | + name: String, |
| 31 | + /// The URL to watch the video. |
| 32 | + url: String, |
| 33 | + /// An optional description of the video. |
| 34 | + description: Option<String>, |
| 35 | +} |
| 36 | + |
| 37 | +impl SepiaSearch { |
| 38 | + /// Creates a new SepiaSearch engine instance. |
| 39 | + pub fn new() -> Result<SepiaSearch, EngineError> { |
| 40 | + Ok(Self) |
| 41 | + } |
| 42 | + |
| 43 | + /// Parses the raw JSON response body into a list of search results. |
| 44 | + fn parse_json_response(json: &[u8]) -> Result<Vec<(String, SearchResult)>, EngineError> { |
| 45 | + let response: SepiaSearchResponse = |
| 46 | + serde_json::from_slice(json).change_context(EngineError::UnexpectedError)?; |
| 47 | + |
| 48 | + if let Some(err) = &response.error { |
| 49 | + return Err(Report::new(EngineError::UnexpectedError) |
| 50 | + .attach(format!("SepiaSearch API error: {err}"))); |
| 51 | + } |
| 52 | + |
| 53 | + let results = response |
| 54 | + .data |
| 55 | + .into_iter() |
| 56 | + .map(|video| { |
| 57 | + let description = video.description.unwrap_or_default().trim().to_string(); |
| 58 | + let search_result = SearchResult::new( |
| 59 | + video.name.trim(), |
| 60 | + video.url.as_str(), |
| 61 | + description.as_str(), |
| 62 | + &["sepiasearch"], |
| 63 | + ); |
| 64 | + (search_result.url.clone(), search_result) |
| 65 | + }) |
| 66 | + .collect(); |
| 67 | + |
| 68 | + Ok(results) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[async_trait::async_trait] |
| 73 | +impl SearchEngine for SepiaSearch { |
| 74 | + async fn results( |
| 75 | + &self, |
| 76 | + query: &str, |
| 77 | + page: u32, |
| 78 | + user_agent: &str, |
| 79 | + client: &Client, |
| 80 | + safe_search: u8, |
| 81 | + ) -> Result<Vec<(String, SearchResult)>, EngineError> { |
| 82 | + let nsfw = if safe_search == 0 { "both" } else { "false" }; |
| 83 | + |
| 84 | + // Pagination: 0-based offset, 10 results per page |
| 85 | + let start = page * 10; |
| 86 | + |
| 87 | + let encoded_query = form_urlencoded::byte_serialize(query.as_bytes()).collect::<String>(); |
| 88 | + let url = format!( |
| 89 | + "https://sepiasearch.org/api/v1/search/videos?search={encoded_query}&start={start}&count=10&sort=-match&nsfw={nsfw}" |
| 90 | + ); |
| 91 | + |
| 92 | + let header_map = HeaderMap::try_from(&HashMap::from([ |
| 93 | + ("User-Agent".to_string(), user_agent.to_string()), |
| 94 | + ( |
| 95 | + "Referer".to_string(), |
| 96 | + "https://sepiasearch.org/".to_string(), |
| 97 | + ), |
| 98 | + ( |
| 99 | + "Content-Type".to_string(), |
| 100 | + "text/html; charset=utf-8".to_string(), |
| 101 | + ), |
| 102 | + ])) |
| 103 | + .change_context(EngineError::UnexpectedError)?; |
| 104 | + |
| 105 | + let json_bytes = |
| 106 | + SepiaSearch::fetch_json_as_bytes_from_upstream(self, &url, header_map, client).await?; |
| 107 | + |
| 108 | + let results = Self::parse_json_response(&json_bytes)?; |
| 109 | + |
| 110 | + if results.is_empty() { |
| 111 | + return Err(Report::new(EngineError::EmptyResultSet)); |
| 112 | + } |
| 113 | + |
| 114 | + Ok(results) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +mod tests { |
| 120 | + use super::*; |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_parse_json_response() { |
| 124 | + let json = br#"{ |
| 125 | + "total": 1, |
| 126 | + "data": [ |
| 127 | + { |
| 128 | + "name": "Test Video", |
| 129 | + "url": "https://video.example.org/videos/watch/abc123", |
| 130 | + "description": "A test video description" |
| 131 | + } |
| 132 | + ] |
| 133 | + }"#; |
| 134 | + |
| 135 | + let results = SepiaSearch::parse_json_response(json).unwrap(); |
| 136 | + assert_eq!(results.len(), 1); |
| 137 | + assert_eq!(results[0].1.title, "Test Video"); |
| 138 | + assert_eq!( |
| 139 | + results[0].1.url, |
| 140 | + "https://video.example.org/videos/watch/abc123" |
| 141 | + ); |
| 142 | + assert_eq!(results[0].1.description, "A test video description"); |
| 143 | + assert_eq!(results[0].1.engine, vec!["sepiasearch"]); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn test_parse_json_response_no_description() { |
| 148 | + let json = br#"{ |
| 149 | + "total": 1, |
| 150 | + "data": [ |
| 151 | + { |
| 152 | + "name": "No Desc Video", |
| 153 | + "url": "https://video.example.org/videos/watch/def456" |
| 154 | + } |
| 155 | + ] |
| 156 | + }"#; |
| 157 | + |
| 158 | + let results = SepiaSearch::parse_json_response(json).unwrap(); |
| 159 | + assert_eq!(results.len(), 1); |
| 160 | + assert_eq!(results[0].1.description, ""); |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn test_parse_json_response_empty_results() { |
| 165 | + let json = br#"{ |
| 166 | + "total": 0, |
| 167 | + "data": [] |
| 168 | + }"#; |
| 169 | + |
| 170 | + let results = SepiaSearch::parse_json_response(json).unwrap(); |
| 171 | + assert!(results.is_empty()); |
| 172 | + } |
| 173 | +} |
0 commit comments