TorznabClient::search now returns a Vec<TorrentResult>

This commit is contained in:
seanomik 2022-06-20 22:56:55 -04:00 committed by SeanOMik
parent a93fef3ac5
commit 25bf4db796
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
3 changed files with 27 additions and 19 deletions

View File

@ -2,6 +2,7 @@ mod config;
mod torznab; mod torznab;
use config::Config; use config::Config;
use lava_torrent::bencode::BencodeElem;
use tracing::{info, Level, debug}; use tracing::{info, Level, debug};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -64,8 +65,6 @@ async fn main() {
let torrent_files = read_torrents(config.torrents_path()).unwrap(); let torrent_files = read_torrents(config.torrents_path()).unwrap();
info!("Found {} torrents", torrent_files.len()); info!("Found {} torrents", torrent_files.len());
//panic!("rhfhujergfre");
// Convert the indexers to be async friendly. // Convert the indexers to be async friendly.
let mut indexers = indexers.iter() let mut indexers = indexers.iter()
.map(|indexer| Arc::new(RwLock::new(indexer.clone()))) .map(|indexer| Arc::new(RwLock::new(indexer.clone())))
@ -74,8 +73,19 @@ async fn main() {
let mut indexer_handles = vec![]; let mut indexer_handles = vec![];
for torrent_path in torrent_files.iter() { for torrent_path in torrent_files.iter() {
let torrent = Arc::new(Torrent::read_from_file(torrent_path).unwrap()); let torrent = Torrent::read_from_file(torrent_path).unwrap();
info!("{}:", torrent.name);
// To check for private torrents
/* if let Some(extra_info) = &torrent.extra_info_fields {
if let Some(BencodeElem::Integer(is_private)) = extra_info.get("private") {
if *is_private == 1 {
}
}
} */
let torrent = Arc::new(torrent);
//info!("{}:", torrent.name);
for indexer in indexers.iter() { for indexer in indexers.iter() {
let mut indexer = Arc::clone(indexer); let mut indexer = Arc::clone(indexer);
@ -87,7 +97,9 @@ async fn main() {
let generic = GenericSearchParametersBuilder::new() let generic = GenericSearchParametersBuilder::new()
.query(torrent.name.clone()) .query(torrent.name.clone())
.build(); .build();
client.search(SearchFunction::Search, generic).await.unwrap(); let results = client.search(SearchFunction::Search, generic).await.unwrap();
//println!("Results: {:?}", results);
}, },
None => { None => {
panic!("idfk"); panic!("idfk");

View File

@ -81,7 +81,7 @@ impl TorznabClient {
} }
/// Search for torrents. /// Search for torrents.
pub async fn search(&self, func: SearchFunction, generic_params: GenericSearchParameters) -> Result<(), ClientError> { pub async fn search(&self, func: SearchFunction, generic_params: GenericSearchParameters) -> Result<Vec<TorrentResult>, ClientError> {
let param_str = format!("{}{}", func.to_params(), generic_params.to_params()); let param_str = format!("{}{}", func.to_params(), generic_params.to_params());
let bytes = self.request(param_str).await?; let bytes = self.request(param_str).await?;
@ -91,13 +91,9 @@ impl TorznabClient {
let items = channel.into_items(); let items = channel.into_items();
let torrents: Vec<TorrentResult> = items.iter() let torrents: Vec<TorrentResult> = items.iter()
.map(TorrentResult::from_item) .map(|i| TorrentResult::from_item(i.clone()))
.collect::<Result<Vec<TorrentResult>, super::ResultError>>()?; .collect::<Result<Vec<TorrentResult>, super::ResultError>>()?;
debug!("Found results: {:?}", torrents); Ok(torrents)
//Torrent::from
Ok(())
} }
} }

View File

@ -7,23 +7,23 @@ pub enum ResultError {
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct TorrentResult<'a> { pub struct TorrentResult {
name: &'a str, name: String,
link: &'a str, link: String,
/* size: u64, /* size: u64,
categories: Vec<u32>, */ categories: Vec<u32>, */
} }
impl<'a> TorrentResult<'a> { impl TorrentResult {
pub fn from_item(item: &'a Item) -> Result<Self, ResultError> { pub fn from_item(item: Item) -> Result<Self, ResultError> {
let name = item.title().ok_or(ResultError::MissingTitle)?; let name = item.title().ok_or(ResultError::MissingTitle)?;
let link = item.link().ok_or(ResultError::MissingLink)?; let link = item.link().ok_or(ResultError::MissingLink)?;
/* let size = item.enclosure().map(|e| e.length().parse::<u64>()); /* let size = item.enclosure().map(|e| e.length().parse::<u64>());
let categories = item.categories().ok_or(ResultError::MissingTitle)?; */ let categories = item.categories().ok_or(ResultError::MissingTitle)?; */
Ok(TorrentResult { Ok(TorrentResult {
name, name: String::from(name.clone()),
link, link: String::from(link),
/* size, /* size,
categories, */ categories, */
}) })