magneto 0.2.4

A torrent searching library
Documentation
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! `Magneto` is a library for searching torrents across multiple providers.
//! It provides a unified interface for querying torrent metadata and integrating
//! custom providers.
//!
//! ## Features
//! - Fully async-powered using `reqwest` and `tokio`.
//! - Query multiple torrent search providers simultaneously.
//! - Retrieve torrent results in a unified format.
//! - Add custom providers with minimal effort.
//!
//! ## Supported providers
//! - Knaben: A multi search archiver, acting as a cached proxy towards multiple different trackers.
//! - PirateBay: The galaxy’s most resilient Public BitTorrent site.
//! - YTS: A public torrent site specialising in HD movies of small size.
//!
//! ## Usage
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! tokio = { version = "1", features = ["full"] }
//! magneto = "0.2"
//! ```
//!
//! Then:
//!
//! ```rust
//! use magneto::{Magneto, SearchRequest};
//!
//! #[tokio::main]
//! async fn main() {
//!     let magneto = Magneto::new();
//!
//!     let request = SearchRequest::new("Ubuntu");
//!     let results = magneto.search(request).await.unwrap();
//!
//!     for torrent in results {
//!         println!(
//!             "found: {} from {} with magnet link {} (seeders: {}, peers: {})",
//!             torrent.name,
//!             torrent.provider,
//!             torrent.magnet_link,
//!             torrent.seeders,
//!             torrent.peers,
//!         );
//!     }
//! }
//! ```
//!
//! ### Specifying search providers
//!
//! ```no_run
//! use magneto::{Magneto, Knaben, PirateBay, Yts};
//!
//! // By default, all built-in providers are used (Knaben, PirateBay, Yts)
//! let magneto = Magneto::new();
//!
//! // You can specify which providers to use like this
//! let magneto =
//!     Magneto::with_providers(vec![Box::new(Knaben::new()), Box::new(PirateBay::new())]);
//!
//! // Or like this
//! let magneto = Magneto::default()
//!     .add_provider(Box::new(Knaben::new()))
//!     .add_provider(Box::new(Yts::new()));
//! ```
//!
//! ### Search request parameters
//!
//! ```no_run
//! use magneto::{Category, SearchRequest, OrderBy};
//!
//! // You can add categories to filter your search results
//! let request = SearchRequest::new("Ubuntu")
//!     .add_category(Category::Software)
//!     .add_categories(vec![Category::Audio, Category::Movies]);
//!
//! // Or initialize the request like this for more customization
//! let request = SearchRequest {
//!     query: "Debian",
//!     order_by: OrderBy::Seeders,
//!     categories: vec![Category::Software],
//!     number_of_results: 10,
//! };
//! ```
//!
//! ### Add a custom provider
//!
//! ```no_run
//! use magneto::{
//!     async_trait, Client, ClientError, Magneto, Request, SearchProvider, SearchRequest, Torrent,
//! };
//!
//! struct CustomProvider;
//!
//! #[async_trait]
//! impl SearchProvider for CustomProvider {
//!     fn build_request(
//!         &self,
//!         client: &Client,
//!         request: SearchRequest<'_>,
//!     ) -> Result<Request, ClientError> {
//!         // Convert SearchRequest parameters to a reqwest::Request
//!         unimplemented!();
//!     }
//!
//!     fn parse_response(&self, response: &str) -> Result<Vec<Torrent>, ClientError> {
//!         // Parse the raw reponse into Vec<Torrent>
//!         unimplemented!();
//!     }
//!
//!
//!     fn id(&self) -> String {
//!         // Return a unique id, built-in providers use the provider url
//!         unimplemented!();
//!     }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//!     let custom_provider = CustomProvider{};
//!     let magneto = Magneto::new().add_provider(Box::new(custom_provider));
//! }
//! ```

pub mod errors;
pub mod search_providers;

use core::fmt;

// Re-exports from reqwest
pub use reqwest::{Client, Request};

// Re-export async_trait;
pub use async_trait::async_trait;

use log::debug;
use serde::{Deserialize, Serialize};

pub use errors::ClientError;
pub use search_providers::{Knaben, PirateBay, SearchProvider, Yts};

/// Represents metadata for a torrent returned by a search provider.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Torrent {
    /// The name of the torrent.
    pub name: String,

    /// The magnet link for downloading the torrent.
    pub magnet_link: String,

    /// The number of seeders available.
    pub seeders: u32,

    /// The number of peers available.
    pub peers: u32,

    /// The size of the torrent in bytes.
    pub size_bytes: u64,

    /// The identifier of the provider that returned this torrent.
    pub provider: String,
}

/// Enum specifying the different categories available for torrents.
#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
pub enum Category {
    /// Represents the category for movies.
    Movies,

    /// Represents the category for TV shows.
    TvShows,

    /// Represents the category for games.
    Games,

    /// Represents the category for software.
    Software,

    /// Represents the category for audio.
    Audio,

    /// Represents the category for anime.
    Anime,

    /// Represents the category for adult content.
    Xxx,
}

/// Enum specifying the order by which search results are sorted.
///
/// Implements fmt::Display
#[derive(Serialize, Debug, Clone)]
pub enum OrderBy {
    /// Sort results by the number of seeders.
    Seeders,

    /// Sort results by the number of peers.
    Peers,
}

impl fmt::Display for OrderBy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            OrderBy::Seeders => write!(f, "seeders"),
            OrderBy::Peers => write!(f, "peers"),
        }
    }
}
/// Represents a search request to be sent to torrent providers.
#[derive(Serialize, Debug, Clone)]
pub struct SearchRequest<'a> {
    /// The query string to search for.
    pub query: &'a str,

    /// The order by which results are sorted.
    pub order_by: OrderBy,

    /// Categories to filter results by. Empty means all categories are searched.
    pub categories: Vec<Category>,

    /// The number of results to retrieve.
    pub number_of_results: usize,
}

impl<'a> SearchRequest<'a> {
    /// Creates a new `SearchRequest` with the specified query.
    ///
    /// Remaining fields get the following default values:
    /// - `order_by`: `OrderBy::Seeders`
    /// - `categories`: An empty `Vec<Category>`
    /// - `number_of_results`: `50`
    ///
    /// # Parameters
    /// - `query`: The search term or phrase.
    ///
    /// # Returns
    /// - A new `SearchRequest` instance.
    ///
    /// # Example
    /// ```rust
    /// use magneto::SearchRequest;
    ///
    /// let request = SearchRequest::new("example query");
    /// ```
    pub fn new(query: &'a str) -> Self {
        Self {
            query,
            order_by: OrderBy::Seeders,
            categories: vec![],
            number_of_results: 50,
        }
    }

    /// Adds a single category to the `SearchRequest`.
    ///
    /// This method consumes the current instance and returns a new `SearchRequest`
    /// with the added category.
    ///
    /// # Parameters
    /// - `category`: The `Category` to add.
    ///
    /// # Returns
    /// - `Self`: A new `SearchRequest` instance with the updated category.
    ///
    /// # Example
    /// ```rust
    /// use magneto::{Category, SearchRequest};
    ///
    /// let request = SearchRequest::new("example query")
    ///     .add_category(Category::Movies)
    ///     .add_category(Category::Movies); // duplicates are filtered
    /// assert_eq!(request.categories, vec![Category::Movies]);
    /// ```
    pub fn add_category(mut self, category: Category) -> Self {
        if !self.categories.contains(&category) {
            self.categories.push(category);
        }
        self
    }

    /// Adds multiple categories to the `SearchRequest`.
    ///
    /// This method consumes the current instance and returns a new `SearchRequest`
    /// with the added categories.
    ///
    /// # Parameters
    /// - `categories`: A vector of `Category` values to add.
    ///
    /// # Returns
    /// - `Self`: A new `SearchRequest` instance with the updated categories.
    ///
    /// # Example
    /// ```rust
    /// use magneto::{Category, SearchRequest};
    ///
    /// let request = SearchRequest::new("example query").add_categories(vec![
    ///     Category::Movies,
    ///     Category::Anime,
    ///     Category::Anime, // duplicates are filtered
    /// ]);
    /// assert_eq!(request.categories, vec![Category::Movies, Category::Anime]);
    /// ```
    pub fn add_categories(mut self, categories: Vec<Category>) -> Self {
        for category in categories {
            if !self.categories.contains(&category) {
                self.categories.push(category);
            }
        }
        self
    }
}

/// The main interface for managing and querying torrent providers.
///
/// `Magneto` manages a collection of torrent search providers and allows
/// querying them simultaneously. It supports adding custom providers, querying
/// specific providers, and retrieving results in a unified format.
#[derive(Default)]
pub struct Magneto {
    pub active_providers: Vec<Box<dyn SearchProvider>>,
}

impl Magneto {
    /// Creates a new `Magneto` instance with default providers.
    ///
    /// The default providers include:
    /// - `Knaben`
    /// - `PirateBay`
    /// - `Yts`
    ///
    /// # Returns
    /// - A new `Magneto` instance with default providers.
    pub fn new() -> Self {
        let providers: Vec<Box<dyn SearchProvider>> = vec![
            Box::new(Knaben::new()),
            Box::new(PirateBay::new()),
            Box::new(Yts::new()),
        ];

        Self {
            active_providers: providers,
        }
    }

    /// Creates a new `Magneto` instance with the specified providers.
    ///
    /// # Parameters
    /// - `providers`: A vector of custom providers implementing the `SearchProvider` trait.
    ///
    /// # Returns
    /// - A new `Magneto` instance with unique providers.
    ///
    /// # Notes
    /// Duplicate providers are filtered based on their `id()` method to avoid duplicate searches.
    ///
    /// # Examples
    /// ```
    /// use magneto::{search_providers::{Knaben, SearchProvider}, Magneto};
    ///
    /// let providers: Vec<Box<dyn SearchProvider>> =
    ///     vec![Box::new(Knaben::new()), Box::new(Knaben::new())];
    /// let magneto = Magneto::with_providers(providers);
    ///
    /// // Duplicates are removed
    /// assert_eq!(magneto.active_providers.len(), 1);
    /// ```
    pub fn with_providers(providers: Vec<Box<dyn SearchProvider>>) -> Self {
        providers
            .into_iter()
            .fold(Self::default(), |acc, provider| acc.add_provider(provider))
    }

    /// Adds a provider to the list of active providers.
    ///
    /// This method consumes the current `Magneto` instance and returns a new instance
    /// with the added provider. If a provider with the same ID already exists, it will
    /// not be added again.
    ///
    /// # Parameters
    /// - `provider`: A provider implementing the `SearchProvider` trait.
    ///
    /// # Returns
    /// - A new `Magneto` instance with the updated list of providers.
    ///
    ///
    /// # Examples
    /// ```
    /// use magneto::{search_providers::Yts, Magneto};
    ///
    /// let magneto = Magneto::default()
    ///     .add_provider(Box::new(Yts::new()))
    ///     .add_provider(Box::new(Yts::new()));
    ///
    /// // Duplicates are removed
    /// assert_eq!(magneto.active_providers.len(), 1);
    /// ```
    pub fn add_provider(mut self, provider: Box<dyn SearchProvider>) -> Self {
        let provider_id = provider.id();

        if self
            .active_providers
            .iter()
            .any(|existing| existing.id() == provider_id)
        {
            debug!(
                "provider '{}' already exists, skipping addition",
                provider_id
            );
            return self;
        }

        self.active_providers.push(provider);
        self
    }

    /// Executes a search query across all active providers in sequence and aggregates the results.
    ///
    /// # Parameters
    /// - `request`: The `SearchRequest` specifying the search parameters.
    ///
    /// # Returns
    /// - `Ok(Vec<Torrent>)`: A list of torrents returned by all active providers.
    /// - `Err(ClientError)`: An error if the query fails for any provider.
    ///
    /// # Examples
    /// ```no_run
    /// use magneto::{Magneto, SearchRequest};
    ///
    /// let magneto = Magneto::new();
    /// let request = SearchRequest::new("Ubuntu");
    ///
    /// // Search default providers for "Ubuntu" and returns a vector of torrent metadata
    /// let torrents = magneto.search(request);
    /// ```
    pub async fn search(&self, request: SearchRequest<'_>) -> Result<Vec<Torrent>, ClientError> {
        let client = Client::new();
        let mut results = Vec::new();

        for provider in &self.active_providers {
            match provider.send_request(&client, request.clone()).await {
                Ok(mut torrents) => results.append(&mut torrents),
                Err(e) => return Err(e),
            }
        }

        results.sort_by(|a, b| match request.order_by {
            OrderBy::Seeders => b.seeders.cmp(&a.seeders),
            OrderBy::Peers => b.peers.cmp(&a.peers),
        });

        results.truncate(request.number_of_results);

        Ok(results)
    }
}