pinakes-core: improve media management features; various configuration improvements
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I2d1f04f13970d21c36067f30bc04a9176a6a6964
This commit is contained in:
parent
cfdc3d0622
commit
e02c15490e
31 changed files with 1167 additions and 197 deletions
|
|
@ -26,8 +26,9 @@ impl BookEnricher {
|
|||
pub async fn try_openlibrary(&self, isbn: &str) -> Result<Option<ExternalMetadata>> {
|
||||
match self.openlibrary.fetch_by_isbn(isbn).await {
|
||||
Ok(book) => {
|
||||
let metadata_json = serde_json::to_string(&book)
|
||||
.map_err(|e| PinakesError::External(format!("Failed to serialize metadata: {}", e)))?;
|
||||
let metadata_json = serde_json::to_string(&book).map_err(|e| {
|
||||
PinakesError::External(format!("Failed to serialize metadata: {}", e))
|
||||
})?;
|
||||
|
||||
Ok(Some(ExternalMetadata {
|
||||
id: Uuid::new_v4(),
|
||||
|
|
@ -48,8 +49,9 @@ impl BookEnricher {
|
|||
match self.googlebooks.fetch_by_isbn(isbn).await {
|
||||
Ok(books) if !books.is_empty() => {
|
||||
let book = &books[0];
|
||||
let metadata_json = serde_json::to_string(book)
|
||||
.map_err(|e| PinakesError::External(format!("Failed to serialize metadata: {}", e)))?;
|
||||
let metadata_json = serde_json::to_string(book).map_err(|e| {
|
||||
PinakesError::External(format!("Failed to serialize metadata: {}", e))
|
||||
})?;
|
||||
|
||||
Ok(Some(ExternalMetadata {
|
||||
id: Uuid::new_v4(),
|
||||
|
|
@ -75,8 +77,9 @@ impl BookEnricher {
|
|||
if let Ok(results) = self.openlibrary.search(title, author).await
|
||||
&& let Some(result) = results.first()
|
||||
{
|
||||
let metadata_json = serde_json::to_string(result)
|
||||
.map_err(|e| PinakesError::External(format!("Failed to serialize metadata: {}", e)))?;
|
||||
let metadata_json = serde_json::to_string(result).map_err(|e| {
|
||||
PinakesError::External(format!("Failed to serialize metadata: {}", e))
|
||||
})?;
|
||||
|
||||
return Ok(Some(ExternalMetadata {
|
||||
id: Uuid::new_v4(),
|
||||
|
|
@ -93,8 +96,9 @@ impl BookEnricher {
|
|||
if let Ok(results) = self.googlebooks.search(title, author).await
|
||||
&& let Some(book) = results.first()
|
||||
{
|
||||
let metadata_json = serde_json::to_string(book)
|
||||
.map_err(|e| PinakesError::External(format!("Failed to serialize metadata: {}", e)))?;
|
||||
let metadata_json = serde_json::to_string(book).map_err(|e| {
|
||||
PinakesError::External(format!("Failed to serialize metadata: {}", e))
|
||||
})?;
|
||||
|
||||
return Ok(Some(ExternalMetadata {
|
||||
id: Uuid::new_v4(),
|
||||
|
|
|
|||
|
|
@ -31,12 +31,10 @@ impl GoogleBooksClient {
|
|||
url.push_str(&format!("&key={}", key));
|
||||
}
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.get(&url)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| PinakesError::External(format!("Google Books request failed: {}", e)))?;
|
||||
let response =
|
||||
self.client.get(&url).send().await.map_err(|e| {
|
||||
PinakesError::External(format!("Google Books request failed: {}", e))
|
||||
})?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(PinakesError::External(format!(
|
||||
|
|
@ -45,10 +43,9 @@ impl GoogleBooksClient {
|
|||
)));
|
||||
}
|
||||
|
||||
let volumes: GoogleBooksResponse = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| PinakesError::External(format!("Failed to parse Google Books response: {}", e)))?;
|
||||
let volumes: GoogleBooksResponse = response.json().await.map_err(|e| {
|
||||
PinakesError::External(format!("Failed to parse Google Books response: {}", e))
|
||||
})?;
|
||||
|
||||
Ok(volumes.items)
|
||||
}
|
||||
|
|
@ -70,12 +67,10 @@ impl GoogleBooksClient {
|
|||
url.push_str(&format!("&key={}", key));
|
||||
}
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.get(&url)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| PinakesError::External(format!("Google Books search failed: {}", e)))?;
|
||||
let response =
|
||||
self.client.get(&url).send().await.map_err(|e| {
|
||||
PinakesError::External(format!("Google Books search failed: {}", e))
|
||||
})?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(PinakesError::External(format!(
|
||||
|
|
@ -84,10 +79,9 @@ impl GoogleBooksClient {
|
|||
)));
|
||||
}
|
||||
|
||||
let volumes: GoogleBooksResponse = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| PinakesError::External(format!("Failed to parse search results: {}", e)))?;
|
||||
let volumes: GoogleBooksResponse = response.json().await.map_err(|e| {
|
||||
PinakesError::External(format!("Failed to parse search results: {}", e))
|
||||
})?;
|
||||
|
||||
Ok(volumes.items)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,12 +30,10 @@ impl OpenLibraryClient {
|
|||
pub async fn fetch_by_isbn(&self, isbn: &str) -> Result<OpenLibraryBook> {
|
||||
let url = format!("{}/isbn/{}.json", self.base_url, isbn);
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.get(&url)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| PinakesError::External(format!("OpenLibrary request failed: {}", e)))?;
|
||||
let response =
|
||||
self.client.get(&url).send().await.map_err(|e| {
|
||||
PinakesError::External(format!("OpenLibrary request failed: {}", e))
|
||||
})?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(PinakesError::External(format!(
|
||||
|
|
@ -44,15 +42,22 @@ impl OpenLibraryClient {
|
|||
)));
|
||||
}
|
||||
|
||||
response
|
||||
.json::<OpenLibraryBook>()
|
||||
.await
|
||||
.map_err(|e| PinakesError::External(format!("Failed to parse OpenLibrary response: {}", e)))
|
||||
response.json::<OpenLibraryBook>().await.map_err(|e| {
|
||||
PinakesError::External(format!("Failed to parse OpenLibrary response: {}", e))
|
||||
})
|
||||
}
|
||||
|
||||
/// Search for books by title and author
|
||||
pub async fn search(&self, title: &str, author: Option<&str>) -> Result<Vec<OpenLibrarySearchResult>> {
|
||||
let mut url = format!("{}/search.json?title={}", self.base_url, urlencoding::encode(title));
|
||||
pub async fn search(
|
||||
&self,
|
||||
title: &str,
|
||||
author: Option<&str>,
|
||||
) -> Result<Vec<OpenLibrarySearchResult>> {
|
||||
let mut url = format!(
|
||||
"{}/search.json?title={}",
|
||||
self.base_url,
|
||||
urlencoding::encode(title)
|
||||
);
|
||||
|
||||
if let Some(author) = author {
|
||||
url.push_str(&format!("&author={}", urlencoding::encode(author)));
|
||||
|
|
@ -74,10 +79,9 @@ impl OpenLibraryClient {
|
|||
)));
|
||||
}
|
||||
|
||||
let search_response: OpenLibrarySearchResponse = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| PinakesError::External(format!("Failed to parse search results: {}", e)))?;
|
||||
let search_response: OpenLibrarySearchResponse = response.json().await.map_err(|e| {
|
||||
PinakesError::External(format!("Failed to parse search results: {}", e))
|
||||
})?;
|
||||
|
||||
Ok(search_response.docs)
|
||||
}
|
||||
|
|
@ -153,9 +157,9 @@ impl OpenLibraryClient {
|
|||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum CoverSize {
|
||||
Small, // 256x256
|
||||
Medium, // 600x800
|
||||
Large, // Original
|
||||
Small, // 256x256
|
||||
Medium, // 600x800
|
||||
Large, // Original
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
|
@ -277,7 +281,8 @@ mod tests {
|
|||
let string_desc: StringOrObject = serde_json::from_str(r#""Simple description""#).unwrap();
|
||||
assert_eq!(string_desc.as_str(), "Simple description");
|
||||
|
||||
let object_desc: StringOrObject = serde_json::from_str(r#"{"value": "Object description"}"#).unwrap();
|
||||
let object_desc: StringOrObject =
|
||||
serde_json::from_str(r#"{"value": "Object description"}"#).unwrap();
|
||||
assert_eq!(object_desc.as_str(), "Object description");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue