use bili::run;

fn main() {
    run();
}
#[allow(warnings)]
pub mod bili {
    pub fn get_page(url: &str) -> String {
        reqwest::blocking::get(url).unwrap().text().unwrap()
    }
    pub fn search_bili(keyword: &str, page: String) -> String {
        let words = keyword.replace(" ", "%20");
        get_page(&format!(
            "https://search.bilibili.com/all?vt=77538889&keyword={}&page={}",
            words, page
        ))
    }
    pub fn run(){
        let arg = doe::args!();
        if arg.len()==0{
            println!("require search keyword argument");
            return;
        }else{
            let keyword = arg.join(" ");
            use scraper::Html;
            use scraper::Selector;
            let body = search_bili(&keyword, "".to_string());
            let fragment = Html::parse_document(&body);
            let selector = Selector::parse("button").unwrap();
            let mut nums = vec![];
            for element in fragment.select(&selector) {
                if element.html().contains("pagination-btn") {
                    let mut v = doe::split_to_vec!(element.html().replace("\n", "fuck_num").replace("</button>", "fuck_num"), "fuck_num");
                    v.remove(v.len()-1);
                    let page_num = doe::vec_element_clone!(v,v.len()-1);
                    nums.push(page_num);
                }
            }
            // println!("{:?}",nums);
            if nums.last().is_none(){
                eprintln!("No related content was found~");
                return;
            }
            let last_num: &str = nums.last().expect("error to find last num");
            let mut herfs = std::collections::BTreeSet::new();
            for i in 1..=last_num.parse::<usize>().unwrap() {
                let body = search_bili(&keyword, i.to_string());
                let fragment = Html::parse_document(&body);
                let selector = Selector::parse("a").unwrap();
                for element in fragment.select(&selector) {
                    if element.html().contains("title=") {
                        use doe::split_to_vec;
                        use doe::vec_element_clone;
                        {
                            let v_herf = split_to_vec!(element.html().replace("\n", ""), "href=");
                            let v_title = split_to_vec!(element.html().replace("\n", ""), "title=");
                            let may_herf = vec_element_clone!(
                                split_to_vec!(
                                    vec_element_clone!(
                                        split_to_vec!(vec_element_clone!(v_herf, 1), " "),
                                        0
                                    ),
                                    "?from=search"
                                ),
                                0
                            )
                            .replace("\"//", "");
                            let may_title = vec_element_clone!(split_to_vec!(vec_element_clone!(v_title, 1).replace("amp","").replace("&","").replace("\">","\" "),"\" "),0).replace("\" ","").replace("\"","");
                            if !herfs.contains(&may_herf.to_string()){
                                herfs.insert(may_herf.to_string());
                                println!("{}", may_title);
                                println!("https://{}", may_herf);
                            }
                        }
                      
                    }
                }
            }
            println!("A total of search results count:{}", herfs.len());
        }
       
    }
}
