tweet
This library is for deserializing data from the Twitter API.
I ended up creating this since I couldn't find a way to deserialize data while using twitter-stream
, and twitter-stream-message
seems to be an abandoned project that does not work currently.
If there is anything missing feel free to create an issue. I tried to add every field regardless of potential use, but there were some I left out; mainly enrichment stuff, or things with no concrete docs I could find.
```rust use std::str::FromStr; use tweet::Tweet;
Tweet::from_str(&json) ```
```rust use twitterstream::{Token, TwitterStreamBuilder}; use twitterstream::rt::{self, Future, Stream}; use tweet::TwitterResponse;
// Import your keys however you want let token = Token::new( dotenv!("TWAPI"), dotenv!("TWSEC"), dotenv!("TWACCKEY"), dotenv!("TWACCSEC"));
let future = TwitterStreamBuilder::filter(token) .timeout(None) .track(Some("cat, dog, rabbit")) .listen().unwrap() .flattenstream() .foreach(move |json| { // A twitter stream just sends us raw JSON responses, and those // responses can contain a Tweet or a Limit payload. TwitterResponse // encapsulates deserializing this variable payload. Without it there // is a possibility of trying to deserialize a Limit as a Tweet and // getting a deserialization error. let tweet = match TwitterResponse::from_str(&json) { // Return the tweet so we can use it Ok(TwitterResponse::Tweet(tweet)) => tweet, // Just print out limit information if we get it and return Ok(TwitterResponse::Limit(limit)) => { println!("Got a limit: {:#?}", limit); return Ok(()); } // If something goes wrong, print the error and the payload Err(why) => { println!("Error: {:?}\nPayload: {}", why, json); return Ok(()); } };
// Use tweet however you want
println!("Tweet URL: {}", tweet.url());
Ok(())
})
.map_err(|e| println!("Error: {}", e));
```