use dialoguer::console::Term;
use dialoguer::{theme::ColorfulTheme, Select};
use html_editor::prelude::*;
use rphtml::config::{ParseOptions, RenderOptions};
use rphtml::parser::Doc;
#[derive(Clone, Debug)]
struct Row {
pub build: String,
pub arch: String,
pub date: String,
pub id: String,
}
impl ToString for Row {
fn to_string(&self) -> String {
format!(
"{: <8} {: <80} {: <44} {}",
self.arch, self.build, self.id, self.date
)
.clone()
}
}
fn main() -> Result<(), somehow::Error> {
let index = reqwest::blocking::get("https://uupdump.net/")?.text()?;
let doc = Doc::parse(
&index,
ParseOptions {
case_sensitive_tagname: false, allow_self_closing: true, auto_fix_unclosed_tag: true, auto_fix_unexpected_endtag: true, auto_fix_unescaped_lt: true, },
)?;
let render_html = doc.render(&RenderOptions {
..Default::default() });
let index = format!("{}", render_html);
let index = html_editor::parse(&index)?;
let selector = html_editor::Selector::from("table");
let elements = index.query_all(&selector);
let table = elements[0].html();
let table = table_extract::Table::find_by_headers(&table, &["Build"]).unwrap();
let mut rows = Vec::<Row>::new();
for row in &table {
if let [build, arch, date, id] = row.iter().collect::<Vec<_>>()[..] {
let builds = html_editor::parse(&build)?;
if let [_, html_editor::Node::Element {
name: _,
attrs: _,
children: nodes,
}] = &builds[..]
{
if let html_editor::Node::Text(ref build_text) = nodes[0] {
let id = html_editor::parse(&id)?;
if let html_editor::Node::Element {
name: _,
attrs: _,
children: nodes,
} = &id[0]
{
if let html_editor::Node::Text(ref id_text) = nodes[0] {
rows.push(Row {
build: build_text.to_string(),
arch: arch.to_string(),
date: date.to_string(),
id: id_text.to_string(),
})
}
}
}
}
}
}
let selection = Select::with_theme(&ColorfulTheme::default())
.items(&rows.clone())
.default(0)
.interact_on_opt(&Term::stderr())?;
if let Some(index) = selection {
println!("{}", rows[index].id);
}
Ok(())
}