mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11: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:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1249,7 +1249,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scootaloo"
|
name = "scootaloo"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"egg-mode",
|
"egg-mode",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "scootaloo"
|
name = "scootaloo"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["VC <veretcle+framagit@mateu.be>"]
|
authors = ["VC <veretcle+framagit@mateu.be>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
71
src/lib.rs
71
src/lib.rs
@@ -1,7 +1,9 @@
|
|||||||
// std
|
// std
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
|
collections::HashMap,
|
||||||
io,
|
io,
|
||||||
|
fmt,
|
||||||
fs::{read_to_string, write},
|
fs::{read_to_string, write},
|
||||||
error::Error,
|
error::Error,
|
||||||
};
|
};
|
||||||
@@ -13,6 +15,7 @@ use serde::Deserialize;
|
|||||||
use egg_mode::{
|
use egg_mode::{
|
||||||
Token,
|
Token,
|
||||||
KeyPair,
|
KeyPair,
|
||||||
|
entities::UrlEntity,
|
||||||
tweet::{
|
tweet::{
|
||||||
Tweet,
|
Tweet,
|
||||||
user_timeline,
|
user_timeline,
|
||||||
@@ -28,8 +31,6 @@ use mammut::status_builder::StatusBuilder;
|
|||||||
/**********
|
/**********
|
||||||
* Generic usage functions
|
* Generic usage functions
|
||||||
***********/
|
***********/
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Those functions are related to the Twitter side of things
|
* 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)
|
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
|
* Config structure
|
||||||
***********/
|
***********/
|
||||||
|
|
||||||
/// General configuration Struct
|
/// General configuration Struct
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
@@ -118,7 +173,6 @@ struct MastodonConfig {
|
|||||||
/*********
|
/*********
|
||||||
* Main functions
|
* Main functions
|
||||||
*********/
|
*********/
|
||||||
|
|
||||||
/// Parses the TOML file into a Config Struct
|
/// Parses the TOML file into a Config Struct
|
||||||
pub fn parse_toml(toml_file: &str) -> Config {
|
pub fn parse_toml(toml_file: &str) -> Config {
|
||||||
let toml_config = read_to_string(toml_file).unwrap_or_else(|e|
|
let toml_config = read_to_string(toml_file).unwrap_or_else(|e|
|
||||||
@@ -187,8 +241,13 @@ pub fn run(config: Config) {
|
|||||||
feed.reverse();
|
feed.reverse();
|
||||||
|
|
||||||
for tweet in &feed {
|
for tweet in &feed {
|
||||||
// build status based upon the original tweet
|
let status = match build_status(tweet) {
|
||||||
let status = StatusBuilder::new(String::from(&tweet.text));
|
Ok(t) => t,
|
||||||
|
Err(e) => {
|
||||||
|
println!("Could not create status from tweet {}: {}", tweet.id ,e);
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
// publish status
|
// publish status
|
||||||
mastodon.new_status(status).unwrap();
|
mastodon.new_status(status).unwrap();
|
||||||
|
Reference in New Issue
Block a user