first real functional version

This commit is contained in:
VC
2023-11-08 15:41:27 +01:00
parent bf70072376
commit b3ba8637f8
5 changed files with 55 additions and 9 deletions

View File

@@ -38,7 +38,7 @@ pub async fn run(config: &Config) {
};
// if we wanted to cut toot in half, now would be the right time to do so
// treating medias (nothing for now)
let _tweet_id = post_tweet(&tweet_content, &[])
let _tweet_id = post_tweet(&config.twitter, &tweet_content, &[])
.await
.unwrap_or_else(|e| panic!("Cannot Tweet {}: {}", toot.id, e));
return;

View File

@@ -15,7 +15,7 @@ pub fn read_state(
conn: &Connection,
s: Option<u64>,
) -> Result<Option<TweetToToot>, Box<dyn Error>> {
debug!("Reading tweet_id {:?}", s);
debug!("Reading toot_id {:?}", s);
let query: String = match s {
Some(i) => format!("SELECT * FROM tweet_to_toot WHERE toot_id = {i}"),
None => "SELECT * FROM tweet_to_toot ORDER BY toot_id DESC LIMIT 1".to_string(),
@@ -49,7 +49,10 @@ pub fn write_state(conn: &Connection, t: TweetToToot) -> Result<(), Box<dyn Erro
/// Initiates the DB from path
pub fn init_db(d: &str) -> Result<(), Box<dyn Error>> {
debug!("Initializing DB for Scootaloo");
debug!(
"{}",
format!("Initializing DB for {}", env!("CARGO_PKG_NAME"))
);
let conn = Connection::open(d)?;
conn.execute(

View File

@@ -1,12 +1,31 @@
use crate::config::TwitterConfig;
use oauth1_request::Token;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::error::Error;
/// I dont know, dont 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, thats 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)
}