3 releases (breaking)
0.3.0 | Jun 17, 2019 |
---|---|
0.2.0 | Jun 16, 2019 |
0.1.0 | Jun 10, 2019 |
#46 in #twitter
28KB
454 lines
tweet
This library is for deserializing data from the Twitter API.
I created this since I couldn't find a way to deserialize data while using twitter-stream
, and twitter-stream-message
is 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 things with no concrete documentation or things that I cannot access like PowerTrack and enterprise endpoint responses.
Simple example
use std::str::FromStr;
use tweet::Tweet;
Tweet::from_str(&json)
Usage with twitter-stream
use twitter_stream::{Token, TwitterStreamBuilder};
use twitter_stream::rt::{self, Future, Stream};
use tweet::TwitterResponse;
// Import your keys however you want
let token = Token::new(
dotenv!("TW_API"), dotenv!("TW_SEC"),
dotenv!("TW_ACC_KEY"), dotenv!("TW_ACC_SEC"));
let future = TwitterStreamBuilder::filter(token)
.timeout(None)
.track(Some("cat, dog, rabbit"))
.listen().unwrap()
.flatten_stream()
.for_each(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));
Dependencies
~1.6–2.8MB
~52K SLoC