Struct TwAPI

Source
pub struct TwAPI { /* private fields */ }

Implementations§

Source§

impl TwAPI

Source

pub fn new(session_path: Option<PathBuf>) -> Result<TwAPI>

Examples found in repository?
examples/simple.rs (line 22)
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}
Source

pub async fn get_flow_token(&mut self, data: Value) -> Result<Option<Flow>>

Source

pub async fn before_password_steps(&mut self, username: String) -> Result<Flow>

Source

pub async fn login( &mut self, username: &str, password: &str, confirmation: &str, latest_flow: Option<Flow>, ) -> Result<Option<Flow>>

Examples found in repository?
examples/simple.rs (line 24)
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}
Source

pub async fn is_logged_in(&mut self) -> Result<bool>

Examples found in repository?
examples/simple.rs (line 38)
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}
Source

pub fn save_session(&mut self) -> Result<()>

Examples found in repository?
examples/simple.rs (line 35)
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}
Source§

impl TwAPI

Source

pub async fn search(&self, query: &str, limit: u8, cursor: &str) -> Result<Data>

Source

pub async fn search_tweets( &self, query: &str, limit: u8, cursor: &str, ) -> Result<(Vec<Tweet>, String)>

Source§

impl TwAPI

Source

pub async fn me(&self) -> Result<Value>

Source

pub fn me_following(&mut self)

Source

pub async fn me_rest_id(&mut self) -> Result<i64, Box<dyn Error>>

Examples found in repository?
examples/simple.rs (line 41)
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}
Source§

impl TwAPI

Source

pub async fn user_id( &mut self, username: String, ) -> Result<String, Box<dyn Error>>

Source

pub async fn get_friends( &mut self, user_id: i64, following: bool, start_cursor: Option<String>, ) -> Result<PaginationResponse, Box<dyn Error>>

Source

pub async fn get_follower_ids( &mut self, user_id: String, cursor: i32, ) -> Result<PaginationResponse>

Source

pub async fn get_following_ids( &mut self, user_id: String, cursor: i32, ) -> Result<PaginationResponse>

Examples found in repository?
examples/simple.rs (line 42)
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}
Source

pub async fn users_lookup(&mut self, ids: Vec<String>) -> Result<Vec<Value>>

Examples found in repository?
examples/simple.rs (line 45)
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}

Auto Trait Implementations§

§

impl Freeze for TwAPI

§

impl !RefUnwindSafe for TwAPI

§

impl Send for TwAPI

§

impl Sync for TwAPI

§

impl Unpin for TwAPI

§

impl !UnwindSafe for TwAPI

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,