diff --git a/source/selectors.rs b/source/selectors.rs index 16b24bb..b8122e3 100644 --- a/source/selectors.rs +++ b/source/selectors.rs @@ -29,6 +29,9 @@ lazy_static! { /// Selector for group wiki links. pub static ref GROUP_WIKI_LINKS: Selector = selector(r#"#sidebar [href*="/wiki/"]"#); + /// Selector for the site header context. + pub static ref SITE_HEADER_CONTEXT: Selector = selector(".site-header-context"); + /// Selector for the topic comment count. pub static ref TOPIC_COMMENT_COUNT: Selector = selector(".topic-comments-header h2"); diff --git a/source/topic.rs b/source/topic.rs index 80ae354..b7a8103 100644 --- a/source/topic.rs +++ b/source/topic.rs @@ -5,8 +5,9 @@ use scraper::Html; use crate::{ regexes::DUPLICATE_WHITESPACE_RE, selectors::{ - TOPIC_COMMENT_COUNT, TOPIC_FULL_BYLINE, TOPIC_FULL_LINK, TOPIC_FULL_TAGS, - TOPIC_FULL_TEXT, TOPIC_MAIN_ARTICLE, TOPIC_TOAST_WARNING, TOPIC_VOTE_COUNT, + SITE_HEADER_CONTEXT, TOPIC_COMMENT_COUNT, TOPIC_FULL_BYLINE, + TOPIC_FULL_LINK, TOPIC_FULL_TAGS, TOPIC_FULL_TEXT, TOPIC_MAIN_ARTICLE, + TOPIC_TOAST_WARNING, TOPIC_VOTE_COUNT, }, utilities::select_first_element_text, ParseError, @@ -25,6 +26,9 @@ pub struct Topic { /// The content of the topic. pub content: TopicContent, + /// The group the topic was posted in, with a leading tilde character. + pub group: String, + /// The unique ID of the topic. pub id: String, @@ -132,6 +136,20 @@ impl Topic { TopicContent::Unknown }; + let group = DUPLICATE_WHITESPACE_RE + .replace_all( + html + .select(&SITE_HEADER_CONTEXT) + .next() + .ok_or(ParseError::MissingExpectedHtml)? + .text() + .collect::() + .trim(), + "", + ) + .to_string(); + assert!(group.starts_with("~")); + let id = topic_article_element .value() .id() @@ -158,6 +176,7 @@ impl Topic { author, comment_total, content, + group, id, is_locked, is_official: false, // TODO: Implement this once it can be done. diff --git a/tests/samples/topic_deleted.html b/tests/samples/topic_deleted.html index 6283f1c..1d79523 100644 --- a/tests/samples/topic_deleted.html +++ b/tests/samples/topic_deleted.html @@ -5,7 +5,13 @@ Deleted topic sample for topic.rs - + +
+
+ ~group +
+
+
diff --git a/tests/samples/topic_link.html b/tests/samples/topic_link.html index 2163900..4f5537b 100644 --- a/tests/samples/topic_link.html +++ b/tests/samples/topic_link.html @@ -6,6 +6,12 @@ +
+
+ ~group +
+
+
diff --git a/tests/samples/topic_scheduled.html b/tests/samples/topic_scheduled.html index b6600a6..ccecdd3 100644 --- a/tests/samples/topic_scheduled.html +++ b/tests/samples/topic_scheduled.html @@ -6,6 +6,14 @@ +
+
+ ~group + . + recurring +
+
+
diff --git a/tests/samples/topic_text.html b/tests/samples/topic_text.html index 06d7947..8614edc 100644 --- a/tests/samples/topic_text.html +++ b/tests/samples/topic_text.html @@ -6,6 +6,12 @@ +
+
+ ~group +
+
+
diff --git a/tests/snapshots/topic__deleted.snap b/tests/snapshots/topic__deleted.snap index 7b9bea9..6c74c2a 100644 --- a/tests/snapshots/topic__deleted.snap +++ b/tests/snapshots/topic__deleted.snap @@ -6,6 +6,7 @@ Topic { author: Unknown, comment_total: 2, content: Unknown, + group: "~group", id: "666", is_locked: false, is_official: false, diff --git a/tests/snapshots/topic__link.snap b/tests/snapshots/topic__link.snap index 029bbc9..58517d2 100644 --- a/tests/snapshots/topic__link.snap +++ b/tests/snapshots/topic__link.snap @@ -10,6 +10,7 @@ Topic { content: Link( "https://tildes.net/~test", ), + group: "~group", id: "456", is_locked: false, is_official: false, diff --git a/tests/snapshots/topic__scheduled.snap b/tests/snapshots/topic__scheduled.snap index 74e134b..c7fc46a 100644 --- a/tests/snapshots/topic__scheduled.snap +++ b/tests/snapshots/topic__scheduled.snap @@ -8,6 +8,7 @@ Topic { content: Text( "

Scheduled Topic Text

", ), + group: "~group.recurring", id: "789", is_locked: false, is_official: false, diff --git a/tests/snapshots/topic__text.snap b/tests/snapshots/topic__text.snap index 09348ff..691cb03 100644 --- a/tests/snapshots/topic__text.snap +++ b/tests/snapshots/topic__text.snap @@ -10,6 +10,7 @@ Topic { content: Text( "

Topic Text

", ), + group: "~group", id: "123", is_locked: true, is_official: false,