mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-21 09:31:19 +02:00
Dereferences Twitter short url (t.co) to the real URLs + add Error handling (not used at the moment)
This commit is contained in:
71
src/lib.rs
71
src/lib.rs
@@ -1,7 +1,9 @@
|
||||
// std
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
io,
|
||||
fmt,
|
||||
fs::{read_to_string, write},
|
||||
error::Error,
|
||||
};
|
||||
@@ -13,6 +15,7 @@ use serde::Deserialize;
|
||||
use egg_mode::{
|
||||
Token,
|
||||
KeyPair,
|
||||
entities::UrlEntity,
|
||||
tweet::{
|
||||
Tweet,
|
||||
user_timeline,
|
||||
@@ -28,8 +31,6 @@ use mammut::status_builder::StatusBuilder;
|
||||
/**********
|
||||
* Generic usage functions
|
||||
***********/
|
||||
|
||||
|
||||
/*
|
||||
* Those functions are related to the Twitter side of things
|
||||
*/
|
||||
@@ -85,10 +86,64 @@ fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon {
|
||||
|
||||
Mastodon::from_data(data)
|
||||
}
|
||||
|
||||
/// build toot from tweet
|
||||
fn build_status(tweet: &Tweet) -> Result<StatusBuilder, Box<dyn Error>> {
|
||||
let mut toot = String::from(&tweet.text);
|
||||
|
||||
let decoded_urls = decode_urls(&tweet.entities.urls);
|
||||
|
||||
for decoded_url in decoded_urls {
|
||||
toot = toot.replace(&decoded_url.0, &decoded_url.1);
|
||||
}
|
||||
|
||||
Ok(StatusBuilder::new(toot))
|
||||
}
|
||||
|
||||
fn decode_urls(urls: &Vec<UrlEntity>) -> HashMap<String, String> {
|
||||
let mut decoded_urls = HashMap::new();
|
||||
|
||||
for url in urls {
|
||||
if url.expanded_url.is_some() {
|
||||
// unwrap is safe here as we just verified that there is something inside expanded_url
|
||||
decoded_urls.insert(String::from(&url.url), String::from(url.expanded_url.as_deref().unwrap()));
|
||||
}
|
||||
}
|
||||
|
||||
decoded_urls
|
||||
}
|
||||
|
||||
/**********
|
||||
* local error handler
|
||||
**********/
|
||||
#[derive(Debug)]
|
||||
struct ScootalooError {
|
||||
details: String,
|
||||
}
|
||||
|
||||
impl ScootalooError {
|
||||
fn new(msg: &str) -> ScootalooError {
|
||||
ScootalooError {
|
||||
details: String::from(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ScootalooError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.details)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ScootalooError {
|
||||
fn description(&self) -> &str {
|
||||
&self.details
|
||||
}
|
||||
}
|
||||
|
||||
/**********
|
||||
* Config structure
|
||||
***********/
|
||||
|
||||
/// General configuration Struct
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
@@ -118,7 +173,6 @@ struct MastodonConfig {
|
||||
/*********
|
||||
* Main functions
|
||||
*********/
|
||||
|
||||
/// Parses the TOML file into a Config Struct
|
||||
pub fn parse_toml(toml_file: &str) -> Config {
|
||||
let toml_config = read_to_string(toml_file).unwrap_or_else(|e|
|
||||
@@ -187,8 +241,13 @@ pub fn run(config: Config) {
|
||||
feed.reverse();
|
||||
|
||||
for tweet in &feed {
|
||||
// build status based upon the original tweet
|
||||
let status = StatusBuilder::new(String::from(&tweet.text));
|
||||
let status = match build_status(tweet) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
println!("Could not create status from tweet {}: {}", tweet.id ,e);
|
||||
continue;
|
||||
},
|
||||
};
|
||||
|
||||
// publish status
|
||||
mastodon.new_status(status).unwrap();
|
||||
|
Reference in New Issue
Block a user