asdf.rs | egg-mode

asdf.rs

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

extern crate egg_mode;

mod common;

use common::tokio_core::reactor;
use common::futures::Stream;

use egg_mode::stream::{self, StreamMessage};

fn main() {
    let mut core = reactor::Core::new().unwrap();

    let c = common::Config::load(&mut core);
    let handle = core.handle();

    let stream = stream::user().start(&handle, &c.token);

    core.run(stream.for_each(|msg| {
        match msg {
            StreamMessage::Tweet(tweet) => {
                println!("");
                common::print_tweet(&tweet);
            }
            StreamMessage::DirectMessage(dm) => {
                if dm.sender_id != c.user_id {
                    println!("");
                    println!("    Direct message from {} (@{}):",
                             dm.sender.name, dm.sender_screen_name);
                    println!("{}", dm.text);
                }
            }
            StreamMessage::Like(_, ref user, ref tweet) if user.id != c.user_id => {
                let text = if let Some(rng) = tweet.display_text_range {
                    &tweet.text[rng.0..rng.1]
                } else {
                    &tweet.text
                };
                println!("");
                println!("{} liked your tweet:", user.name);
                println!("{}", text);
            }
            StreamMessage::Unknown(msg) => println!("{}", msg.pretty()),
            _ => {}
        }
        Ok(())
    })).unwrap();
}