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
const NET: &str = "nhentai.net";
const XXX: &str = "nhentai.xxx";

fn get_type(file_type: &str) -> &str {
    if file_type == "j" {
        "jpg"
    } else if file_type == "p" {
        "png"
    } else {
        "gif"
    }
}

/// In cases where the standard output domain [nhentai.net](https://nhentai.net) may be restricted
/// on the end user's network, an option can be provided to use [nhentai.xxx](https://nhentai.xxx)
/// URLs instead. Note that the `Hentai::new()` constructor will not work if the standard output
/// domain is blocked. This is because [nhentai.xxx](https://nhentai.xxx) does not reimplement
/// nhentai's API and requests must still be made to the original URL.
///
/// However, if the JSON is provided, one may generate either type of URL regardless of whether or
/// not the original domain is blocked. This feature's primary purpose is to retrieve information
/// for the doujin under an unrestricted network, and create alternative URLs for the user with the
/// restricted network.
///
/// One of these options must be provided in the constructor.
#[derive(Debug)]
#[allow(clippy::upper_case_acronyms)]
pub enum Website {
    NET,
    XXX,
}

pub struct Make {
    pub mode: Website,
}

impl Make {
    pub fn new(mode: Website) -> Make {
        Make { mode }
    }

    pub fn doujin_url(&self, id: &String) -> String {
        match self.mode {
            Website::NET => format!("https://{}/g/{}", NET, id),
            Website::XXX => format!("https://{}/g/{}", XXX, id),
        }
    }

    pub fn cover(&self, media_id: &str, file_type: &str) -> String {
        match self.mode {
            Website::NET => format!(
                "https://t.{}/galleries/{}/cover.{}",
                NET,
                media_id,
                get_type(file_type)
            ),
            Website::XXX => format!(
                "https://cdn.{}/g/{}/cover.{}",
                XXX,
                media_id,
                get_type(file_type)
            ),
        }
    }

    pub fn cover_thumbnail(&self, media_id: &str, file_type: &str) -> String {
        match self.mode {
            Website::NET => format!(
                "https://t.{}/galleries/{}/thumb.{}",
                NET,
                media_id,
                get_type(file_type)
            ),
            Website::XXX => format!(
                "https://cdn.{}/g/{}/thumb.{}",
                XXX,
                media_id,
                get_type(file_type)
            ),
        }
    }

    pub fn page(&self, media_id: &str, number: u32, file_type: &str) -> String {
        match self.mode {
            Website::NET => format!(
                "https://i.{}/galleries/{}/{}.{}",
                NET,
                media_id,
                number.to_string(),
                get_type(file_type)
            ),
            Website::XXX => format!(
                "https://cdn.{}/g/{}/{}.{}",
                XXX,
                media_id,
                number.to_string(),
                get_type(file_type)
            ),
        }
    }

    #[allow(dead_code)]
    pub fn page_thumbnail(&self, media_id: &str, number: u32, file_type: &str) -> String {
        match self.mode {
            Website::NET => format!(
                "https://t.{}/galleries/{}/{}t.{}",
                NET,
                media_id,
                number.to_string(),
                get_type(file_type)
            ),
            Website::XXX => format!(
                "https://cdn.{}/g/{}/{}t.{}",
                XXX,
                media_id,
                number.to_string(),
                get_type(file_type)
            ),
        }
    }
}

pub fn doujin(id: u32) -> String {
    format!("https://nhentai.net/api/gallery/{}", id.to_string())
}