From fc7735d33a562f71729e96514b722db7d6f55f05 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 8 Oct 2018 13:48:11 +0200 Subject: Add fetching network_stream to main example. This duplicates a lot of the code from the channel_stream example, will try to refactor later. --- examples/network_stream.rs | 109 +++++++++++++++++++++++++++++++++++++++++++++ examples/zot.rs | 15 ++++++- 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 examples/network_stream.rs (limited to 'examples') diff --git a/examples/network_stream.rs b/examples/network_stream.rs new file mode 100644 index 0000000..de3b12f --- /dev/null +++ b/examples/network_stream.rs @@ -0,0 +1,109 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * 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 . + */ + +use zotapi; + +use serde_json; +use std::iter::Iterator; + +pub fn fetch(client: &zotapi::Client, raw: bool) { + match client.network_stream() { + 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::>().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"])); +} diff --git a/examples/zot.rs b/examples/zot.rs index a0d930e..2c49fe3 100644 --- a/examples/zot.rs +++ b/examples/zot.rs @@ -26,6 +26,7 @@ use dotenv::dotenv; use std::env; mod channel_stream; +mod network_stream; fn main() { dotenv().ok(); @@ -40,12 +41,24 @@ fn main() { .arg(Arg::with_name("raw") .long("raw") .help("Display raw json payload"))) + .subcommand( + SubCommand::with_name("network-stream") + .about("Fetch the network stream") + .arg(Arg::with_name("raw") + .long("raw") + .help("Display raw json payload"))) .get_matches(); + let client = zotapi::client(&site, &user, &password); + match matches.subcommand() { ("channel-stream", Some(m)) => { let raw = m.is_present("raw"); - channel_stream::fetch(&zotapi::client(&site, &user, &password), raw); + channel_stream::fetch(&client, raw); + }, + ("network-stream", Some(m)) => { + let raw = m.is_present("raw"); + network_stream::fetch(&client, raw); }, _ => { println!("{}", matches.usage()); -- cgit v1.2.3