parse_book_source/
lib.rs

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
use anyhow::anyhow;
use serde_json::json;

pub mod analyzer;
pub mod book;
pub mod book_source;
pub mod error;
pub mod http_client;
pub mod utils;
pub use analyzer::*;
pub use book::*;
pub use book_source::*;
pub use error::*;
pub use http_client::*;

#[derive(Debug, Clone)]
pub struct BookSourceParser {
    pub book_source: BookSource,
    pub http_client: HttpClient,
    pub analyzer: AnalyzerManager,
    pub temp: Option<String>,
}

impl TryFrom<BookSource> for BookSourceParser {
    type Error = ParseError;

    fn try_from(book_source: BookSource) -> Result<Self> {
        let mut http_config = book_source.http_config.clone();
        if let Some(ref header) = book_source.header {
            http_config.header = Some(serde_json::from_str(header)?);
        }

        if let Some(ref response_time) = book_source.respond_time {
            http_config.timeout = Some(*response_time);
        }

        Ok(Self {
            http_client: HttpClient::new(&book_source.book_source_url, &http_config)?,
            book_source,
            analyzer: AnalyzerManager::new()?,
            temp: None,
        })
    }
}

impl BookSourceParser {
    pub fn new(book_source: BookSource) -> Result<Self> {
        Self::try_from(book_source)
    }

    /// 获取分类信息
    pub async fn get_explores(&mut self) -> Result<ExploreList> {
        if let Some(ref explore_url) = self.book_source.explore_url {
            if let Some(ref rule_explore_item) = self.book_source.rule_explore_item {
                let res = self
                    .http_client
                    .get(&self.book_source.book_source_url)
                    .await?
                    .text()
                    .await?;

                let list = self.analyzer.get_element(explore_url, &res)?;

                let res = list
                    .into_iter()
                    .flat_map(|item| {
                        rule_explore_item.parse_to_explore_item(&mut self.analyzer, &item)
                    })
                    .collect::<Vec<_>>();
                return Ok(res);
            } else {
                return Ok(serde_json::from_str(explore_url)?);
            }
        }

        Ok(vec![])
    }

    /// 搜索书籍
    pub async fn search_books(&mut self, key: &str, page: u32, page_size: u32) -> Result<BookList> {
        let url = self.analyzer.get_string(
            &self.book_source.search_url,
            "",
            Some(json!({
                "key": key,
                "page": page,
                "page_size": page_size,
            })),
        )?;

        let mut res = String::new();

        for i in url.split(",") {
            res = self.http_client.get(i).await?.text().await?;
        }

        let list = self
            .analyzer
            .get_element(&self.book_source.rule_search.book_list, &res)?;

        let res = list
            .into_iter()
            .flat_map(|item| {
                self.book_source
                    .rule_search
                    .parse_to_book_list_item(&mut self.analyzer, &item)
            })
            .collect::<Vec<_>>();

        Ok(res)
    }

    /// 使用explore_item的url获取书籍列表
    pub async fn explore_books(
        &mut self,
        url: &str,
        page: u32,
        page_size: u32,
    ) -> Result<BookList> {
        if self.book_source.rule_explore.is_none() {
            return Err(anyhow!("explore rule is none").into());
        }
        let url = self.analyzer.get_string(
            url,
            "",
            Some(json!({
                "page": page,
                "page_size": page_size,
            })),
        )?;

        let res = self.http_client.get(url.as_str()).await?.text().await?;

        let list = self.analyzer.get_element(
            &self.book_source.rule_explore.as_ref().unwrap().book_list,
            &res,
        )?;

        let res = list
            .into_iter()
            .flat_map(|item| {
                self.book_source
                    .rule_explore
                    .as_ref()
                    .unwrap()
                    .parse_to_book_list_item(&mut self.analyzer, &item)
            })
            .collect::<Vec<_>>();

        Ok(res)
    }

    /// 获取书籍信息
    pub async fn get_book_info(&mut self, book_url: &str) -> Result<BookInfo> {
        let res = self.http_client.get(book_url).await?.text().await?;

        let book_info = self
            .book_source
            .rule_book_info
            .parse_to_book_info(&mut self.analyzer, &res);

        self.temp = Some(res);

        book_info
    }

    pub async fn get_chapters(&mut self, toc_url: &str) -> Result<Vec<Chapter>> {
        // 如果toc_url是http开头的url,直接请求
        let res = if toc_url.starts_with("/") || toc_url.starts_with("http") {
            self.http_client.get(toc_url).await?.text().await?
        } else {
            self.temp.clone().ok_or(anyhow!("temp is none"))?
        };

        let list = self
            .analyzer
            .get_element(&self.book_source.rule_toc.chapter_list, &res)?;

        let res = list
            .into_iter()
            .flat_map(|item| {
                self.book_source
                    .rule_toc
                    .parse_to_chapter(&mut self.analyzer, &item)
            })
            .collect::<Vec<_>>();

        Ok(res)
    }

    pub async fn get_content(&mut self, chapter_url: &str) -> Result<String> {
        let mut res = self.http_client.get(chapter_url).await?.text().await?;

        match &self.book_source.rule_content {
            RuleContent::One { content } => self.analyzer.get_string(content, &res, None),

            RuleContent::More {
                content,
                next_content_url,
                start,
                end,
            } => {
                let end = self
                    .analyzer
                    .get_string(end, &res, None)?
                    .parse::<usize>()?;
                let mut contents = vec![];
                let mut start = *start;

                loop {
                    let content = self.analyzer.get_string(content, &res, None)?;
                    contents.push(content);

                    if start > end {
                        break;
                    }

                    let next_url = self.analyzer.get_string(
                        next_content_url,
                        &res,
                        Some(json!({
                            "index": start,
                        })),
                    )?;
                    res = self
                        .http_client
                        .get(next_url.as_str())
                        .await?
                        .text()
                        .await?;
                    start += 1;
                }

                Ok(contents.join("  "))
            }
        }
    }
}