refactor: downloads/uploads every media from a tweet async way

This commit is contained in:
VC
2022-11-08 08:37:17 +01:00
parent 62011b4b81
commit 3b0e7234af

View File

@@ -1,9 +1,15 @@
use crate::{twitter::get_tweet_media, ScootalooError}; use crate::{twitter::get_tweet_media, ScootalooError};
use egg_mode::tweet::Tweet;
use elefren::prelude::*;
use log::{error, warn};
use reqwest::Url;
use std::{borrow::Cow, error::Error}; use std::{borrow::Cow, error::Error};
use egg_mode::tweet::Tweet;
use elefren::prelude::*;
use log::{error, info, warn};
use reqwest::Url;
use tokio::{ use tokio::{
fs::{create_dir_all, remove_file, File}, fs::{create_dir_all, remove_file, File},
io::copy, io::copy,
@@ -35,6 +41,10 @@ pub async fn generate_media_ids(
let mastodon = mastodon.clone(); let mastodon = mastodon.clone();
let task = tokio::task::spawn(async move { let task = tokio::task::spawn(async move {
<<<<<<< HEAD
=======
info!("Start treating {}", media.media_url_https);
>>>>>>> 32f9605 (tamerelol)
// get the tweet embedded media // get the tweet embedded media
let local_tweet_media_path = match get_tweet_media(&media, &cache_path).await { let local_tweet_media_path = match get_tweet_media(&media, &cache_path).await {
Ok(l) => l, Ok(l) => l,
@@ -51,6 +61,7 @@ pub async fn generate_media_ids(
match mastodon.media(Cow::from(local_tweet_media_path.to_owned())) { match mastodon.media(Cow::from(local_tweet_media_path.to_owned())) {
Ok(l) => l, Ok(l) => l,
Err(e) => { Err(e) => {
remove_file(&local_tweet_media_path).await.unwrap();
return Err(ScootalooError::new(&format!( return Err(ScootalooError::new(&format!(
"Attachment {} cannot be uploaded to Mastodon Instance: {}", "Attachment {} cannot be uploaded to Mastodon Instance: {}",
&local_tweet_media_path, e &local_tweet_media_path, e