pub struct Timeline {
pub count: i32,
pub max_id: Option<u64>,
pub min_id: Option<u64>,
/* private fields */
}
Expand description
Helper struct to navigate collections of tweets by requesting tweets older or newer than certain IDs.
Using a Timeline to navigate collections of tweets (like a user’s timeline, their list of likes, etc) allows you to efficiently cursor through a collection and only load in tweets you need.
To begin, call a method that returns a Timeline
, optionally set the page size, and call
start
to load the first page of results:
let timeline = egg_mode::tweet::home_timeline(&token).with_page_size(10);
let (timeline, feed) = timeline.start().await.unwrap();
for tweet in &*feed {
println!("<@{}> {}", tweet.user.as_ref().unwrap().screen_name, tweet.text);
}
If you need to load the next set of tweets, call older
, which will automatically update the
tweet IDs it tracks:
let (timeline, feed) = timeline.older(None).await.unwrap();
for tweet in &*feed {
println!("<@{}> {}", tweet.user.as_ref().unwrap().screen_name, tweet.text);
}
…and similarly for newer
, which operates in a similar fashion.
If you want to start afresh and reload the newest set of tweets again, you can call start
again, which will clear the tracked tweet IDs before loading the newest set of tweets. However,
if you’ve been storing these tweets as you go, and already know the newest tweet ID you have on
hand, you can load only those tweets you need like this:
let timeline = egg_mode::tweet::home_timeline(&token)
.with_page_size(10);
let (timeline, _feed) = timeline.start().await.unwrap();
//keep the max_id for later
let reload_id = timeline.max_id.unwrap();
//simulate scrolling down a little bit
let (timeline, _feed) = timeline.older(None).await.unwrap();
let (mut timeline, _feed) = timeline.older(None).await.unwrap();
//reload the timeline with only what's new
timeline.reset();
let (timeline, _new_posts) = timeline.older(Some(reload_id)).await.unwrap();
Here, the argument to older
means “older than what I just returned, but newer than the given
ID”. Since we cleared the tracked IDs with reset
, that turns into “the newest tweets
available that were posted after the given ID”. The earlier invocations of older
with None
do not place a bound on the tweets it loads. newer
operates in a similar fashion with its
argument, saying “newer than what I just returned, but not newer than this given ID”. When
called like this, it’s possible for these methods to return nothing, which will also clear the
Timeline
’s tracked IDs.
If you want to manually pull tweets between certain IDs, the baseline call
function can do
that for you. Keep in mind, though, that call
doesn’t update the min_id
or max_id
fields,
so you’ll have to set those yourself if you want to follow up with older
or newer
.
Fields§
§count: i32
The maximum number of tweets to return in a single call. Twitter doesn’t guarantee returning exactly this number, as suspended or deleted content is removed after retrieving the initial collection of tweets.
max_id: Option<u64>
The largest/most recent tweet ID returned in the last call to start
, older
, or newer
.
min_id: Option<u64>
The smallest/oldest tweet ID returned in the last call to start
, older
, or newer
.
Implementations§
source§impl Timeline
impl Timeline
sourcepub fn start(self) -> TimelineFuture ⓘ
pub fn start(self) -> TimelineFuture ⓘ
Clear the saved IDs on this timeline, and return the most recent set of tweets.
sourcepub fn older(self, since_id: Option<u64>) -> TimelineFuture ⓘ
pub fn older(self, since_id: Option<u64>) -> TimelineFuture ⓘ
Return the set of tweets older than the last set pulled, optionally placing a minimum tweet ID to bound with.
sourcepub fn newer(self, max_id: Option<u64>) -> TimelineFuture ⓘ
pub fn newer(self, max_id: Option<u64>) -> TimelineFuture ⓘ
Return the set of tweets newer than the last set pulled, optionall placing a maximum tweet ID to bound with.
sourcepub async fn call(
&self,
since_id: Option<u64>,
max_id: Option<u64>
) -> Result<Response<Vec<Tweet>>>
pub async fn call(
&self,
since_id: Option<u64>,
max_id: Option<u64>
) -> Result<Response<Vec<Tweet>>>
Return the set of tweets between the IDs given.
Note that the range is not fully inclusive; the tweet ID given by since_id
will not be
returned, but the tweet ID in max_id
will be returned.
If the range of tweets given by the IDs would return more than self.count
, the newest set
of tweets will be returned.
sourcepub fn with_page_size(self, page_size: i32) -> Self
pub fn with_page_size(self, page_size: i32) -> Self
Helper builder function to set the page size.