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

4
Cargo.lock generated
View File

@@ -1855,9 +1855,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "2.5.0" version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]] [[package]]
name = "syn" name = "syn"

View File

@@ -13,7 +13,7 @@ log = "^0.4"
megalodon = "^0.11" megalodon = "^0.11"
oauth1-request = "^0.6" oauth1-request = "^0.6"
regex = "1.10.2" regex = "1.10.2"
reqwest = "^0.11" reqwest = { version = "0.11.22", features = ["json"] }
rusqlite = "^0.27" rusqlite = "^0.27"
serde = { version = "^1.0", features = ["derive"] } serde = { version = "^1.0", features = ["derive"] }
tokio = { version = "^1.33", features = ["rt-multi-thread", "macros"] } tokio = { version = "^1.33", features = ["rt-multi-thread", "macros"] }

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

View File

@@ -15,7 +15,7 @@ pub fn read_state(
conn: &Connection, conn: &Connection,
s: Option<u64>, s: Option<u64>,
) -> Result<Option<TweetToToot>, Box<dyn Error>> { ) -> Result<Option<TweetToToot>, Box<dyn Error>> {
debug!("Reading tweet_id {:?}", s); debug!("Reading toot_id {:?}", s);
let query: String = match s { let query: String = match s {
Some(i) => format!("SELECT * FROM tweet_to_toot WHERE toot_id = {i}"), 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(), 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 /// Initiates the DB from path
pub fn init_db(d: &str) -> Result<(), Box<dyn Error>> { 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)?; let conn = Connection::open(d)?;
conn.execute( conn.execute(

View File

@@ -1,12 +1,31 @@
use crate::config::TwitterConfig;
use oauth1_request::Token;
use reqwest::Client;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::error::Error; use std::error::Error;
/// I dont know, dont ask me
#[derive(oauth1_request::Request)]
struct EmptyRequest {}
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct Tweet {} pub struct Tweet {
pub text: String,
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct TweetResponse {} 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 /// This function uploads media from Mastodon to Twitter and returns the media id from Twitter
#[allow(dead_code)] #[allow(dead_code)]
pub async fn upload_media(_u: &str) -> Result<u64, Box<dyn Error>> { 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 /// This posts Tweets with all the associated medias
#[allow(dead_code)] pub async fn post_tweet(
pub async fn post_tweet(_content: &str, _medias: &[u64]) -> Result<u64, Box<dyn Error>> { 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) Ok(0)
} }