Skip to content

Added member count to org info. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub enum Organization {
sid: String,
url: Box<Url>,
rank: OrganizationRank,
member_count: usize,
},
Redacted,
}
Expand Down
29 changes: 29 additions & 0 deletions src/parse_org.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct OrgData {
title: String,
rank: u8,
url: Url,
member_count: usize,
}

pub(crate) fn parse_org<'a>(
Expand Down Expand Up @@ -39,6 +40,7 @@ pub(crate) fn parse_org<'a>(
},
sid: info.sid,
url: Box::new(info.url),
member_count: info.member_count,
}
} else {
Organization::Redacted
Expand Down Expand Up @@ -66,6 +68,7 @@ fn parse_logo<'a>(element: &ElementRef) -> Result<Url, ScScrapingError<'a>> {
fn parse_info<'a>(element: &ElementRef) -> Result<OrgData, ScScrapingError<'a>> {
let info_selector = Selector::parse("div.info > p").unwrap();
let rank_selector = Selector::parse("div.info > div.ranking > span.active").unwrap();
let member_count_selector = Selector::parse("div.thumb > span.members").unwrap();

let rank = element.select(&rank_selector).count() as u8;

Expand Down Expand Up @@ -134,12 +137,38 @@ fn parse_info<'a>(element: &ElementRef) -> Result<OrgData, ScScrapingError<'a>>
ScScrapingError::organization(message)
})?;

let member_count = element
.select(&member_count_selector)
.next()
.ok_or_else(|| {
let message = "Could not find member count";
error!("{message}");
ScScrapingError::organization(message)
})?
.inner_html()
.split(" members")
.collect::<Vec<&str>>()
.first()
.ok_or_else(|| {
let message = "Could not split out member count";
error!("{message}");
ScScrapingError::organization(message)
})?
.parse::<usize>()
.ok()
.ok_or_else(|| {
let message = "Could not parse member count";
error!("{message}");
ScScrapingError::organization(message)
})?;

let data = OrgData {
name,
rank,
sid,
title,
url,
member_count,
};

Ok(data)
Expand Down