Struct x_api_rs::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)
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
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let cookies_path = PathBuf::from("cookies.json");
    let username = std::env::var("USERNAME")?;
    let password = std::env::var("PASSWORD")?;
    debug!("username: {username}");
    debug!("password: {password}");
    let mut api = x_api_rs::TwAPI::new(Some(cookies_path.clone()))?;
    if !cookies_path.exists() {
        let result = api.login(&username, &password, "", None).await;
        match result {
            Err(err) => {
                let error = err.downcast_ref::<SuspiciousLoginError>().unwrap();
                println!("Enter your username (eg. @user): ");
                let username = read_string();
                api.login(&username, &password, "".into(), Some(error.1.clone())).await?;
            }
            Ok(_) => {}
        }
        
        api.save_session().unwrap();
    }
    // always call this for extract csrf
    let is_logged_in = api.is_logged_in().await?;
    println!("is logged: {is_logged_in}");
    
    let user_id = api.me_rest_id().await?;
    let res = api.get_following_ids(user_id.to_string(), -1).await?;
    debug!("res is {res:?}");
    let ids = res.entries.iter().map(|v| v.as_i64().unwrap_or_default().to_string()).collect();
    let res = api.users_lookup(ids).await?;
    debug!("res is {res:?}");
    // loop {
    //     let pagination = api.get_friends(user_id, true, Some(cursor.into()))?;
    //     cursor = pagination.cursor.clone();
    //     debug!("Found {:?} following", pagination.entries.len());
    //     if !pagination.has_more {
    //         break;
    //     }
    // }
    Ok(())
    
}
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)
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
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let cookies_path = PathBuf::from("cookies.json");
    let username = std::env::var("USERNAME")?;
    let password = std::env::var("PASSWORD")?;
    debug!("username: {username}");
    debug!("password: {password}");
    let mut api = x_api_rs::TwAPI::new(Some(cookies_path.clone()))?;
    if !cookies_path.exists() {
        let result = api.login(&username, &password, "", None).await;
        match result {
            Err(err) => {
                let error = err.downcast_ref::<SuspiciousLoginError>().unwrap();
                println!("Enter your username (eg. @user): ");
                let username = read_string();
                api.login(&username, &password, "".into(), Some(error.1.clone())).await?;
            }
            Ok(_) => {}
        }
        
        api.save_session().unwrap();
    }
    // always call this for extract csrf
    let is_logged_in = api.is_logged_in().await?;
    println!("is logged: {is_logged_in}");
    
    let user_id = api.me_rest_id().await?;
    let res = api.get_following_ids(user_id.to_string(), -1).await?;
    debug!("res is {res:?}");
    let ids = res.entries.iter().map(|v| v.as_i64().unwrap_or_default().to_string()).collect();
    let res = api.users_lookup(ids).await?;
    debug!("res is {res:?}");
    // loop {
    //     let pagination = api.get_friends(user_id, true, Some(cursor.into()))?;
    //     cursor = pagination.cursor.clone();
    //     debug!("Found {:?} following", pagination.entries.len());
    //     if !pagination.has_more {
    //         break;
    //     }
    // }
    Ok(())
    
}
source

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

Examples found in repository?
examples/simple.rs (line 38)
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
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let cookies_path = PathBuf::from("cookies.json");
    let username = std::env::var("USERNAME")?;
    let password = std::env::var("PASSWORD")?;
    debug!("username: {username}");
    debug!("password: {password}");
    let mut api = x_api_rs::TwAPI::new(Some(cookies_path.clone()))?;
    if !cookies_path.exists() {
        let result = api.login(&username, &password, "", None).await;
        match result {
            Err(err) => {
                let error = err.downcast_ref::<SuspiciousLoginError>().unwrap();
                println!("Enter your username (eg. @user): ");
                let username = read_string();
                api.login(&username, &password, "".into(), Some(error.1.clone())).await?;
            }
            Ok(_) => {}
        }
        
        api.save_session().unwrap();
    }
    // always call this for extract csrf
    let is_logged_in = api.is_logged_in().await?;
    println!("is logged: {is_logged_in}");
    
    let user_id = api.me_rest_id().await?;
    let res = api.get_following_ids(user_id.to_string(), -1).await?;
    debug!("res is {res:?}");
    let ids = res.entries.iter().map(|v| v.as_i64().unwrap_or_default().to_string()).collect();
    let res = api.users_lookup(ids).await?;
    debug!("res is {res:?}");
    // loop {
    //     let pagination = api.get_friends(user_id, true, Some(cursor.into()))?;
    //     cursor = pagination.cursor.clone();
    //     debug!("Found {:?} following", pagination.entries.len());
    //     if !pagination.has_more {
    //         break;
    //     }
    // }
    Ok(())
    
}
source

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

Examples found in repository?
examples/simple.rs (line 35)
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
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let cookies_path = PathBuf::from("cookies.json");
    let username = std::env::var("USERNAME")?;
    let password = std::env::var("PASSWORD")?;
    debug!("username: {username}");
    debug!("password: {password}");
    let mut api = x_api_rs::TwAPI::new(Some(cookies_path.clone()))?;
    if !cookies_path.exists() {
        let result = api.login(&username, &password, "", None).await;
        match result {
            Err(err) => {
                let error = err.downcast_ref::<SuspiciousLoginError>().unwrap();
                println!("Enter your username (eg. @user): ");
                let username = read_string();
                api.login(&username, &password, "".into(), Some(error.1.clone())).await?;
            }
            Ok(_) => {}
        }
        
        api.save_session().unwrap();
    }
    // always call this for extract csrf
    let is_logged_in = api.is_logged_in().await?;
    println!("is logged: {is_logged_in}");
    
    let user_id = api.me_rest_id().await?;
    let res = api.get_following_ids(user_id.to_string(), -1).await?;
    debug!("res is {res:?}");
    let ids = res.entries.iter().map(|v| v.as_i64().unwrap_or_default().to_string()).collect();
    let res = api.users_lookup(ids).await?;
    debug!("res is {res:?}");
    // loop {
    //     let pagination = api.get_friends(user_id, true, Some(cursor.into()))?;
    //     cursor = pagination.cursor.clone();
    //     debug!("Found {:?} following", pagination.entries.len());
    //     if !pagination.has_more {
    //         break;
    //     }
    // }
    Ok(())
    
}
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)
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
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let cookies_path = PathBuf::from("cookies.json");
    let username = std::env::var("USERNAME")?;
    let password = std::env::var("PASSWORD")?;
    debug!("username: {username}");
    debug!("password: {password}");
    let mut api = x_api_rs::TwAPI::new(Some(cookies_path.clone()))?;
    if !cookies_path.exists() {
        let result = api.login(&username, &password, "", None).await;
        match result {
            Err(err) => {
                let error = err.downcast_ref::<SuspiciousLoginError>().unwrap();
                println!("Enter your username (eg. @user): ");
                let username = read_string();
                api.login(&username, &password, "".into(), Some(error.1.clone())).await?;
            }
            Ok(_) => {}
        }
        
        api.save_session().unwrap();
    }
    // always call this for extract csrf
    let is_logged_in = api.is_logged_in().await?;
    println!("is logged: {is_logged_in}");
    
    let user_id = api.me_rest_id().await?;
    let res = api.get_following_ids(user_id.to_string(), -1).await?;
    debug!("res is {res:?}");
    let ids = res.entries.iter().map(|v| v.as_i64().unwrap_or_default().to_string()).collect();
    let res = api.users_lookup(ids).await?;
    debug!("res is {res:?}");
    // loop {
    //     let pagination = api.get_friends(user_id, true, Some(cursor.into()))?;
    //     cursor = pagination.cursor.clone();
    //     debug!("Found {:?} following", pagination.entries.len());
    //     if !pagination.has_more {
    //         break;
    //     }
    // }
    Ok(())
    
}
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)
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
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let cookies_path = PathBuf::from("cookies.json");
    let username = std::env::var("USERNAME")?;
    let password = std::env::var("PASSWORD")?;
    debug!("username: {username}");
    debug!("password: {password}");
    let mut api = x_api_rs::TwAPI::new(Some(cookies_path.clone()))?;
    if !cookies_path.exists() {
        let result = api.login(&username, &password, "", None).await;
        match result {
            Err(err) => {
                let error = err.downcast_ref::<SuspiciousLoginError>().unwrap();
                println!("Enter your username (eg. @user): ");
                let username = read_string();
                api.login(&username, &password, "".into(), Some(error.1.clone())).await?;
            }
            Ok(_) => {}
        }
        
        api.save_session().unwrap();
    }
    // always call this for extract csrf
    let is_logged_in = api.is_logged_in().await?;
    println!("is logged: {is_logged_in}");
    
    let user_id = api.me_rest_id().await?;
    let res = api.get_following_ids(user_id.to_string(), -1).await?;
    debug!("res is {res:?}");
    let ids = res.entries.iter().map(|v| v.as_i64().unwrap_or_default().to_string()).collect();
    let res = api.users_lookup(ids).await?;
    debug!("res is {res:?}");
    // loop {
    //     let pagination = api.get_friends(user_id, true, Some(cursor.into()))?;
    //     cursor = pagination.cursor.clone();
    //     debug!("Found {:?} following", pagination.entries.len());
    //     if !pagination.has_more {
    //         break;
    //     }
    // }
    Ok(())
    
}
source

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

Examples found in repository?
examples/simple.rs (line 45)
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
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let cookies_path = PathBuf::from("cookies.json");
    let username = std::env::var("USERNAME")?;
    let password = std::env::var("PASSWORD")?;
    debug!("username: {username}");
    debug!("password: {password}");
    let mut api = x_api_rs::TwAPI::new(Some(cookies_path.clone()))?;
    if !cookies_path.exists() {
        let result = api.login(&username, &password, "", None).await;
        match result {
            Err(err) => {
                let error = err.downcast_ref::<SuspiciousLoginError>().unwrap();
                println!("Enter your username (eg. @user): ");
                let username = read_string();
                api.login(&username, &password, "".into(), Some(error.1.clone())).await?;
            }
            Ok(_) => {}
        }
        
        api.save_session().unwrap();
    }
    // always call this for extract csrf
    let is_logged_in = api.is_logged_in().await?;
    println!("is logged: {is_logged_in}");
    
    let user_id = api.me_rest_id().await?;
    let res = api.get_following_ids(user_id.to_string(), -1).await?;
    debug!("res is {res:?}");
    let ids = res.entries.iter().map(|v| v.as_i64().unwrap_or_default().to_string()).collect();
    let res = api.users_lookup(ids).await?;
    debug!("res is {res:?}");
    // loop {
    //     let pagination = api.get_friends(user_id, true, Some(cursor.into()))?;
    //     cursor = pagination.cursor.clone();
    //     debug!("Found {:?} following", pagination.entries.len());
    //     if !pagination.has_more {
    //         break;
    //     }
    // }
    Ok(())
    
}

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>,

§

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>,

§

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