Skip to main content

nyaa_si/
nyaa.rs

1use std::fmt::Display;
2use tokio::task::spawn_blocking;
3use tracing::{event, Level};
4
5use crate::{
6    category::Category, client::Client, error::Result, extractor::extract,
7    model::Torrent, query::Query,
8};
9
10#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
11pub enum NyaaCategory {
12    All,
13    Anime,
14    AnimeMusicVideo,
15    AnimeEnglishTranslated,
16    AnimeNonEnglishTranslated,
17    AnimeRaw,
18    Audio,
19    AudioLossless,
20    AudioLossy,
21    Literature,
22    LiteratureEnglishTranslated,
23    LiteratureNonEnglishTranslated,
24    LiteratureRaw,
25    LiveAction,
26    LiveActionEnglishTranslated,
27    LiveActionIdol,
28    LiveActionNonEnglishTranslated,
29    LiveActionRaw,
30    Pictures,
31    PicturesGraphics,
32    PicturesPhotos,
33    Software,
34    SoftwareApplications,
35    SoftwareGames,
36}
37
38impl Display for NyaaCategory {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        match self {
41            Self::All => write!(f, "0_0"),
42            Self::Anime => write!(f, "1_0"),
43            Self::AnimeMusicVideo => write!(f, "1_1"),
44            Self::AnimeEnglishTranslated => write!(f, "1_2"),
45            Self::AnimeNonEnglishTranslated => write!(f, "1_3"),
46            Self::AnimeRaw => write!(f, "1_4"),
47            Self::Audio => write!(f, "2_0"),
48            Self::AudioLossless => write!(f, "2_1"),
49            Self::AudioLossy => write!(f, "2_2"),
50            Self::Literature => write!(f, "3_0"),
51            Self::LiteratureEnglishTranslated => write!(f, "3_1"),
52            Self::LiteratureNonEnglishTranslated => write!(f, "3_2"),
53            Self::LiteratureRaw => write!(f, "3_3"),
54            Self::LiveAction => write!(f, "4_0"),
55            Self::LiveActionEnglishTranslated => write!(f, "4_1"),
56            Self::LiveActionIdol => write!(f, "4_2"),
57            Self::LiveActionNonEnglishTranslated => write!(f, "4_3"),
58            Self::LiveActionRaw => write!(f, "4_4"),
59            Self::Pictures => write!(f, "5_0"),
60            Self::PicturesGraphics => write!(f, "5_1"),
61            Self::PicturesPhotos => write!(f, "5_2"),
62            Self::Software => write!(f, "6_0"),
63            Self::SoftwareApplications => write!(f, "6_1"),
64            Self::SoftwareGames => write!(f, "6_2"),
65        }
66    }
67}
68
69impl Default for NyaaCategory {
70    fn default() -> Self {
71        Self::All
72    }
73}
74
75impl Category for NyaaCategory {}
76
77pub type NyaaQuery = Query<NyaaCategory>;
78
79#[derive(Debug, Default)]
80pub struct NyaaClient {
81    inner: reqwest::Client,
82}
83
84impl NyaaClient {
85    pub fn new() -> Self {
86        Self::default()
87    }
88}
89
90impl Client<NyaaCategory> for NyaaClient {
91    const BASE_URL: &'static str = "https://nyaa.si";
92
93    /// Send a query to the api
94    ///
95    ///```
96    #[doc = include_str!("../examples/custom_query.rs")]
97    ///```
98    #[tracing::instrument(skip(self))]
99    async fn get(&self, query: &Query<NyaaCategory>) -> Result<Vec<Torrent>> {
100        let url = format!("{}/?{}", Self::BASE_URL, query);
101
102        event!(Level::DEBUG, "url = {}", url);
103
104        let res = self.inner.get(url).send().await?.text().await?;
105
106        spawn_blocking(move || extract(&res, Self::BASE_URL)).await?
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113    use crate::query::QueryBuilder;
114    use crate::query::Sort;
115
116    #[tokio::test]
117    async fn test_get() {
118        let client = NyaaClient::new();
119        let query = QueryBuilder::new()
120            .search("frieren")
121            .sort(Sort::Downloads)
122            .build();
123        let res = client.get(&query).await.unwrap();
124
125        println!("{:#?}", res.get(0).unwrap());
126    }
127}