Make list-tweets fetch all rather than last 200

This commit is contained in:
Sam W 2022-09-13 19:07:50 +01:00
parent 25d3846e1c
commit 73ae1a8613
1 changed files with 36 additions and 19 deletions

View File

@ -9,6 +9,7 @@ use image::{DynamicImage, RgbaImage};
use tiny_skia::{Paint, PathBuilder, Pixmap, PixmapPaint, Stroke, Transform};
use twitter::*;
use wiki::*;
use std::borrow::Cow;
static APP_USER_AGENT: &str = concat!(
"bot_",
@ -143,25 +144,41 @@ fn do_list_tweets() -> StdError<()> {
)?
.json::<serde_json::Value>()?;
let id = user["id"].as_u64().unwrap();
let timeline: serde_json::Value = twitter_api(
reqwest::Url::parse_with_params(
&TwitterEndpoint::UserTimeline.to_string(),
[
("count", "200"),
("exclude_replies", "true"),
("include_retweets", "false"),
("trim_user", "true"),
("user_id", id.to_string().as_ref()),
],
)?,
Some(&user_token),
APIAction::Get,
&[],
)?
.json()?;
for tweet in timeline.as_array().unwrap() {
let id = user["id"].as_u64().unwrap().to_string();
let mut timeline = vec!();
let mut last_id: Option<u64> = None;
loop {
event!(Level::INFO, last_id, "Fetching chunk of tweets...");
let mut params: Vec<(&str, Cow<str>)> = vec!(
("count", "200".into()),
("exclude_replies", "true".into()),
("include_rts", "false".into()),
("trim_user", "true".into()),
("user_id", Cow::Borrowed(&id)),
);
if let Some(since_id) = last_id {
// Get next chunk of tweets before those we've fetched
params.push(("max_id", (since_id - 1).to_string().into()))
}
let timeline_chunk = twitter_api(
reqwest::Url::parse_with_params(
&TwitterEndpoint::UserTimeline.to_string(),
params,
)?,
Some(&user_token),
APIAction::Get,
&[],
)?
.json::<serde_json::Value>()?;
let chunk = timeline_chunk.as_array().unwrap().to_owned();
event!(Level::INFO, count=chunk.len(), "Got tweets.");
if chunk.len() == 0 {
break;
}
last_id = Some(chunk.last().unwrap().as_object().unwrap()["id"].as_u64().unwrap());
timeline.extend(chunk);
}
for tweet in timeline {
let tweet = tweet.as_object().unwrap();
println!("{}, \"{}\"", tweet["id"], tweet["text"]);
}