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
/// Struct that represents a torrent and contains some of its basic information.
#[derive(Debug)]
pub struct Torrent {
    /// The category of the torrent.
    pub category: Category,
    /// The name of the torrent.
    pub name: String,
    /// Links for the torrent (magnet and .torrent) as a pair. If you want to
    /// get a url to the .torrent file or the magnet please use the
    /// [`torrent_file`] or the [`magnet_link`] methods.
    ///
    /// [`torrent_file`]: #method.torrent_file
    /// [`magnet_link`]: #method.magnet_link
    pub links: (Option<String>, Option<String>),
    /// Total size of the torrent's files in bytes.
    pub size: u64,
    /// Date of pubblication of the torrent as unix timestamp.
    pub date: u64,
    /// Number of seeders.
    pub seeders: u32,
    /// Number of leechers.
    pub leechers: u32,
    /// Number of completed downloads.
    pub completed_downloads: u32,
}

impl Torrent {
    /// Extract `.torrent`'s file url as `String`.
    pub fn torrent_file(&self) -> Option<String> {
        let mut result = String::from("https://nyaa.si");
        let (first, second) = &self.links;

        if let Some(torrent_file) = first {
            if torrent_file.starts_with("/download") {
                result.push_str(torrent_file);
                return Some(result.clone());
            }
        }

        if let Some(torrent_file) = second {
            if torrent_file.starts_with("/download") {
                result.push_str(torrent_file);
                return Some(result.clone());
            }
        }

        None
    }
    
    /// Extract magnet link as `String`.
    pub fn magnet_link(&self) -> Option<String> {
        let (first, second) = &self.links;

        if let Some(magnet) = first {
            if magnet.starts_with("magnet") {
                return Some(magnet.clone());
            }
        }

        if let Some(magnet) = second {
            if magnet.starts_with("magnet") {
                return Some(magnet.clone());
            }
        }

        None
    }
}

/// Enum that encodes a torrent's category.
#[derive(Debug)]
pub enum Category {
    Anime(Anime),
    Audio(Audio),
    Literature(Literature),
    LiveAction(LiveAction),
    Pictures(Pictures),
    Software(Software),
}

/// Enum that encodes variants of anime torrents.
#[derive(Debug)]
pub enum Anime {
    AnimeMusicVideo,
    EnglishTranslated,
    NonEnglishTranslated,
    Raw,
}

/// Enum that encodes variants of audio torrents.
#[derive(Debug)]
pub enum Audio {
    Lossless,
    Lossy,
}

/// Enum that encodes variants of literature torrents.
#[derive(Debug)]
pub enum Literature {
    EnglishTranslated,
    NonEnglishTranslated,
    Raw,
}

/// Enum that encodes variants of live action torrents.
#[derive(Debug)]
pub enum LiveAction {
    EnglishTranslated,
    IdolPromotionalVideo,
    NonEnglishTranslated,
    Raw,
}

/// Enum that encodes variants of pictures torrents.
#[derive(Debug)]
pub enum Pictures {
    Graphics,
    Photos,
}

/// Enum that encodes variants of software torrents.
#[derive(Debug)]
pub enum Software {
    Applications,
    Games,
}

/// Emun that encodes possible errors
#[derive(Debug)]
pub enum Error {
    /// Error with the request e.g. the server do not respond or you don't have
    /// access to it.
    Request,
    /// Scraping error. Since all the data is obtained via scraping of html
    /// pages if somehting in how Nyaa.si's pages are generated is changed
    /// searching for torrents may fail. This should hopefully never happen but
    /// I highly suggest you to always check for scaping errors and, if you ever
    /// record one, to report it as an issue on [GitHub] so that the library can
    /// be updated.
    ///
    /// [GitHub]: https://github.com/grastello/nyaadesu
    Scraping,
}