1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::collections::HashMap;
use anyhow::Result;
use kuon::TwitterAPI;
pub struct TwitterClient {
api: TwitterAPI,
}
impl TwitterClient {
pub async fn new() -> Result<Self> {
let api = TwitterAPI::new_using_env().await?;
Ok(TwitterClient { api })
}
pub async fn tweet_long_text(&self, contents: &str) -> Result<()> {
let mut contents = contents.chars().collect::<Vec<_>>();
let mut status_id: Option<String> = None;
while !contents.is_empty() {
let bound = tweet_bound(&contents);
let tweet_contents = contents.drain(..bound).collect::<String>();
if let Some(in_reply_to_status_id) = status_id {
let reply_id = self.reply(&tweet_contents, &in_reply_to_status_id).await?;
status_id = Some(reply_id);
} else {
let tweet_result = self.api.tweet(&tweet_contents).await?;
status_id = Some(tweet_result.id.to_string());
}
}
Ok(())
}
async fn reply(&self, contents: &str, in_reply_to_status_id: &str) -> Result<String> {
let params: HashMap<&str, &str> = vec![
("in_reply_to_status_id", in_reply_to_status_id),
("auto_populate_reply_metadata", "true"),
]
.into_iter()
.collect();
let reply_result = self.api.tweet_with_params(contents, ¶ms).await?;
Ok(reply_result.id.to_string())
}
}
fn tweet_bound(contents: &[char]) -> usize {
let mut remain_num_of_char = 279;
let mut bound = 0;
for &c in contents {
remain_num_of_char -= if c.is_ascii() { 1 } else { 2 };
bound += 1;
if remain_num_of_char <= 0 {
break;
}
}
bound
}