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
/*
 * 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 as well as a way to query an HTML tag via [`ElementQuery`]

pub mod query;

use self::query::{
	DataLocation, ElementDataQuery, ElementKind, ElementQuery, ElementQuerySliceExt,
};
use super::TransformEntry;
use crate::{
	action::transform::{
		error::RawContentsNotSetError,
		result::{TransformResult as TrRes, TransformedEntry, TransformedMessage},
	},
	entry::Entry,
	error::InvalidUrlError,
	sink::message::Media,
	utils::OptionExt,
};

use async_trait::async_trait;
use either::Either;
use itertools::Itertools;
use soup_kuchiki::{Handle as HtmlNode, NodeExt, QueryBuilderExt, Soup};
use std::iter;
use url::Url;

/// HTML parser
#[derive(Debug)]
pub struct Html {
	/// Query to find an item/entry/article in a list on the page. None means to thread the entire page as a single item
	pub item: Option<Vec<ElementQuery>>,

	/// Query to find the title of an item
	pub title: Option<ElementDataQuery>,

	/// One or more query 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
	pub text: Option<Vec<ElementDataQuery>>, // allow to find multiple paragraphs and join them together

	/// Query to find the id of an item
	pub id: Option<ElementDataQuery>,

	/// Query to find the link to an item
	pub link: Option<ElementDataQuery>,

	/// Query to find the image of that item
	pub img: Option<ElementDataQuery>,
}

#[allow(missing_docs)] // error message is self-documenting
#[derive(thiserror::Error, Debug)]
pub enum HtmlError {
	#[error(transparent)]
	RawContentsNotSet(#[from] RawContentsNotSetError),

	#[error("HTML element #{} not found. From query list: \n{}",
			.num + 1,
			.elem_list.display()
			)]
	ElementNotFound {
		num: usize,
		elem_list: Vec<ElementQuery>,
	},

	#[error("Data not found at {data:?} in element fount at {}",
			.element.display())]
	DataNotFoundInElement {
		data: DataLocation,
		element: Vec<ElementQuery>,
	},

	#[error("HTML element {0:?} is empty")]
	ElementEmpty(Vec<ElementQuery>),

	#[error(transparent)]
	InvalidUrl(#[from] InvalidUrlError),
}

#[async_trait]
impl TransformEntry for Html {
	type Err = HtmlError;

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

		let dom =
			Soup::new(entry.raw_contents.as_ref().ok_or(RawContentsNotSetError)?).get_handle();

		let body = dom
			.tag("body") // use body as the root html node
			.find()
			.unwrap_or_else(|| {
				tracing::debug!("HTML doesn't contain a body, using the root as the body");

				// or use the entire html if it doesn't exist for some reason (I don't think it should?)
				dom
			});

		if body.text().trim().is_empty() {
			tracing::warn!("HTML body is completely empty");

			return Ok(Vec::new());
		}

		let items = match self.item.as_ref() {
			Some(itemq) => Either::Left(
				find_chain(&body, itemq)
					.map_err(|i| HtmlError::ElementNotFound {
						num: i,
						elem_list: itemq.clone(),
					})?
					.into_iter(),
			),
			None => Either::Right(iter::once(body)),
		};

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

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

		Ok(entries)
	}
}

// TODO: make sure (and add tests!) that it errors if no item was found
// Won't remove this one till I add these goddamned tests >:(
impl Html {
	fn extract_entry(&self, html: &HtmlNode) -> Result<TransformedEntry, HtmlError> {
		let title = self
			.title
			.as_ref()
			.try_and_then(|q| extract_title(html, q))?;

		let body = self.text.as_ref().try_map(|q| extract_body(html, q))?;
		let id = self.id.as_ref().try_and_then(|q| extract_id(html, q))?;

		let link = self
			.link
			.as_ref()
			.try_and_then(|q| extract_url(html, q))?
			.try_map(|mut x| {
				x.next()
					.expect("iterator shouldn't be empty, otherwise it would've been None before")
			})?;

		let img = self.img.as_ref().try_and_then(|q| extract_imgs(html, q))?;

		Ok(TransformedEntry {
			id: TrRes::Old(id.map(Into::into)),
			raw_contents: TrRes::Old(body.clone()),
			msg: TransformedMessage {
				title: TrRes::Old(title),
				body: TrRes::Old(body),
				link: TrRes::Old(link),
				media: TrRes::Old(img),
			},
			..Default::default()
		})
	}
}

/// Extract data from the provided HTML tags
fn extract_data<'a>(
	html: &HtmlNode,
	data_query: &'a ElementDataQuery,
) -> Result<Option<impl Iterator<Item = String> + 'a>, HtmlError> {
	let data = find_chain(html, &data_query.query).map(|nodes| {
		nodes
			.into_iter()
			.map(|html| {
				match &data_query.data_location {
					DataLocation::Text => Some(html.text()),
					DataLocation::Attr(v) => html.get(v),
				}
				.map(|s| s.trim().to_owned())
			})
			.collect::<Option<Vec<_>>>()
	});

	let data = match data {
		Ok(Some(v)) => v,
		Ok(None) | Err(_) if data_query.optional => return Ok(None),
		Ok(None) => {
			return Err(HtmlError::DataNotFoundInElement {
				data: data_query.data_location.clone(),
				element: data_query.query.clone(),
			});
		}
		Err(i) => {
			return Err(HtmlError::ElementNotFound {
				num: i,
				elem_list: data_query.query.clone(),
			});
		}
	};

	if data.iter().all(String::is_empty) {
		return if data_query.optional {
			Ok(None)
		} else {
			Err(HtmlError::ElementEmpty(data_query.query.clone()))
		};
	}

	Ok(Some(data.into_iter().map(|s| match &data_query.regex {
		Some(r) => r.replace(&s).into_owned(),
		None => s,
	})))
}

fn extract_title(
	html: &HtmlNode,
	data_query: &ElementDataQuery,
) -> Result<Option<String>, HtmlError> {
	Ok(extract_data(html, data_query)?.map(|mut it| it.join("\n\n"))) // concat string with "\n\n" as sep
}

fn extract_body(html: &HtmlNode, data_queries: &[ElementDataQuery]) -> Result<String, HtmlError> {
	Ok(data_queries
		.iter()
		.map(|query| extract_data(html, query))
		.collect::<Result<Vec<_>, _>>()?
		.into_iter()
		.flatten() // flatten options, ignore none's
		.flatten() // flatten inner iterator
		.join("\n\n"))
}

fn extract_id(html: &HtmlNode, data_query: &ElementDataQuery) -> Result<Option<String>, HtmlError> {
	Ok(extract_data(html, data_query)?.map(Iterator::collect)) // concat strings if several
}

fn extract_url<'a>(
	html: &HtmlNode,
	query: &'a ElementDataQuery,
) -> Result<Option<impl Iterator<Item = Result<Url, HtmlError>> + 'a>, HtmlError> {
	Ok(extract_data(html, query)?.map(|it| {
		it.map(|url| Url::try_from(url.as_str()).map_err(|e| InvalidUrlError(e, url).into()))
	}))
}

fn extract_imgs(
	html: &HtmlNode,
	data_query: &ElementDataQuery,
) -> Result<Option<Vec<Media>>, HtmlError> {
	extract_url(html, data_query)?.try_map(|it| {
		it.map(|url| url.map(Media::Photo))
			.collect::<Result<Vec<_>, _>>()
	})
}

/// Find all elements matching the query in all the provided HTML parts
///
/// # Errors
/// Errors if the element in the `elem_queries` list wasn't found and returns the id of the query that wasn't found
fn find_chain(html: &HtmlNode, elem_queries: &[ElementQuery]) -> Result<Vec<HtmlNode>, usize> {
	if elem_queries.is_empty() {
		return Ok(vec![html.get_handle()]);
	}

	let mut html_nodes = vec![html.get_handle()];

	for (i, elem_query) in elem_queries.iter().enumerate() {
		html_nodes = html_nodes
			.into_iter()
			.flat_map(|html| find(html, elem_query))
			.collect(); // can't avoid this collect, using iterators directly produces "infinite cycle error"

		if html_nodes.is_empty() {
			return Err(i);
		}
	}

	Ok(html_nodes)
}

/// Find items matching the query in the provided HTML part
// I'm pretty sure this shouldn't capture from elem_query, so this is probably a bug in Soup
#[allow(clippy::needless_pass_by_value)]
fn find(html: HtmlNode, elem_query: &ElementQuery) -> impl Iterator<Item = HtmlNode> + '_ {
	match &elem_query.kind {
		ElementKind::Tag(val) => html.tag(val.as_str()).find_all(),
		ElementKind::Class(val) => html.class(val.as_str()).find_all(),
		ElementKind::Attr { name, value } => html.attr(name.as_str(), value.as_str()).find_all(),
	}
	.filter(move |found| {
		if let Some(ignore) = &elem_query.ignore {
			for i in ignore.iter() {
				let should_be_ignored = match i {
					ElementKind::Tag(tag) => found.name() == tag,
					ElementKind::Class(class) => found.get("class").map_or(false, |c| &c == class),
					ElementKind::Attr { name, value } => {
						found.get(name).map_or(false, |a| &a == value)
					}
				};

				if should_be_ignored {
					return false;
				}
			}
		}

		true
	})
}