fetcher 0.15.3

Data automation and pipelining framework
Documentation
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

//! This module contains the [`Html`] parser

pub mod error;

pub use self::error::HtmlError;
pub use scraper::Selector;

use self::error::{ErrorLocation, HtmlErrorInner, SelectorError};
use super::Transform;
use crate::{
	StaticStr,
	actions::transforms::{
		error::RawContentsNotSetError,
		result::{OptionUnwrapTransformResultExt, TransformedEntry, TransformedMessage},
	},
	entry::{Entry, EntryId},
	sinks::message::Media,
	utils::OptionExt,
};

use either::Either;
use itertools::Itertools;
use non_non_full::NonEmptyVec;
use scraper::{ElementRef, Html as HtmlDom};
use std::{borrow::Cow, iter};

/// HTML parser
#[derive(bon::Builder, Debug)]
pub struct Html {
	/// One or more CSS selectors to find the text of an item.
	/// If more than one, then they all get joined with "\n\n" in-between and put into the [`Message.body`] field.
	#[builder(field)]
	pub text: Option<NonEmptyVec<DataSelector>>,

	/// CSS selector to find an item/entry/article in a list on the page. None means to threat the entire page as a single item
	#[builder(with = |sel: &str| -> Result<_, SelectorError> { Selector::parse(sel).map_err(Into::into) })]
	pub item: Option<Selector>,

	/// CSS selector to find the title of an item
	#[builder(setters(
		name = title_internal,
		vis = ""
	))]
	pub title: Option<DataSelector>,

	/// CSS selector to find the ID of an item
	#[builder(setters(
		name = id_internal,
		vis = ""
	))]
	pub id: Option<DataSelector>,

	/// CSS selector to find the link to an item
	#[builder(setters(
		name = link_internal,
		vis = ""
	))]
	pub link: Option<DataSelector>,

	// TODO: support more media types
	// TODO: why only one selector? JSON transform supports many
	/// CSS selector to find the image of that item
	#[builder(setters(
		name = img_internal,
		vis = ""
	))]
	pub img: Option<DataSelector>,
}

/// A [`Selector`] can only select an HTML element.
/// A [`DataSelector`] makes it possible to specify where the expect data should be, e.g. in an attribute or as the text of the element
#[derive(Clone, Debug)]
pub struct DataSelector {
	/// CSS selector to find the HTML element
	pub selector: Selector,

	/// Places where to extract the expected data from
	pub locations: Vec<DataLocation>,

	/// If true, don't error if the data wasn't found
	pub optional: bool,
}

/// Location of the data we are looking for in an attribute
#[derive(Clone, Debug)]
pub enum DataLocation {
	/// Text of the element
	Text,

	/// An attribute of the element
	Attribute(StaticStr),
}

impl Transform for Html {
	type Err = HtmlError;

	async fn transform_entry(&mut self, entry: Entry) -> Result<Vec<TransformedEntry>, Self::Err> {
		tracing::trace!("Parsing raw_contents as HTML");

		// TODO: check .errors and .quirks
		let dom =
			HtmlDom::parse_document(entry.raw_contents.as_ref().ok_or(RawContentsNotSetError)?);

		let root = dom.root_element();

		// don't check this in prod, it should probably be fine. If it's not, it'll hopefully be caught while developing
		#[cfg(debug_assertions)]
		if root.text().collect::<String>().trim().is_empty() {
			tracing::warn!("HTML body is empty");
		}

		let items = match self.item.as_ref() {
			Some(item_sel) => {
				let matched_items = root.select(item_sel);

				if matched_items.clone().count() == 0 {
					tracing::error!("Item selector didn't match an item");
					return Err(HtmlError::Inner {
						error: HtmlErrorInner::SelectorNotMatched(item_sel.clone()),
						r#where: ErrorLocation::Item,
					});
				}

				Either::Left(matched_items)
			}
			None => Either::Right(iter::once(root)),
		};

		let entries = items
			.map(|item| self.extract_entry(item))
			.collect::<Result<Vec<_>, _>>()?;

		tracing::debug!("Found {} HTML entries total", entries.len());

		Ok(entries)
	}
}

impl Html {
	fn extract_entry(&self, html_fragment: ElementRef<'_>) -> Result<TransformedEntry, HtmlError> {
		let title = self
			.title
			.as_ref()
			.try_and_then(|q| extract_title(html_fragment, q))
			.map_err(|error| HtmlError::Inner {
				r#where: ErrorLocation::Title,
				error,
			})?;

		let body = self
			.text
			.as_ref()
			.try_map(|q| extract_body(html_fragment, q))
			.map_err(|(error, index)| HtmlError::Inner {
				r#where: ErrorLocation::Text { index },
				error,
			})?;

		let id = self
			.id
			.as_ref()
			.try_and_then(|q| extract_id(html_fragment, q))
			.map_err(|error| HtmlError::Inner {
				r#where: ErrorLocation::Id,
				error,
			})?;

		let link = self
			.link
			.as_ref()
			.try_and_then(|q| extract_link(html_fragment, q))
			.map_err(|error| HtmlError::Inner {
				r#where: ErrorLocation::Link,
				error,
			})?;

		let imgs = self
			.img
			.as_ref()
			.try_and_then(|q| extract_imgs(html_fragment, q))
			.map_err(|error| HtmlError::Inner {
				r#where: ErrorLocation::Img,
				error,
			})?;

		Ok(TransformedEntry {
			id: id.and_then(EntryId::new).unwrap_or_prev(),
			raw_contents: body.clone().unwrap_or_prev(),
			msg: TransformedMessage {
				title: title.unwrap_or_prev(),
				body: body.unwrap_or_prev(),
				link: link.unwrap_or_prev(),
				media: imgs.unwrap_or_prev(),
			},
			..Default::default()
		})
	}
}

/// Extract data from the provided HTML tags
fn extract_data(
	html_fragment: ElementRef<'_>,
	sel: &DataSelector,
) -> Result<Option<Vec<String>>, HtmlErrorInner> {
	let matched_elements = html_fragment.select(&sel.selector).collect::<Vec<_>>();

	if matched_elements.is_empty() {
		if sel.optional {
			tracing::debug!("Selector didn't match any element");
			return Ok(None);
		} else {
			return Err(HtmlErrorInner::SelectorNotMatched(sel.selector.clone()));
		}
	}

	let extracted_data = matched_elements
		.iter()
		.cartesian_product(sel.locations.iter())
		.filter_map(|(elem, location)| {
			let extracted_text = match location {
				DataLocation::Text => Some(Cow::Owned(elem.text().collect::<String>())),
				DataLocation::Attribute(attr) => elem.attr(attr).map(Cow::Borrowed),
			};

			extracted_text.map(|s| s.trim().to_owned())
		})
		.collect::<Vec<_>>();

	// selector matched an element that didn't have a required attribute
	if extracted_data.is_empty() {
		if sel.optional {
			tracing::debug!("Selector matched an element but wasn't able to extract anything");
			return Ok(None);
		} else {
			return Err(HtmlErrorInner::DataNotFoundInElement(sel.clone()));
		}
	}
	// selector matched an empty (or full of whitespace) element
	else if extracted_data.iter().all(String::is_empty) {
		if sel.optional {
			tracing::debug!("Selector matched an element but its contents were empty");
			return Ok(None);
		} else {
			return Err(HtmlErrorInner::ElementEmpty(sel.clone()));
		}
	}

	Ok(Some(extracted_data))
}

fn extract_title(
	html_fragment: ElementRef<'_>,
	selector: &DataSelector,
) -> Result<Option<String>, HtmlErrorInner> {
	Ok(extract_data(html_fragment, selector)?.map(|it| it.join("\n\n"))) // concat string with "\n\n" as sep
}

fn extract_body(
	html_fragment: ElementRef<'_>,
	selectors: &[DataSelector],
) -> Result<String, (HtmlErrorInner, usize)> {
	Ok(selectors
		.iter()
		.enumerate()
		.map(|(sel_idx, selector)| {
			extract_data(html_fragment, selector).map_err(|error| (error, sel_idx))
		})
		.collect::<Result<Vec<_>, _>>()?
		.into_iter()
		.flatten() // flatten options, ignore none's
		.flatten() // flatten inner iterator
		.join("\n\n"))
}

fn extract_id(
	html_fragment: ElementRef<'_>,
	selector: &DataSelector,
) -> Result<Option<String>, HtmlErrorInner> {
	Ok(extract_data(html_fragment, selector)?.map(|v| v.into_iter().collect::<String>())) // concat strings if several
}

fn extract_link(
	html_fragment: ElementRef<'_>,
	selector: &DataSelector,
) -> Result<Option<String>, HtmlErrorInner> {
	let urls = extract_data(html_fragment, selector)?;

	Ok(urls.map(|mut it| it.swap_remove(0)))
}

fn extract_imgs(
	html_fragment: ElementRef<'_>,
	selector: &DataSelector,
) -> Result<Option<NonEmptyVec<Media>>, HtmlErrorInner> {
	let extracted_strings = extract_data(html_fragment, selector)?;

	let as_images = extracted_strings.and_then(|it| {
		let vec = it.into_iter().map(Media::Photo).collect::<Vec<_>>();
		NonEmptyVec::new(vec)
	});

	Ok(as_images)
}

impl<S: html_builder::State> HtmlBuilder<S> {
	/// CSS Selector to find the text of an item.
	///
	/// Can be called multiple times.
	/// Makes it not optional and extract from [`DataLocation::Text`] by default.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn text(self, sel: &str) -> Result<Self, SelectorError> {
		self.text_with_conf(sel, iter::once(DataLocation::Text), false)
	}

	/// CSS Selector to find the text of an item.
	///
	/// Can be called multiple times.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn text_with_conf(
		mut self,
		sel: &str,
		locations: impl IntoIterator<Item = DataLocation>,
		optional: bool,
	) -> Result<Self, SelectorError> {
		let data_selector = DataSelector {
			selector: Selector::parse(sel)?,
			locations: locations.into_iter().collect(),
			optional,
		};

		match &mut self.text {
			Some(text) => text.push(data_selector),
			None => self.text = Some(NonEmptyVec::with_first(data_selector)),
		}

		Ok(self)
	}

	/// CSS Selector to find the title of an item.
	///
	/// Makes it not optional and extract from [`DataLocation::Text`] by default.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn title(self, sel: &str) -> Result<HtmlBuilder<html_builder::SetTitle<S>>, SelectorError>
	where
		S::Title: html_builder::IsUnset,
	{
		self.title_with_conf(sel, iter::once(DataLocation::Text), false)
	}

	/// CSS Selector to find the title of an item.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn title_with_conf(
		self,
		sel: &str,
		locations: impl IntoIterator<Item = DataLocation>,
		optional: bool,
	) -> Result<HtmlBuilder<html_builder::SetTitle<S>>, SelectorError>
	where
		S::Title: html_builder::IsUnset,
	{
		Ok(self.title_internal(DataSelector {
			selector: Selector::parse(sel)?,
			locations: locations.into_iter().collect(),
			optional,
		}))
	}

	/// CSS Selector to find the ID of an item.
	///
	/// Makes it not optional and extract from [`DataLocation::Text`] by default.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn id(self, sel: &str) -> Result<HtmlBuilder<html_builder::SetId<S>>, SelectorError>
	where
		S::Id: html_builder::IsUnset,
	{
		self.id_with_conf(sel, iter::once(DataLocation::Text), false)
	}

	/// CSS Selector to find the ID of an item.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn id_with_conf(
		self,
		sel: &str,
		locations: impl IntoIterator<Item = DataLocation>,
		optional: bool,
	) -> Result<HtmlBuilder<html_builder::SetId<S>>, SelectorError>
	where
		S::Id: html_builder::IsUnset,
	{
		Ok(self.id_internal(DataSelector {
			selector: Selector::parse(sel)?,
			locations: locations.into_iter().collect(),
			optional,
		}))
	}

	/// CSS Selector to find the link of an item.
	///
	/// Makes it not optional and extract from [`DataLocation::Text`] by default.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn link(self, sel: &str) -> Result<HtmlBuilder<html_builder::SetLink<S>>, SelectorError>
	where
		S::Link: html_builder::IsUnset,
	{
		self.link_with_conf(sel, iter::once(DataLocation::Text), false)
	}

	/// CSS Selector to find the link of an item.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn link_with_conf(
		self,
		sel: &str,
		locations: impl IntoIterator<Item = DataLocation>,
		optional: bool,
	) -> Result<HtmlBuilder<html_builder::SetLink<S>>, SelectorError>
	where
		S::Link: html_builder::IsUnset,
	{
		Ok(self.link_internal(DataSelector {
			selector: Selector::parse(sel)?,
			locations: locations.into_iter().collect(),
			optional,
		}))
	}

	/// CSS Selector to find the image of an item.
	///
	/// Makes it not optional and extract from [`DataLocation::Text`] by default.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn img(self, sel: &str) -> Result<HtmlBuilder<html_builder::SetImg<S>>, SelectorError>
	where
		S::Img: html_builder::IsUnset,
	{
		self.img_with_conf(sel, iter::once(DataLocation::Text), false)
	}

	/// CSS Selector to find the image of an item.
	///
	/// # Errors
	/// if the CSS selector `sel` isn't actually a valid CSS selector
	pub fn img_with_conf(
		self,
		sel: &str,
		locations: impl IntoIterator<Item = DataLocation>,
		optional: bool,
	) -> Result<HtmlBuilder<html_builder::SetImg<S>>, SelectorError>
	where
		S::Img: html_builder::IsUnset,
	{
		Ok(self.img_internal(DataSelector {
			selector: Selector::parse(sel)?,
			locations: locations.into_iter().collect(),
			optional,
		}))
	}
}

// TODO: add tests for multiple text selectors and multiple data locations with a "check" function to remove code duplication
#[cfg(test)]
mod tests {
	use std::{iter, sync::LazyLock};

	use assert_matches::assert_matches;

	use super::Html;
	use crate::{
		actions::transforms::{
			Transform,
			html::{
				DataLocation, HtmlError,
				error::{ErrorLocation, HtmlErrorInner},
			},
		},
		entry::Entry,
	};

	const DUMMY_HTML_PAGE: &str = r#"
	<body>
		<div>
			<p class="title">Hello, World!</p>
			<a href="https://example.com">Go to example.com</a>
			<h1 class="empty"></h1>
		</div>
	</body>	
	"#;

	static ENTRY: LazyLock<Entry> = LazyLock::new(|| {
		Entry::builder()
			.raw_contents(DUMMY_HTML_PAGE.to_owned())
			.build()
	});

	#[tokio::test]
	async fn item_found() {
		Html::builder()
			.item("div")
			.unwrap()
			.build()
			.transform_entry(ENTRY.clone())
			.await
			.unwrap();
	}

	#[tokio::test]
	async fn item_not_found() {
		let result = Html::builder()
			.item("article")
			.unwrap()
			.build()
			.transform_entry(ENTRY.clone())
			.await;

		assert_matches!(
			result,
			Err(HtmlError::Inner {
				r#where: ErrorLocation::Item,
				error: HtmlErrorInner::SelectorNotMatched(_)
			})
		);
	}

	#[tokio::test]
	async fn title_extracts_by_full_path() {
		let transformed_entries = Html::builder()
			.item("div")
			.unwrap()
			.title("p.title")
			.unwrap()
			.build()
			.transform_entry(ENTRY.clone())
			.await
			.unwrap();

		let mut transformed_entries = transformed_entries.into_iter();
		assert_eq!(
			transformed_entries
				.next()
				.unwrap()
				.into_entry(&*ENTRY)
				.msg
				.title
				.as_deref(),
			Some("Hello, World!")
		);
		assert_matches!(transformed_entries.next(), None);
	}

	#[tokio::test]
	async fn title_extracts_by_class() {
		let transformed_entries = Html::builder()
			.item("div")
			.unwrap()
			.title(".title")
			.unwrap()
			.build()
			.transform_entry(ENTRY.clone())
			.await
			.unwrap();

		let mut transformed_entries = transformed_entries.into_iter();
		assert_eq!(
			transformed_entries
				.next()
				.unwrap()
				.into_entry(&*ENTRY)
				.msg
				.title
				.as_deref(),
			Some("Hello, World!")
		);
		assert_matches!(transformed_entries.next(), None);
	}

	#[tokio::test]
	async fn title_extracts_by_element_type() {
		let transformed_entries = Html::builder()
			.item("div")
			.unwrap()
			.title("p")
			.unwrap()
			.build()
			.transform_entry(ENTRY.clone())
			.await
			.unwrap();

		let mut transformed_entries = transformed_entries.into_iter();
		assert_eq!(
			transformed_entries
				.next()
				.unwrap()
				.into_entry(&*ENTRY)
				.msg
				.title
				.as_deref(),
			Some("Hello, World!")
		);
		assert_matches!(transformed_entries.next(), None);
	}

	#[tokio::test]
	async fn title_not_found() {
		let result = Html::builder()
			.item("div")
			.unwrap()
			.title("article.title")
			.unwrap()
			.build()
			.transform_entry(ENTRY.clone())
			.await;

		assert_matches!(
			result,
			Err(HtmlError::Inner {
				r#where: ErrorLocation::Title,
				error: HtmlErrorInner::SelectorNotMatched(_)
			})
		);
	}

	#[tokio::test]
	async fn link_extracts_from_attr() {
		let transformed_entries = Html::builder()
			.item("div")
			.unwrap()
			.link_with_conf(
				"a",
				iter::once(DataLocation::Attribute("href".into())),
				false,
			)
			.unwrap()
			.build()
			.transform_entry(ENTRY.clone())
			.await
			.unwrap();

		let mut transformed_entries = transformed_entries.into_iter();
		assert_eq!(
			transformed_entries
				.next()
				.unwrap()
				.into_entry(&*ENTRY)
				.msg
				.link
				.as_deref(),
			Some("https://example.com")
		);
		assert_matches!(transformed_entries.next(), None);
	}

	#[tokio::test]
	async fn link_matched_but_wrong_attr() {
		let result = Html::builder()
			.item("div")
			.unwrap()
			.link_with_conf(
				"a",
				iter::once(DataLocation::Attribute("url".into())),
				false,
			)
			.unwrap()
			.build()
			.transform_entry(ENTRY.clone())
			.await;

		assert_matches!(
			result,
			Err(HtmlError::Inner {
				r#where: ErrorLocation::Link,
				error: HtmlErrorInner::DataNotFoundInElement(_),
			})
		);
	}

	#[tokio::test]
	async fn body_matched_empty_elem() {
		let result = Html::builder()
			.item("div")
			.unwrap()
			.text("h1.empty")
			.unwrap()
			.build()
			.transform_entry(ENTRY.clone())
			.await;

		assert_matches!(
			result,
			Err(HtmlError::Inner {
				r#where: ErrorLocation::Text { index: 0 },
				error: HtmlErrorInner::ElementEmpty(_),
			})
		);
	}
}