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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use std::borrow::{Borrow, Cow};
use std::hash::{Hash, Hasher};
use std::time::Duration;

use anyhow::{Context, Result};
#[cfg(not(target_arch = "wasm32"))]
use reqwest::header::{HeaderMap, USER_AGENT};
use reqwest::{Client, IntoUrl, Url};
use select::document::Document;
#[cfg(feature = "serde0")]
use serde::{Deserialize, Serialize};

use crate::date::ArticleDate;
#[cfg(not(target_arch = "wasm32"))]
use crate::extrablatt::Config;
use crate::extract::{DefaultExtractor, Extractor};
use crate::language::Language;

/// Extension for documents that are considered valid sources for articles.
pub const ALLOWED_FILE_EXT: [&str; 12] = [
    "html", "htm", "md", "rst", "aspx", "jsp", "rhtml", "cgi", "xhtml", "jhtml", "asp", "shtml",
];

/// Segments in an url path that usually point to valid articles.
pub const GOOD_SEGMENTS: [&str; 15] = [
    "story",
    "article",
    "feature",
    "featured",
    "slides",
    "slideshow",
    "gallery",
    "news",
    "video",
    "media",
    "v",
    "radio",
    "press",
    "graphics",
    "investigations",
];

/// Segments in an url path that usually point to invalid articles.
pub const BAD_SEGMENTS: [&str; 17] = [
    "careers",
    "contact",
    "about",
    "faq",
    "terms",
    "privacy",
    "advert",
    "preferences",
    "feedback",
    "info",
    "browse",
    "howto",
    "account",
    "subscribe",
    "donate",
    "shop",
    "admin",
];

/// Domain names that are treated as bad sources for articles.
pub const BAD_DOMAINS: [&str; 4] = ["amazon", "doubleclick", "twitter", "outbrain"];

/// An identified url to an article and it's title.
#[derive(Debug, Clone)]
pub struct ArticleUrl {
    /// The url of the article.
    pub url: Url,
    /// The title of the article.
    pub title: Option<String>,
}

impl ArticleUrl {
    pub fn new(url: Url) -> Self {
        Self { url, title: None }
    }

    pub fn new_with_title<T: ToString>(url: Url, title: Option<T>) -> Self {
        Self {
            url,
            title: title.map(|s| s.to_string()),
        }
    }

    /// If the article is related heavily to media: gallery, video, big
    /// pictures, etc
    pub fn is_media_news(&self) -> bool {
        if let Some(segments) = self.url.path_segments() {
            let media_segemnts = &[
                "video",
                "slide",
                "gallery",
                "powerpoint",
                "fashion",
                "glamour",
                "cloth",
                "graphics",
            ];
            for segment in segments.filter(|s| s.len() < 11) {
                if media_segemnts.contains(&segment.to_lowercase().as_str()) {
                    return true;
                }
            }
        }
        false
    }
}

impl PartialEq for ArticleUrl {
    fn eq(&self, other: &Self) -> bool {
        self.url.eq(&other.url)
    }
}

impl Hash for ArticleUrl {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.url.hash(state)
    }
}

impl Eq for ArticleUrl {}

impl Borrow<Url> for ArticleUrl {
    fn borrow(&self) -> &Url {
        &self.url
    }
}

/// A full representation of a news article.
///
/// # Example
///
/// Download a single article directly
///
/// ```no_run
/// # use extrablatt::Article;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let article = Article::builder("http://example.com/")?.get().await?;
/// #   Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct Article {
    /// The url of the article.
    pub url: Url,
    /// The parsed response html `Document`.
    pub doc: Document,
    /// The extracted content of the article.
    pub content: ArticleContent<'static>,
    /// The expected language of the article.
    pub language: Language,
}

impl Article {
    /// Extract the article directly from the doc using the [`DefaultExtractor`]
    pub fn new<U: IntoUrl, T: AsRef<str>>(url: U, doc: T) -> Result<Article> {
        Self::with_extractor(url, doc, &DefaultExtractor::default())
    }

    pub fn with_extractor<U, T, TExtract>(url: U, doc: T, extractor: &TExtract) -> Result<Article>
    where
        U: IntoUrl,
        T: AsRef<str>,
        TExtract: Extractor,
    {
        Self::with_extractor_and_lang(url, doc, extractor, Default::default())
    }

    /// Extract the article directly from the doc using the provided `extractor`
    /// and `lang`
    pub fn with_extractor_and_lang<U, T, TExtract>(
        url: U,
        doc: T,
        extractor: &TExtract,
        language: Language,
    ) -> Result<Article>
    where
        U: IntoUrl,
        T: AsRef<str>,
        TExtract: Extractor,
    {
        let url = url.into_url()?;
        let doc = Document::from(doc.as_ref());
        let content = extractor
            .article_content(
                &doc,
                extractor.base_url(&doc).as_ref(),
                Some(language.clone()),
            )
            .into_owned();

        Ok(Article {
            url,
            doc,
            content,
            language,
        })
    }

    /// Retrieves the [`ArticleContent`] from the `url`
    ///
    /// Convenience method for:
    ///
    /// ```no_run
    ///  # use extrablatt::Article;
    ///  # #[tokio::main]
    ///  # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///        let content = Article::builder("http://example.com")?.get().await?.content;
    ///  #     Ok(())
    ///  # }
    /// ```
    pub async fn content<T: IntoUrl>(url: T) -> Result<ArticleContent<'static>> {
        let article = Self::get(url).await?;
        Ok(article.content)
    }

    /// Get the [`Article`] for the `url`
    pub async fn get<T: IntoUrl>(url: T) -> Result<Article> {
        Self::builder(url)?.get().await
    }

    /// Get the [`Article`] for the `url` using a specific `Extractor`
    pub async fn get_with_extractor<T: IntoUrl, TExtract: Extractor>(
        url: T,
        extractor: &TExtract,
    ) -> Result<Article> {
        Self::builder(url)?.get_with_extractor(extractor).await
    }

    /// Convenience method for creating a new [`ArticleBuilder`]
    ///
    /// Same as calling [`ArticleBuilder::new`]
    pub fn builder<T: IntoUrl>(url: T) -> Result<ArticleBuilder> {
        ArticleBuilder::new(url)
    }

    /// Drops the article's html [`select::document::Document`].
    pub fn drop_document(self) -> PureArticle {
        PureArticle {
            url: self.url,
            content: self.content,
            language: self.language,
        }
    }
}

/// An [`crate::Article`] without the [`select::document::Document`], mainly to
/// use serde.
#[derive(Debug)]
#[cfg_attr(feature = "serde0", derive(Serialize, Deserialize))]
pub struct PureArticle {
    /// The url of the article.
    pub url: Url,
    /// The extracted content of the article.
    pub content: ArticleContent<'static>,
    /// The expected language of the article.
    pub language: Language,
}

pub struct ArticleBuilder {
    url: Option<Url>,
    timeout: Option<Duration>,
    language: Option<Language>,
    browser_user_agent: Option<String>,
}

impl ArticleBuilder {
    pub fn new<T: IntoUrl>(url: T) -> Result<Self> {
        let url = url.into_url()?;

        Ok(ArticleBuilder {
            url: Some(url),
            timeout: None,
            language: None,
            browser_user_agent: None,
        })
    }

    pub fn browser_user_agent<T: ToString>(mut self, browser_user_agent: T) -> Self {
        self.browser_user_agent = Some(browser_user_agent.to_string());
        self
    }

    pub fn timeout(mut self, dur: Duration) -> Self {
        self.timeout = Some(dur);
        self
    }

    pub fn language(mut self, language: Language) -> Self {
        self.language = Some(language);
        self
    }

    /// Downloads the article and extract it's content using the
    /// [`crate::DefaultExtractor`].
    pub async fn get(self) -> Result<Article> {
        self.get_with_extractor(&DefaultExtractor::default()).await
    }

    /// Downloads the article and extracts it's content using the provided
    /// [`crate::Extractor`].
    pub async fn get_with_extractor<TExtract: Extractor>(
        self,
        extractor: &TExtract,
    ) -> Result<Article> {
        let url = self
            .url
            .context("Url of the article must be initialized.")?;

        #[cfg(target_arch = "wasm32")]
        let builder = Client::builder();

        #[cfg(not(target_arch = "wasm32"))]
        let builder = {
            let timeout = self
                .timeout
                .unwrap_or_else(|| Duration::from_secs(Config::DEFAULT_REQUEST_TIMEOUT_SEC));

            let mut headers = HeaderMap::with_capacity(1);

            headers.insert(
                USER_AGENT,
                self.browser_user_agent
                    .map(|x| x.parse())
                    .unwrap_or_else(|| Config::user_agent().parse())
                    .context("Failed to parse user agent header.")?,
            );

            Client::builder().default_headers(headers).timeout(timeout)
        };

        let resp = builder.build()?.get(url).send().await?;

        if !resp.status().is_success() {
            // let msg = format!("Unsuccessful request to {:?}", resp.url());
            // return ExtrablattError::NoHttpSuccessResponse { response: resp
            // }.context(msg);
            return Err(anyhow::anyhow!("Unsuccessful request to {:?}", resp.url()));
        }

        let url = resp.url().to_owned();
        let doc = Document::from_read(&*resp.bytes().await?)
            .context(format!("Failed to read {:?} html as document.", url))?;

        let content = extractor
            .article_content(
                &doc,
                extractor.base_url(&doc).as_ref(),
                self.language.clone(),
            )
            .into_owned();

        Ok(Article {
            url,
            doc,
            content,
            language: self.language.unwrap_or_default(),
        })
    }
}

/// Bundles all the content found for an article.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde0", derive(Serialize, Deserialize))]
pub struct ArticleContent<'a> {
    pub authors: Vec<Cow<'a, str>>,
    pub title: Option<Cow<'a, str>>,
    pub publishing_date: Option<ArticleDate>,
    pub keywords: Vec<Cow<'a, str>>,
    pub description: Option<Cow<'a, str>>,
    pub text: Option<Cow<'a, str>>,
    pub language: Option<Language>,
    pub thumbnail: Option<Url>,
    pub top_image: Option<Url>,
    pub references: Vec<Url>,
    pub images: Vec<Url>,
    pub videos: Vec<Url>,
}

impl<'a> ArticleContent<'a> {
    /// Convenience method to create a  [`ArticleContentBuilder`]
    pub fn builder() -> ArticleContentBuilder<'a> {
        ArticleContentBuilder::default()
    }

    /// Transfers ownership of the content directly to this `ArticleContent`.
    pub fn into_owned(self) -> ArticleContent<'static> {
        ArticleContent {
            authors: self
                .authors
                .into_iter()
                .map(Cow::into_owned)
                .map(Cow::Owned)
                .collect(),
            title: self.title.map(Cow::into_owned).map(Cow::Owned),
            publishing_date: self.publishing_date,
            keywords: self
                .keywords
                .into_iter()
                .map(Cow::into_owned)
                .map(Cow::Owned)
                .collect(),
            description: self.description.map(Cow::into_owned).map(Cow::Owned),
            text: self.text.map(Cow::into_owned).map(Cow::Owned),
            language: self.language,
            thumbnail: self.thumbnail,
            top_image: self.top_image,
            references: self.references,
            images: self.images,
            videos: self.videos,
        }
    }
}

#[derive(Debug, Default)]
pub struct ArticleContentBuilder<'a> {
    pub authors: Option<Vec<Cow<'a, str>>>,
    pub title: Option<Cow<'a, str>>,
    pub publishing_date: Option<ArticleDate>,
    pub keywords: Option<Vec<Cow<'a, str>>>,
    pub description: Option<Cow<'a, str>>,
    pub text: Option<Cow<'a, str>>,
    pub language: Option<Language>,
    pub thumbnail: Option<Url>,
    pub top_image: Option<Url>,
    pub references: Option<Vec<Url>>,
    pub images: Option<Vec<Url>>,
    pub videos: Option<Vec<Url>>,
}

impl<'a> ArticleContentBuilder<'a> {
    pub fn authors(mut self, authors: Vec<Cow<'a, str>>) -> Self {
        self.authors = Some(authors);
        self
    }

    pub fn title(mut self, title: Cow<'a, str>) -> Self {
        self.title = Some(title);
        self
    }

    pub fn publishing_date(mut self, date: ArticleDate) -> Self {
        self.publishing_date = Some(date);
        self
    }

    pub fn keywords(mut self, keywords: Vec<Cow<'a, str>>) -> Self {
        self.keywords = Some(keywords);
        self
    }

    pub fn description(mut self, description: Cow<'a, str>) -> Self {
        self.description = Some(description);
        self
    }

    pub fn text(mut self, text: Cow<'a, str>) -> Self {
        self.text = Some(text);
        self
    }

    pub fn language(mut self, language: Language) -> Self {
        self.language = Some(language);
        self
    }

    pub fn thumbnail(mut self, thumbnail: Url) -> Self {
        self.thumbnail = Some(thumbnail);
        self
    }

    pub fn top_image(mut self, top_image: Url) -> Self {
        self.top_image = Some(top_image);
        self
    }

    pub fn references(mut self, references: Vec<Url>) -> Self {
        self.references = Some(references);
        self
    }

    pub fn images(mut self, images: Vec<Url>) -> Self {
        self.images = Some(images);
        self
    }

    pub fn videos(mut self, videos: Vec<Url>) -> Self {
        self.videos = Some(videos);
        self
    }

    pub fn build(self) -> ArticleContent<'a> {
        ArticleContent {
            authors: self.authors.unwrap_or_default(),
            title: self.title,
            publishing_date: self.publishing_date,
            keywords: self.keywords.unwrap_or_default(),
            description: self.description,
            text: self.text,
            language: self.language,
            thumbnail: self.thumbnail,
            top_image: self.top_image,
            references: self.references.unwrap_or_default(),
            images: self.images.unwrap_or_default(),
            videos: self.videos.unwrap_or_default(),
        }
    }
}