2 Commits

Author SHA1 Message Date
VC
44ec3edfe2 Merge branch 'rust_1_63' into 'master'
feat: adapt to rust 1.63

See merge request veretcle/scootaloo!21
2022-08-17 16:06:39 +00:00
VC
8673dd7866 feat: adapt to rust 1.63 2022-08-17 18:02:12 +02:00
3 changed files with 132 additions and 22 deletions

3
Cargo.lock generated
View File

@@ -2016,7 +2016,7 @@ dependencies = [
[[package]]
name = "scootaloo"
version = "0.6.0"
version = "0.6.1"
dependencies = [
"chrono",
"clap",
@@ -2025,6 +2025,7 @@ dependencies = [
"futures 0.3.14",
"html-escape",
"log",
"mime",
"reqwest 0.11.3",
"rusqlite",
"serde",

View File

@@ -1,6 +1,6 @@
[package]
name = "scootaloo"
version = "0.6.0"
version = "0.6.1"
authors = ["VC <veretcle+framagit@mateu.be>"]
edition = "2021"
@@ -20,6 +20,7 @@ html-escape = "^0.2"
reqwest = "^0.11"
log = "^0.4"
simple_logger = "^2.1"
mime = "^0.3"
[profile.release]
strip = true

View File

@@ -50,29 +50,137 @@ pub async fn get_user_timeline(
/// Retrieves a single media from a tweet and store it in a temporary file
pub async fn get_tweet_media(m: &MediaEntity, t: &str) -> Result<String, Box<dyn Error>> {
match m.media_type {
MediaType::Photo => {
return cache_media(&m.media_url_https, t).await;
}
MediaType::Photo => cache_media(&m.media_url_https, t).await,
_ => match &m.video_info {
Some(v) => {
for variant in &v.variants {
if variant.content_type == "video/mp4" {
return cache_media(&variant.url, t).await;
}
}
return Err(ScootalooError::new(&format!(
Some(v) => match &v.variants.iter().find(|&x| x.content_type == "video/mp4") {
Some(u) => cache_media(&u.url, t).await,
None => Err(ScootalooError::new(&format!(
"Media Type for {} is video but no mp4 file URL is available",
&m.url
))
.into());
}
None => {
return Err(ScootalooError::new(&format!(
"Media Type for {} is video but does not contain any video_info",
&m.url
))
.into());
}
.into()),
},
None => Err(ScootalooError::new(&format!(
"Media Type for {} is video but does not contain any video_info",
&m.url
))
.into()),
},
};
}
}
#[cfg(test)]
mod tests {
use super::*;
use egg_mode::entities::{
MediaSize, MediaSizes,
MediaType::{Gif, Photo},
ResizeMode::Crop,
ResizeMode::Fit,
VideoInfo, VideoVariant,
};
use std::fs::remove_dir_all;
const TMP_DIR: &'static str = "/tmp/scootaloo_get_tweet_media_test";
#[tokio::test]
async fn test_get_tweet_media() {
let m_photo = MediaEntity {
display_url: "pic.twitter.com/sHrwmP69Yv".to_string(),
expanded_url: "https://twitter.com/NintendojoFR/status/1555473821121056771/photo/1"
.to_string(),
id: 1555473771280080896,
range: (91, 114),
media_url: "http://pbs.twimg.com/media/FZYnJ1qWIAAReHt.jpg".to_string(),
media_url_https: "https://pbs.twimg.com/media/FZYnJ1qWIAAReHt.jpg"
.to_string(),
sizes: MediaSizes {
thumb: MediaSize {
w: 150,
h: 150,
resize: Crop
},
small: MediaSize {
w: 680,
h: 510,
resize: Fit
},
medium: MediaSize {
w: 1200,
h: 900,
resize: Fit
},
large: MediaSize {
w: 1280,
h: 960,
resize: Fit
}
},
source_status_id: None,
media_type: Photo,
url: "https://t.co/sHrwmP69Yv".to_string(),
video_info: None,
ext_alt_text: Some("Le menu «\u{a0}Classes » du jeu vidéo Xenoblade Chronicles 3 (Switch). Laffinité du personnage pour la classe est notée par quatre lettres : C, A, C, A (caca)."
.to_string())
};
let m_video = MediaEntity {
display_url: "pic.twitter.com/xDln0RrkjU".to_string(),
expanded_url: "https://twitter.com/NintendojoFR/status/1551822196833673218/photo/1"
.to_string(),
id: 1551822189711790081,
range: (275, 298),
media_url: "http://pbs.twimg.com/tweet_video_thumb/FYkuD0RXEAE-iDx.jpg".to_string(),
media_url_https: "https://pbs.twimg.com/tweet_video_thumb/FYkuD0RXEAE-iDx.jpg"
.to_string(),
sizes: MediaSizes {
thumb: MediaSize {
w: 150,
h: 150,
resize: Crop,
},
small: MediaSize {
w: 320,
h: 240,
resize: Fit,
},
medium: MediaSize {
w: 320,
h: 240,
resize: Fit,
},
large: MediaSize {
w: 320,
h: 240,
resize: Fit,
},
},
source_status_id: None,
media_type: Gif,
url: "https://t.co/xDln0RrkjU".to_string(),
video_info: Some(VideoInfo {
aspect_ratio: (4, 3),
duration_millis: None,
variants: vec![VideoVariant {
bitrate: Some(0),
content_type: "video/mp4".parse::<mime::Mime>().unwrap(),
url: "https://video.twimg.com/tweet_video/FYkuD0RXEAE-iDx.mp4".to_string(),
}],
}),
ext_alt_text: Some("Scared Nintendo GIF".to_string()),
};
let tweet_media_photo = get_tweet_media(&m_photo, TMP_DIR).await.unwrap();
let tweet_media_video = get_tweet_media(&m_video, TMP_DIR).await.unwrap();
assert_eq!(
tweet_media_photo,
format!("{}/FZYnJ1qWIAAReHt.jpg", TMP_DIR)
);
assert_eq!(
tweet_media_video,
format!("{}/FYkuD0RXEAE-iDx.mp4", TMP_DIR)
);
remove_dir_all(TMP_DIR).unwrap();
}
}