Use quick-xml

This commit is contained in:
blank X 2021-01-04 13:39:14 +07:00
parent f16f3db8e4
commit 332d5db7f2
3 changed files with 101 additions and 21 deletions

12
Cargo.lock generated
View File

@ -198,8 +198,9 @@ checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
[[package]] [[package]]
name = "holocal" name = "holocal"
version = "0.1.0" version = "0.1.2"
dependencies = [ dependencies = [
"quick-xml",
"reqwest", "reqwest",
"tokio", "tokio",
] ]
@ -562,6 +563,15 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "quick-xml"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26aab6b48e2590e4a64d1ed808749ba06257882b461d01ca71baeb747074a6dd"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.8" version = "1.0.8"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "holocal" name = "holocal"
version = "0.1.1" version = "0.1.2"
authors = ["blank X <theblankx@protonmail.com>"] authors = ["blank X <theblankx@protonmail.com>"]
edition = "2018" edition = "2018"
@ -12,3 +12,4 @@ lto = true
[dependencies] [dependencies]
reqwest = { version = "0.10.10", features = ["default-tls"] } reqwest = { version = "0.10.10", features = ["default-tls"] }
tokio = { version = "0.2.22", features = ["rt-core"] } tokio = { version = "0.2.22", features = ["rt-core"] }
quick-xml = "0.20"

View File

@ -5,6 +5,8 @@ use std::io::{stdin, BufRead};
use std::process::{exit, Command}; use std::process::{exit, Command};
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
use reqwest::Client; use reqwest::Client;
use quick_xml::Reader;
use quick_xml::events::Event;
extern crate tokio; extern crate tokio;
fn main() { fn main() {
@ -68,25 +70,92 @@ fn main() {
let text = tokio::runtime::Runtime::new() let text = tokio::runtime::Runtime::new()
.unwrap() .unwrap()
.block_on(get_feed_text(nitter_instance, &account)); .block_on(get_feed_text(nitter_instance, &account));
match &text.splitn(4, "<title>").nth(2) { let mut is_inside_item = false;
Some(title) => println!("{}", &title.splitn(2, "</title>").nth(0).unwrap()), let mut is_inside_title = false;
None => { let mut is_inside_description = false;
eprintln!("Cannot find tweet"); let mut title: Option<String> = None;
exit(1); let mut url: Option<String> = None;
} let mut reader = Reader::from_str(&text);
}; let mut buf = Vec::new();
let image_url = text.splitn(3, "<img src=\"") loop {
.nth(1) match reader.read_event(&mut buf) {
.unwrap() Ok(Event::Start(ref e)) => {
.splitn(2, "\"") match String::from_utf8(e.name().to_vec()).unwrap().as_str() {
.nth(0) "item" => {
.unwrap(); is_inside_item = true;
let mut args = Vec::with_capacity(view_image_command.len() - 1); title = None;
for arg in &view_image_command[1..] { url = None;
if arg == &"{}" { },
args.push(image_url); "title" => is_inside_title = true,
} else { "description" => is_inside_description = true,
args.push(&arg); _ => ()
};
},
Ok(Event::Text(e)) => {
if is_inside_item && is_inside_title {
title = Some(e.unescape_and_decode(&reader).unwrap());
}
},
Ok(Event::CData(e)) => {
if is_inside_item && is_inside_description {
let description_text = e.unescape_and_decode(&reader).unwrap();
let mut description_reader = Reader::from_str(&description_text);
let mut description_buf = Vec::new();
loop {
match description_reader.read_event(&mut description_buf) {
Ok(Event::Empty(ref e)) => {
if String::from_utf8(e.name().to_vec()).unwrap().as_str() == "img" {
url = Some(e.attributes()
.find(|attribute| String::from_utf8(attribute.as_ref().unwrap().key.to_vec()).unwrap().as_str() == "src")
.unwrap()
.unwrap()
.unescape_and_decode_value(&description_reader)
.unwrap());
}
},
Err(err) => panic!("Error at position {}: {:?}", description_reader.buffer_position(), err),
Ok(Event::Eof) => break,
_ => ()
}
}
}
},
Ok(Event::End(ref e)) => {
match String::from_utf8(e.name().to_vec()).unwrap().as_str() {
"item" => {
is_inside_item = false;
if title.is_some() && url.is_some() {
break;
}
},
"description" => {
is_inside_description = false;
if title.is_some() && url.is_some() {
break;
}
},
"title" => is_inside_title = false,
_ => ()
};
},
Err(err) => panic!("Error at position {}: {:?}", reader.buffer_position(), err),
Ok(Event::Eof) => {
if title.is_none() || url.is_none() {
eprintln!("Cannot find tweet");
exit(1);
}
break;
},
_ => ()
};
buf.clear();
}
println!("{}", title.unwrap());
let mut args = view_image_command.to_vec().split_off(1);
let url = url.unwrap();
for i in 0..args.len() {
if args[i] == "{}" {
args[i] = url.as_str();
} }
} }
Command::new(&view_image_command[0]) Command::new(&view_image_command[0])