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 tiny_skia::{Paint, PathBuilder, Pixmap, PixmapPaint, Stroke, Transform};
use twitter::*; use twitter::*;
use wiki::*; use wiki::*;
use std::borrow::Cow;
static APP_USER_AGENT: &str = concat!( static APP_USER_AGENT: &str = concat!(
"bot_", "bot_",
@ -143,25 +144,41 @@ fn do_list_tweets() -> StdError<()> {
)? )?
.json::<serde_json::Value>()?; .json::<serde_json::Value>()?;
let id = user["id"].as_u64().unwrap(); let id = user["id"].as_u64().unwrap().to_string();
let mut timeline = vec!();
let timeline: serde_json::Value = twitter_api( 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( reqwest::Url::parse_with_params(
&TwitterEndpoint::UserTimeline.to_string(), &TwitterEndpoint::UserTimeline.to_string(),
[ params,
("count", "200"),
("exclude_replies", "true"),
("include_retweets", "false"),
("trim_user", "true"),
("user_id", id.to_string().as_ref()),
],
)?, )?,
Some(&user_token), Some(&user_token),
APIAction::Get, APIAction::Get,
&[], &[],
)? )?
.json()?; .json::<serde_json::Value>()?;
for tweet in timeline.as_array().unwrap() { 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(); let tweet = tweet.as_object().unwrap();
println!("{}, \"{}\"", tweet["id"], tweet["text"]); println!("{}, \"{}\"", tweet["id"], tweet["text"]);
} }