Skip to main content

simple/
simple.rs

1
2use std::path::PathBuf;
3use log::debug;
4use x_api_rs::auth::SuspiciousLoginError;
5
6fn read_string() -> String {
7    let mut input = String::new();
8    std::io::stdin()
9        .read_line(&mut input)
10        .expect("can not read user input");
11    input
12}
13
14#[tokio::main]
15async fn main() -> Result<(), Box<dyn std::error::Error>> {
16
17    let cookies_path = PathBuf::from("cookies.json");
18    let username = std::env::var("USERNAME")?;
19    let password = std::env::var("PASSWORD")?;
20    debug!("username: {username}");
21    debug!("password: {password}");
22    let mut api = x_api_rs::TwAPI::new(Some(cookies_path.clone()))?;
23    if !cookies_path.exists() {
24        let result = api.login(&username, &password, "", None).await;
25        match result {
26            Err(err) => {
27                let error = err.downcast_ref::<SuspiciousLoginError>().unwrap();
28                println!("Enter your username (eg. @user): ");
29                let username = read_string();
30                api.login(&username, &password, "".into(), Some(error.1.clone())).await?;
31            }
32            Ok(_) => {}
33        }
34        
35        api.save_session().unwrap();
36    }
37    // always call this for extract csrf
38    let is_logged_in = api.is_logged_in().await?;
39    println!("is logged: {is_logged_in}");
40    
41    let user_id = api.me_rest_id().await?;
42    let res = api.get_following_ids(user_id.to_string(), -1).await?;
43    debug!("res is {res:?}");
44    let ids = res.entries.iter().map(|v| v.as_i64().unwrap_or_default().to_string()).collect();
45    let res = api.users_lookup(ids).await?;
46    debug!("res is {res:?}");
47    // loop {
48    //     let pagination = api.get_friends(user_id, true, Some(cursor.into()))?;
49    //     cursor = pagination.cursor.clone();
50    //     debug!("Found {:?} following", pagination.entries.len());
51    //     if !pagination.has_more {
52    //         break;
53    //     }
54    // }
55    Ok(())
56    
57}