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
//!
//! Rss Parse Library
//!
//! ```
//! use easy_rss::RssParser;
//!
//! fn main()->Result<(),Box<dyn std::error::Error>> {
//!     let address = "https://www.zhihu.com/rss";
//!     let mut parser = RssParser::from_url(address,"utf8")?;
//!     parser.author_tag = String::from("dc:creator");
//!     let rss = parser.parse_vec()?;
//!     println!("{:?}",rss);
//!     Ok(())
//! }
//! ```

use std::io::prelude::*;
use std::fs::File;
use quick_xml::Reader;
use quick_xml::events::Event;
use json::{object,array};

/// &lt;item&gt;&lt;/item&gt;
pub static RSS_DEFAULT_NODE_TAG:&'static str = "item";

/// &lt;title&gt;...&lt;/title&gt;
pub static RSS_DEFAULT_TITLE_TAG:&'static str = "title";

/// &lt;link&gt;...&lt;/link&gt;
pub static RSS_DEFAULT_LINK_TAG:&'static str = "link";

/// &lt;author&gt;...&lt;/author&gt;
pub static RSS_DEFAULT_AUTHOR_TAG:&'static str = "author";

/// &lt;description&gt;...&lt;/description&gt;
pub static RSS_DEFAULT_DESC_TAG:&'static str = "description";

/// &lt;guid&gt;...&lt;/guid&gt;
pub static RSS_DEFAULT_GUID_TAG:&'static str = "guid";

/// &lt;pubDate&gt;...&lt;/pubDate&gt;
pub static RSS_DEFAULT_PUBLISH_TAG:&'static str = "pubDate";

/// Check &lt;xml&gt; and &gt;rss&lt;
pub static XML_DEFAULT_TAG:&'static str = "xml";
pub static RSS_DEFAULT_TAG:&'static str = "rss";

///
/// Rss Item Node
///
/// ```
/// use easy_rss::RssItem;
/// fn main(){
///     let item = RssItem::default();
///     println!("{:?}",item);
/// }
/// ```
#[derive(Debug)]
#[allow(dead_code)]
pub struct RssItem{
    pub title: String,
    pub link: String,
    pub author: String,
    pub description: String,
    pub guid: String,
    pub publish: String,
}

///
/// Rss Parse Utils
///
/// ### Parse Xml
/// ```
/// use easy_rss::RssParser;
///
/// fn main()->Result<(),Box<dyn std::error::Error>>{
///     let mut parser_default = RssParser::new();
///     println!("{:?}",parser_default);
///     parser_default.set_xml(String::from(
///        r#"<?xml version="1.0" encoding="UTF-8" ?>
///         <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
///             <item>
///                 <title>Hey!</title>
///                 <link>examples.com</link>
///                 <description>hello.world!</description>
///                 <author>MeteorCat</author>
///                 <guid>unique key</guid>
///                 <pubDate>2020-05-28 15:00:00</pubDate>
///             </item>
///         </rss>
///         "#
///     ));
///     println!("{:?}",parser_default.parse_vec()?);
///     Ok(())
/// }
/// ```
///
/// ### Parse Web XMl
/// ```
/// use easy_rss::RssParser;
///
/// fn main()->Result<(),Box<dyn std::error::Error>> {
///     let address = "https://www.zhihu.com/rss";
///     let mut parser = RssParser::from_url(address,"utf8")?;
///     parser.author_tag = String::from("dc:creator");
///     let rss = parser.parse_vec()?;
///     println!("{:?}",rss);
///     Ok(())
/// }
/// ```
///
/// ### RSS To Json
/// ```
/// use easy_rss::RssParser;
///
/// fn main()->Result<(),Box<dyn std::error::Error>> {
///     let address = "https://www.zhihu.com/rss";
///     let mut parser = RssParser::from_url(address,"utf8")?;
///     parser.author_tag = String::from("dc:creator");
///     assert!(parser.parse_json().is_ok());
///     Ok(())
/// }
/// ```
///
/// ### Rss Request Builder
/// ```
/// use easy_rss::RssParser;
///
/// fn main()->Result<(),Box<dyn std::error::Error>> {
///     let address = "https://www.zhihu.com/rss";
///     let charset = "utf8";
///     let mut parser = RssParser::new();
///     parser.author_tag = "dc:creator".into();
///     parser.publish_tag = "pubDate".into();
///     let xml = parser.request_xml(address,charset)?;
///     parser.set_xml(xml);
///     assert!(parser.parse_vec().is_ok());
///     Ok(())
/// }
/// ```
#[derive(Debug)]
#[allow(dead_code)]
pub struct RssParser{
    xml:String,
    pub node_tag:String,
    pub title_tag:String,
    pub link_tag:String,
    pub author_tag:String,
    pub description_tag:String,
    pub guid_tag:String,
    pub publish_tag:String,
}


impl Default for RssItem{
    fn default() -> Self {
        Self{
            title:String::new(),
            link:String::new(),
            author:String::new(),
            description:String::new(),
            guid:String::new(),
            publish:String::new()
        }
    }
}


impl RssParser{

    ///
    /// Todo: Optimization
    ///
    pub fn check_xml(&mut self)->bool{
        //todo: need optimization
        if !self.xml.contains(XML_DEFAULT_TAG) && !self.xml.contains(&XML_DEFAULT_TAG.to_uppercase()) {
            return false;
        }
        if !self.xml.contains(RSS_DEFAULT_TAG) && !self.xml.contains(&RSS_DEFAULT_TAG.to_uppercase()) {
            return false;
        }
        return true;
    }


    ///
    /// Request Rss by Web
    ///
    pub fn request_xml(&mut self,url:&str,charset:&str)->Result<String,reqwest::Error>{
        Ok(reqwest::blocking::get(url)?
            .text_with_charset(charset)?)
    }

    ///
    /// Request RSS by File
    pub async fn request_file(&mut self,filename:&str)->Result<String,std::io::Error>{
        let mut f = File::open(filename)?;
        let mut body = String::new();
        f.read_to_string(&mut body)?;
        Ok(body)
    }


    pub fn new()->Self{
        Self{
            xml:String::new(),
            node_tag:String::from(RSS_DEFAULT_NODE_TAG),
            title_tag:String::from(RSS_DEFAULT_TITLE_TAG),
            link_tag:String::from(RSS_DEFAULT_LINK_TAG),
            author_tag:String::from(RSS_DEFAULT_AUTHOR_TAG),
            description_tag:String::from(RSS_DEFAULT_DESC_TAG),
            guid_tag:String::from(RSS_DEFAULT_GUID_TAG),
            publish_tag:String::from(RSS_DEFAULT_PUBLISH_TAG)
        }
    }


    pub fn from_str(xml:String)->Result<Self,std::io::Error>{
        let mut parser = Self::new();
        parser.xml = xml;
        if !parser.check_xml() {
            Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Failed by RssParser::check_xml"
            ))
        }else {
            Ok(parser)
        }
    }



    pub fn from_url(url:&str,charset:&str)->Result<Self,std::io::Error>{
        let mut parser = Self::new();
        match parser.request_xml(url,charset) {
            Ok(body) => {
                parser.xml = body;
                if !parser.check_xml() {
                    Err(std::io::Error::from(std::io::ErrorKind::InvalidData))
                }else {
                    Ok(parser)
                }
            }
            Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData,e.to_string()))
        }
    }

    pub async fn from_file(filename:&str)->Result<Self,std::io::Error>{
        let mut parser = Self::new();
        let body = parser.request_file(filename).await?;

        parser.xml = body;
        if !parser.check_xml() {
            Err(std::io::Error::from(std::io::ErrorKind::InvalidData))
        }else {
            Ok(parser)
        }
    }

    pub fn parse_vec(&mut self)->Result<Vec<RssItem>,std::io::Error>{
        let mut reader = Reader::from_str(self.xml.as_str());

        reader.trim_text(true);
        reader.check_end_names(true);
        reader.check_comments(false);
        reader.expand_empty_elements(true);


        let mut buff = Vec::new();
        let mut nodes = Vec::new();
        let mut active = String::new();

        loop{
            match reader.read_event(&mut buff) {
                // Fetch = <Item></Item>
                Ok(Event::Start(ref e)) => {
                    active = std::str::from_utf8(e.name())
                        .expect("Failed By Parse <Item>")
                        .to_string();

                    if self.node_tag.eq_ignore_ascii_case(&active) {
                        nodes.push(RssItem::default());
                    }
                }

                // Fetch = <Item><Node><CDATA></Node><Item>
                Ok(Event::CData(ref e)) => {
                    let node_text = std::str::from_utf8(e.escaped())
                        .expect("Failed by Parse <CData>");

                    if let Some(last) = nodes.last_mut() {
                        match active {
                            _ if self.title_tag.eq_ignore_ascii_case(&active) => { last.title = node_text.to_string() },
                            _ if self.link_tag.eq_ignore_ascii_case(&active) => { last.link = node_text.to_string() },
                            _ if self.author_tag.eq_ignore_ascii_case(&active) => { last.author = node_text.to_string() },
                            _ if self.description_tag.eq_ignore_ascii_case(&active) => { last.description = node_text.to_string() },
                            _ if self.guid_tag.eq_ignore_ascii_case(&active) => { last.guid = node_text.to_string() },
                            _ if self.publish_tag.eq_ignore_ascii_case(&active) => { last.publish = node_text.to_string() },
                            _ => (),
                        }
                    }
                }

                // Fetch = <Item><Node></Node><Item>
                Ok(Event::Text(ref e)) => {
                    let node_text = e
                        .unescape_and_decode(&reader)
                        .expect("Failed by Parse <Node>");

                    if let Some(last) = nodes.last_mut() {
                        match active {
                            _ if self.title_tag.eq_ignore_ascii_case(&active) => { last.title = node_text.to_string() },
                            _ if self.link_tag.eq_ignore_ascii_case(&active) => { last.link = node_text.to_string() },
                            _ if self.author_tag.eq_ignore_ascii_case(&active) => { last.author = node_text.to_string() },
                            _ if self.description_tag.eq_ignore_ascii_case(&active) => { last.description = node_text.to_string() },
                            _ if self.guid_tag.eq_ignore_ascii_case(&active) => { last.guid = node_text.to_string() },
                            _ if self.publish_tag.eq_ignore_ascii_case(&active) => { last.publish = node_text.to_string() },
                            _ => (),
                        }
                    }
                }

                Ok(Event::Eof) => break,
                Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other,format!("{:?}",e))),
                _ =>(),
            }
            buff.clear();
        }

        Ok(nodes)
    }

    pub fn parse_json(&mut self)->Result<String,std::io::Error>{
        let item = self.parse_vec()?;
        let mut json = array![];
        for node in item.into_iter() {
            let data = object!{
                "title": node.title,
                "link": node.link,
                "author": node.author,
                "description": node.description,
                "guid": node.guid,
                "publish": node.publish,
            };
            json.push(data).expect("Failed by Parse Json")
        }

        Ok(json.dump())
    }


    pub fn set_xml(&mut self,xml:String){
        self.xml = xml;
    }

    pub fn get_xml(&self)->&String{
        &self.xml
    }
}




#[cfg(test)]
mod tests {
    use crate::RssParser;

    #[test]
    fn easy_rss_works()->Result<(),Box<dyn std::error::Error>> {
        let address = "https://www.zhihu.com/rss";
        let mut parser = RssParser::from_url(address,"utf8")?;
        parser.author_tag = String::from("dc:creator");
        let rss = parser.parse_vec()?;
        assert!(rss.len()>0);
        Ok(())
    }

    #[test]
    fn easy_rss_to_json(){
        let address = "https://www.zhihu.com/rss";
        let mut parser = RssParser::from_url(address,"utf8").unwrap();
        parser.author_tag = String::from("dc:creator");
        assert!(parser.parse_json().is_ok());
    }


    #[test]
    fn easy_rss_builder(){
        let mut parser = RssParser::new();
        parser.set_xml(String::from(
            r#"<?xml version="1.0" encoding="UTF-8" ?>
                <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
                    <item>
                        <title>Hey!</title>
                        <link>examples.com</link>
                        <description>hello.world!</description>
                        <author>MeteorCat</author>
                        <guid>unique key</guid>
                        <pubDate>2020-05-28 15:00:00</pubDate>
                    </item>
                </rss>
        "#));
        let rss = parser.parse_vec().unwrap();
        assert!(rss.len()>0);
    }
}