2018-10-28 21:29:54 +00:00
|
|
|
use crate::{
|
|
|
|
error::*,
|
|
|
|
logic::plain_parse_elem as plain_parse,
|
|
|
|
models::{
|
|
|
|
search::{
|
|
|
|
Paginated,
|
|
|
|
linkshell::LinkshellSearchItem,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
use ffxiv_types::World;
|
|
|
|
|
|
|
|
use scraper::{Html, ElementRef};
|
|
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
selectors!(
|
|
|
|
ITEM_ENTRY => ".ldst__window > .entry";
|
|
|
|
|
|
|
|
ITEM_ID => ".entry__link--line";
|
|
|
|
ITEM_NAME => ".entry__linkshell > .entry__name";
|
|
|
|
ITEM_WORLD => ".entry__linkshell > p.entry__world";
|
|
|
|
ITEM_ACTIVE_MEMBERS => ".entry .entry__linkshell__member > span";
|
|
|
|
);
|
|
|
|
|
|
|
|
pub fn parse(s: &str) -> Result<Paginated<LinkshellSearchItem>> {
|
|
|
|
let html = Html::parse_document(s);
|
|
|
|
|
|
|
|
let pagination = crate::logic::search::parse_pagination(&html)?;
|
|
|
|
|
|
|
|
// has results but requested an invalid page
|
|
|
|
if pagination.total_results != 0 && pagination.current_page == 0 {
|
|
|
|
return Err(Error::InvalidPage(pagination.total_pages));
|
|
|
|
}
|
|
|
|
|
|
|
|
let results: Vec<LinkshellSearchItem> = html
|
|
|
|
.select(&*ITEM_ENTRY)
|
2019-04-04 22:58:06 +00:00
|
|
|
.map(parse_single)
|
2018-10-28 21:29:54 +00:00
|
|
|
.collect::<Result<_>>()?;
|
|
|
|
|
|
|
|
Ok(Paginated {
|
|
|
|
pagination,
|
|
|
|
results,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:58:06 +00:00
|
|
|
fn parse_single(html: ElementRef) -> Result<LinkshellSearchItem> {
|
2018-10-28 21:29:54 +00:00
|
|
|
let id = parse_id(html)?;
|
|
|
|
let name = plain_parse(html, &*ITEM_NAME)?;
|
|
|
|
let world = parse_world(html)?;
|
|
|
|
let active_members = parse_active_members(html)?;
|
|
|
|
|
|
|
|
Ok(LinkshellSearchItem {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
world,
|
|
|
|
active_members,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:58:06 +00:00
|
|
|
fn parse_id(html: ElementRef) -> Result<u64> {
|
2018-10-28 21:29:54 +00:00
|
|
|
let e = html
|
|
|
|
.select(&*ITEM_ID)
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| Error::missing_element(&*ITEM_ID))?;
|
|
|
|
crate::logic::parse_id(e.value())
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:58:06 +00:00
|
|
|
fn parse_world(html: ElementRef) -> Result<World> {
|
2019-04-24 23:26:38 +00:00
|
|
|
let parts_str = plain_parse(html, &*ITEM_WORLD)?;
|
2022-06-16 14:10:03 +00:00
|
|
|
let mut parts = parts_str.split(" [");
|
2019-04-24 23:26:38 +00:00
|
|
|
let world_str = parts.next()
|
|
|
|
.ok_or_else(|| Error::invalid_content("world with data centre in parens", Some(&parts_str)))?;
|
|
|
|
World::from_str(world_str)
|
2018-10-28 21:29:54 +00:00
|
|
|
.map_err(|_| Error::invalid_content("valid world", Some(&world_str)))
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:58:06 +00:00
|
|
|
fn parse_active_members(html: ElementRef) -> Result<u8> {
|
2018-10-28 21:29:54 +00:00
|
|
|
plain_parse(html, &*ITEM_ACTIVE_MEMBERS)
|
|
|
|
.and_then(|x| x.parse().map_err(Error::InvalidNumber))
|
|
|
|
}
|