1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#![allow(unused_parens)]
extern crate bytes;
extern crate chrono;
extern crate rss;
extern crate thiserror;
extern crate urlencoding;

use std::collections::BTreeMap;
use std::convert::TryFrom;

use bytes::Buf;
use bytes::Bytes;
use rss::Channel;
use rss::Item;

mod error;
pub use error::Error;
mod torrent;
pub use torrent::Torrent;

#[derive(Clone)]
pub struct Client {
	http: reqwest::Client,
	base_url: String,
	apikey: String
}

impl Client {
	pub fn new(base_url: impl ToString, apikey: impl ToString) -> Result<Self, reqwest::Error> {
		let this = Self{
			http: reqwest::Client::builder()
				.gzip(true)
				.build()?,
			base_url: base_url.to_string(),
			apikey: apikey.to_string()
		};
		// TODO:  Check caps
		Ok(this)
	}

	async fn get(&self, t: &str, mut qparams: BTreeMap<String, String>) -> Result<Bytes, reqwest::Error> {
		qparams.insert("apikey".to_string(), self.apikey.clone());
		let url = format!("{}?t={}&{}", self.base_url, &t, qparams.into_iter()
			.map(|(k, v)| format!("{}={}", urlencoding::encode(&k), urlencoding::encode(&v)))
			.collect::<Vec<String>>()
			.join("&")
		);
		Ok(self.http.get(&url).send().await?.error_for_status()?.bytes().await?)
	}

	async fn get_items(&self, t: &str, qparams: BTreeMap<String, String>) -> Result<Vec<Item>, Error> {
		let bytes = self.get(t, qparams).await?;
		let channel = tokio::task::spawn_blocking(move || Channel::read_from(bytes.bytes())).await??;
		Ok(channel.into_items())
	}

	pub async fn tvsearch(&self, q: Option<&str>, rid: Option<u32>, tvdbid: Option<u32>, tvmazeid: Option<u32>, season: Option<u16>, ep: Option<u16>) -> Result<Vec<Result<Torrent, Error>>, Error> {
		let mut qparams = BTreeMap::<String, String>::new();
		if let Some(v) = q {
			qparams.insert("q".to_string(), v.to_string());
		}
		if let Some(v) = rid {
			qparams.insert("rid".to_string(), v.to_string());
		}
		if let Some(v) = tvdbid {
			qparams.insert("tvdbid".to_string(), v.to_string());
		}
		if let Some(v) = tvmazeid {
			qparams.insert("tvmazeid".to_string(), v.to_string());
		}
		if let Some(v) = season {
			qparams.insert("season".to_string(), v.to_string());
		}
		if let Some(v) = ep {
			qparams.insert("ep".to_string(), v.to_string());
		}
		let items = self.get_items("tvsearch", qparams).await?;
		Ok(items.into_iter().map(|v| Torrent::try_from(v)).collect::<Vec<_>>())
	}

	pub async fn moviesearch(&self, q: Option<&str>, imdbid: Option<u64>) -> Result<Vec<Result<Torrent, Error>>, Error> {
		let mut qparams = BTreeMap::<String, String>::new();
		if let Some(v) = q {
			qparams.insert("q".to_string(), v.to_string());
		}
		if let Some(v) = imdbid {
			qparams.insert("imdbid".to_string(), v.to_string());
		}
		let items = self.get_items("movies", qparams).await?;
		Ok(items.into_iter().map(|v| Torrent::try_from(v)).collect::<Vec<_>>())
	}
}