quick & dirty opds implementation

This commit is contained in:
Sebastian Hugentobler 2024-05-09 12:23:26 +02:00
parent 12341d01a6
commit cccd3cbdc9
Signed by: shu
GPG key ID: BB32CF3CA052C2F0
3 changed files with 319 additions and 18 deletions

View file

@ -1,3 +1,4 @@
use calibre_db::data::{author::Author as DbAuthor, series::Series};
use serde::Serialize;
use time::OffsetDateTime;
@ -14,7 +15,8 @@ pub struct Entry {
pub id: String,
#[serde(with = "time::serde::rfc3339")]
pub updated: OffsetDateTime,
pub content: Content,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<Content>,
pub author: Option<Author>,
#[serde(rename = "link")]
pub links: Vec<Link>,
@ -41,20 +43,64 @@ impl From<Book> for Entry {
.collect();
links.append(&mut format_links);
let content = value.description.map(|desc| Content {
media_type: MediaType::Html,
content: desc,
});
Self {
title: value.title.clone(),
id: format!("urn:uuid:{}", value.uuid),
updated: value.last_modified,
content: Content {
media_type: MediaType::Html,
content: value.description.clone().unwrap_or("".to_string()),
},
content,
author: Some(author),
links,
}
}
}
impl From<DbAuthor> for Entry {
fn from(value: DbAuthor) -> Self {
let links = vec![Link {
href: format!("/opds/authors/{}", value.id),
media_type: MediaType::Acquisition,
rel: Relation::Subsection,
title: None,
count: None,
}];
Self {
title: value.name.clone(),
id: format!("rusty:authors:{}", value.id),
updated: OffsetDateTime::now_utc(),
content: None,
author: None,
links,
}
}
}
impl From<Series> for Entry {
fn from(value: Series) -> Self {
let links = vec![Link {
href: format!("/opds/series/{}", value.id),
media_type: MediaType::Acquisition,
rel: Relation::Subsection,
title: None,
count: None,
}];
Self {
title: value.name.clone(),
id: format!("rusty:series:{}", value.id),
updated: OffsetDateTime::now_utc(),
content: None,
author: None,
links,
}
}
}
#[cfg(test)]
mod tests {
use quick_xml::se::to_string;
@ -69,10 +115,10 @@ mod tests {
title: "Authors".to_string(),
id: "rust:authors".to_string(),
updated: datetime!(2024-05-06 19:14:54 UTC),
content: Content {
content: Some(Content {
media_type: MediaType::Text,
content: "All authors".to_string(),
},
}),
author: None,
links: vec![
Link {