From d831cff595f5c30b11320c0f653b6cceabd16a4c Mon Sep 17 00:00:00 2001 From: iximeow Date: Wed, 15 Nov 2017 23:17:57 -0800 Subject: move line wrapping into render_twete this is, hopefully, the start of support for correctly wrapping lines with ANSI control codes included. additionally, hopefully the start of supporting coloring @'s in tweets. --- src/tw/mod.rs | 14 -------------- src/tw/tweet.rs | 10 ++++++++++ 2 files changed, 10 insertions(+), 14 deletions(-) (limited to 'src/tw') diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 08ec974..7024260 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -78,20 +78,6 @@ pub fn full_twete_text(twete: &serde_json::map::Map) .replace(">", ">") .replace("<", "<"); - for url in twete["entities"]["urls"].as_array().unwrap() { - let display_url = url["url"].as_str().unwrap(); - let expanded_url = url["expanded_url"].as_str().unwrap(); - if expanded_url.len() < 200 { - if let Some(twid) = quoted_tweet_id { - if expanded_url.ends_with(twid) { - twete_text = twete_text.replace(display_url, ""); - continue; - } - } - twete_text = twete_text.replace(display_url, expanded_url); - } - } - twete_text } diff --git a/src/tw/tweet.rs b/src/tw/tweet.rs index dc89774..38b838d 100644 --- a/src/tw/tweet.rs +++ b/src/tw/tweet.rs @@ -1,5 +1,7 @@ extern crate serde_json; +use std::collections::HashMap; + use tw::user::User; #[derive(Debug, Serialize, Deserialize, Clone)] @@ -8,6 +10,9 @@ pub struct Tweet { pub author_id: String, pub text: String, pub created_at: String, // lol + #[serde(skip_serializing_if="HashMap::is_empty")] + #[serde(default = "HashMap::default")] + pub urls: HashMap, #[serde(skip_serializing_if="Option::is_none")] #[serde(default = "Option::default")] pub quoted_tweet_id: Option, @@ -50,6 +55,10 @@ impl Tweet { } pub fn from_json(json: serde_json::Value) -> Result { if let serde_json::Value::Object(json_map) = json { + let mut url_map: HashMap = HashMap::new(); + for entry in json_map["entities"]["urls"].as_array().unwrap() { + url_map.insert(entry["url"].as_str().unwrap().to_owned(), entry["expanded_url"].as_str().unwrap().to_owned()); + } let text = ::tw::full_twete_text(&json_map); let rt_twete = json_map.get("retweeted_status") .and_then(|x| x.get("id_str")) @@ -75,6 +84,7 @@ impl Tweet { author_id: author_id.to_owned(), text: text, created_at: created_at.to_owned(), + urls: url_map, quoted_tweet_id: json_map.get("quoted_status_id_str") .and_then(|x| x.as_str()) .map(|x| x.to_owned()), -- cgit v1.1