mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-07-20 12:31:18 +02:00
First functional version
This commit is contained in:
14
src/lib.rs
14
src/lib.rs
@@ -4,7 +4,7 @@ pub use config::{parse_toml, Config};
|
|||||||
mod state;
|
mod state;
|
||||||
pub use state::init_db;
|
pub use state::init_db;
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use state::{read_state, write_state};
|
use state::{read_state, write_state, TweetToToot};
|
||||||
|
|
||||||
mod mastodon;
|
mod mastodon;
|
||||||
use mastodon::get_mastodon_timeline_since;
|
use mastodon::get_mastodon_timeline_since;
|
||||||
@@ -38,9 +38,17 @@ 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(&config.twitter, &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;
|
|
||||||
|
write_state(
|
||||||
|
&conn,
|
||||||
|
TweetToToot {
|
||||||
|
tweet_id,
|
||||||
|
toot_id: toot.id.parse::<u64>().unwrap(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap_or_else(|e| panic!("Cannot store Toot/Tweet ({}/{}): {}", &toot.id, tweet_id, e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -36,7 +36,6 @@ pub fn read_state(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes last treated tweet id and toot id to the db
|
/// Writes last treated tweet id and toot id to the db
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn write_state(conn: &Connection, t: TweetToToot) -> Result<(), Box<dyn Error>> {
|
pub fn write_state(conn: &Connection, t: TweetToToot) -> Result<(), Box<dyn Error>> {
|
||||||
debug!("Write struct {:?}", t);
|
debug!("Write struct {:?}", t);
|
||||||
conn.execute(
|
conn.execute(
|
||||||
|
@@ -14,7 +14,14 @@ pub struct Tweet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct TweetResponse {}
|
pub struct TweetResponse {
|
||||||
|
pub data: TweetResponseData,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct TweetResponseData {
|
||||||
|
pub id: String,
|
||||||
|
}
|
||||||
|
|
||||||
/// This function returns the OAuth1 Token object from TwitterConfig
|
/// This function returns the OAuth1 Token object from TwitterConfig
|
||||||
fn get_token(config: &TwitterConfig) -> Token {
|
fn get_token(config: &TwitterConfig) -> Token {
|
||||||
@@ -39,14 +46,14 @@ pub async fn post_tweet(
|
|||||||
_medias: &[u64],
|
_medias: &[u64],
|
||||||
) -> Result<u64, Box<dyn Error>> {
|
) -> Result<u64, Box<dyn Error>> {
|
||||||
let uri = "https://api.twitter.com/2/tweets";
|
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 empty_request = EmptyRequest {}; // Why? Because fuck you, that’s why!
|
||||||
|
let token = get_token(config);
|
||||||
|
|
||||||
let tweet = Tweet {
|
let tweet = Tweet {
|
||||||
text: content.to_string(),
|
text: content.to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
|
|
||||||
let res = client
|
let res = client
|
||||||
.post(uri)
|
.post(uri)
|
||||||
.header(
|
.header(
|
||||||
@@ -55,9 +62,9 @@ pub async fn post_tweet(
|
|||||||
)
|
)
|
||||||
.json(&tweet)
|
.json(&tweet)
|
||||||
.send()
|
.send()
|
||||||
|
.await?
|
||||||
|
.json::<TweetResponse>()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
println!("{:?}", res.text().await.unwrap());
|
Ok(res.data.id.parse::<u64>().unwrap())
|
||||||
|
|
||||||
Ok(0)
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user