Skip to content

Commit

Permalink
add follow and track fields to stream
Browse files Browse the repository at this point in the history
  • Loading branch information
adwhit committed Jun 9, 2019
1 parent 717b0b3 commit e90c910
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
33 changes: 33 additions & 0 deletions examples/stream_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
extern crate egg_mode;
extern crate futures;

mod common;
use common::futures::Stream;
use common::tokio::runtime::current_thread::block_on_all;

use egg_mode::stream::StreamMessage;

fn main() {
let config = common::Config::load();
println!("Streaming tweets containing popular programming languages (also Rust)");
println!("Ctrl-C to quit\n");

let stream = egg_mode::stream::filter()
.track(&["rustlang", "python", "java", "javascript"])
.start(&config.token)
.for_each(|m| {
use StreamMessage::*;
match m {
Tweet(tweet) => {
common::print_tweet(&tweet);
println!();
}
other => println!("{:?}", other),
}
futures::future::ok(())
});
if let Err(e) = block_on_all(stream) {
println!("Stream error: {}", e);
println!("Disconnected")
}
}
8 changes: 5 additions & 3 deletions examples/stream_sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ use egg_mode::stream::StreamMessage;

fn main() {
let config = common::Config::load();
println!("Printing messages from the 'sample' stream\nCtrl-C to quit\n");
println!("Streaming tweets from the Twitter 'sample' feed");
println!("Ctrl-C to quit\n");

let stream = egg_mode::stream::sample(&config.token).for_each(|m| {
use StreamMessage::*;
match m {
Tweet(tweet) => {
common::print_tweet(&tweet);
println!();
},
other => println!("{:?}", other)
}
other => println!("{:?}", other),
}
futures::future::ok(())
});
Expand Down
43 changes: 40 additions & 3 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//! Access to the Streaming API.
use std::collections::HashMap;
use std::collections::{BTreeSet, HashMap};
use std::str::FromStr;
use std::{self, io};

Expand Down Expand Up @@ -289,17 +289,35 @@ impl ::std::fmt::Display for FilterLevel {
/// Represents a `TwitterStream` before it is started.
pub struct StreamBuilder {
url: &'static str,
follow: BTreeSet<u64>,
track: BTreeSet<String>,
filter_level: Option<FilterLevel>,
}

impl StreamBuilder {
fn new(url: &'static str) -> StreamBuilder {
fn new(url: &'static str) -> Self {
StreamBuilder {
url: url,
follow: BTreeSet::new(),
track: BTreeSet::new(),
filter_level: None,
}
}

/// List of user IDs indicating the users whose Tweets should be delivered on the stream
pub fn follow(mut self, to_follow: &[u64]) -> Self {
self.follow.extend(to_follow.iter());
self
}

/// List of phrases which will be used to determine what Tweets will be delivered on the stream.
/// A phrase may be one or more terms separated by spaces, and a phrase will match if all
/// of the terms in the phrase are present in the Tweet, regardless of order and ignoring case.
pub fn track(mut self, to_track: &[&str]) -> Self {
self.track.extend(to_track.iter().map(|s| s.to_string()));
self
}

/// Applies the given `FilterLevel` to the stream. Tweets with a `filter_level` below the given
/// value will not be shown in the stream.
///
Expand All @@ -321,6 +339,26 @@ impl StreamBuilder {
add_param(&mut params, "filter_level", filter_level.to_string());
}

if !self.follow.is_empty() {
let to_follow = self
.follow
.iter()
.map(|id| id.to_string())
.collect::<Vec<String>>()
.join(",");
add_param(&mut params, "follow", to_follow);
}

if !self.track.is_empty() {
let to_track = self
.track
.iter()
.map(String::as_str)
.collect::<Vec<&str>>()
.join(",");
add_param(&mut params, "track", to_track);
}

let req = auth::post(self.url, token, Some(&params));

TwitterStream::new(req)
Expand All @@ -343,7 +381,6 @@ pub fn filter() -> StreamBuilder {
/// [`filter`]: fn.filter.html
pub fn sample(token: &Token) -> TwitterStream {
let req = auth::get(links::stream::SAMPLE, token, None);

TwitterStream::new(req)
}

Expand Down

0 comments on commit e90c910

Please sign in to comment.