mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-07-21 13:24:18 +02:00
first real functional version
This commit is contained in:
@@ -1,12 +1,31 @@
|
||||
use crate::config::TwitterConfig;
|
||||
use oauth1_request::Token;
|
||||
use reqwest::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::error::Error;
|
||||
|
||||
/// I don’t know, don’t ask me
|
||||
#[derive(oauth1_request::Request)]
|
||||
struct EmptyRequest {}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct Tweet {}
|
||||
pub struct Tweet {
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct TweetResponse {}
|
||||
|
||||
/// This function returns the OAuth1 Token object from TwitterConfig
|
||||
fn get_token(config: &TwitterConfig) -> Token {
|
||||
oauth1_request::Token::from_parts(
|
||||
config.consumer_key.to_string(),
|
||||
config.consumer_secret.to_string(),
|
||||
config.oauth_token.to_string(),
|
||||
config.oauth_token_secret.to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
/// This function uploads media from Mastodon to Twitter and returns the media id from Twitter
|
||||
#[allow(dead_code)]
|
||||
pub async fn upload_media(_u: &str) -> Result<u64, Box<dyn Error>> {
|
||||
@@ -14,7 +33,31 @@ pub async fn upload_media(_u: &str) -> Result<u64, Box<dyn Error>> {
|
||||
}
|
||||
|
||||
/// This posts Tweets with all the associated medias
|
||||
#[allow(dead_code)]
|
||||
pub async fn post_tweet(_content: &str, _medias: &[u64]) -> Result<u64, Box<dyn Error>> {
|
||||
pub async fn post_tweet(
|
||||
config: &TwitterConfig,
|
||||
content: &str,
|
||||
_medias: &[u64],
|
||||
) -> Result<u64, Box<dyn Error>> {
|
||||
let uri = "https://api.twitter.com/2/tweets";
|
||||
let token = get_token(config);
|
||||
let empty_request = EmptyRequest {}; // Why? Because fuck you, that’s why!
|
||||
let tweet = Tweet {
|
||||
text: content.to_string(),
|
||||
};
|
||||
|
||||
let client = Client::new();
|
||||
|
||||
let res = client
|
||||
.post(uri)
|
||||
.header(
|
||||
"Authorization",
|
||||
oauth1_request::post(uri, &empty_request, &token, oauth1_request::HMAC_SHA1),
|
||||
)
|
||||
.json(&tweet)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
println!("{:?}", res.text().await.unwrap());
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
Reference in New Issue
Block a user