diff options
author | Your Name <you@example.com> | 2020-05-03 20:06:12 +0200 |
---|---|---|
committer | Your Name <you@example.com> | 2020-05-03 20:06:12 +0200 |
commit | a3d33d3e680f19c0d61911d64d9e4c30a07f55ab (patch) | |
tree | bfd791d4c0ec9430511a2431d1f04c969f552442 /examples/zot/network_stream.rs | |
parent | eb917c35008d32b3b27bf132b5f831f05888be54 (diff) | |
download | rust-zotapi-a3d33d3e680f19c0d61911d64d9e4c30a07f55ab.tar.gz rust-zotapi-a3d33d3e680f19c0d61911d64d9e4c30a07f55ab.tar.bz2 rust-zotapi-a3d33d3e680f19c0d61911d64d9e4c30a07f55ab.zip |
Promote example app to proper command line client.
Diffstat (limited to 'examples/zot/network_stream.rs')
-rw-r--r-- | examples/zot/network_stream.rs | 104 |
1 files changed, 0 insertions, 104 deletions
diff --git a/examples/zot/network_stream.rs b/examples/zot/network_stream.rs deleted file mode 100644 index 0e6edb4..0000000 --- a/examples/zot/network_stream.rs +++ /dev/null @@ -1,104 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen <haraldei@anduin.net> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -use zotapi; - -use serde_json; -use std::iter::Iterator; - -pub fn fetch(client: &zotapi::Client, raw: bool) { - match zotapi::network_stream().fetch(&client) { - Ok(payload) => { - if raw { - println!("{}", payload); - } else { - list(&payload); - } - } - Err(e) => { - println!("Error getting channel stream: {:?}", e); - } - } -} - -fn list(payload: &str) { - match serde_json::from_str(&payload) { - Ok(serde_json::Value::Array(v)) => { - for item in v.into_iter() { - print_item(&item); - println!("-----"); - } - } - Ok(_) => println!("Wrong type returned, expected an array."), - Err(e) => { - println!("Error: {}", e); - } - } -} - -fn get_str(v: &serde_json::Value) -> &str { - v.as_str().unwrap_or("") -} - -fn get_verb(item: &serde_json::Value) -> &str { - match get_str(&item["verb"]).split("/").last() { - Some("post") => "posted", - Some("like") => "liked", - Some("share") => "shared", - Some("dislike") => "disliked", - _ => "unknown verb", - } -} - -fn get_object_type(item: &serde_json::Value) -> &str { - get_str(&item["object_type"]) - .split("/") - .last() - .unwrap_or("unknown object") -} - -fn get_tags(item: &serde_json::Value) -> String { - match item["tags"] { - serde_json::Value::Array(ref v) => v - .iter() - .map(|t| { - let prefix = match get_str(&t["type"]) { - "hashtag" => "#", - "forum" => "!", - "mention" => "@", - _ => "", - }; - format!("{}{}", prefix, get_str(&t["tag"])) - }) - .collect::<Vec<_>>() - .join(", "), - serde_json::Value::Null => String::new(), - _ => String::from("invalid tags, expected array..."), - } -} - -fn print_item(item: &serde_json::Value) { - let author = &item["author"]; - print!("{} ", get_str(&item["created"])); - print!("{} ", get_str(&author["name"])); - println!("{} a {}", get_verb(&item), get_object_type(&item)); - println!("URL: {}", get_str(&author["url"])); - println!("Proto: {}", get_str(&author["network"])); - println!("Title: {}", get_str(&item["title"])); - println!("Tags : {}", get_tags(&item)); - println!("Message:\n{}", get_str(&item["body"])); -} |