Merge branch '14-do-not-notify-subscribers-for-shorts' into 'master'

Resolve "Do not notify subscribers for Shorts"

Closes #14

See merge request veretcle/tootube!29
This commit is contained in:
VC
2024-09-16 08:40:12 +00:00
8 changed files with 81 additions and 15 deletions

10
Cargo.lock generated
View File

@@ -150,9 +150,9 @@ checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.1.18" version = "1.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476" checksum = "2d74707dde2ba56f86ae90effb3b43ddd369504387e718014de010cec7959800"
dependencies = [ dependencies = [
"shlex", "shlex",
] ]
@@ -677,9 +677,9 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.19.0" version = "1.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" checksum = "33ea5043e58958ee56f3e15a90aee535795cd7dfd319846288d93c5b57d85cbe"
[[package]] [[package]]
name = "openssl" name = "openssl"
@@ -1198,7 +1198,7 @@ dependencies = [
[[package]] [[package]]
name = "tootube" name = "tootube"
version = "0.9.1" version = "0.10.0"
dependencies = [ dependencies = [
"async-stream", "async-stream",
"clap", "clap",

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "tootube" name = "tootube"
authors = ["VC <veretcle+framagit@mateu.be>"] authors = ["VC <veretcle+framagit@mateu.be>"]
version = "0.9.1" version = "0.10.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -58,6 +58,11 @@ client_secret="<YOUR CLIENT_SECRET>"
refresh_token="<YOUR CLIENT TOKEN>" refresh_token="<YOUR CLIENT TOKEN>"
[youtube] [youtube]
notify_subscribers_on_shorts=false # will you notify subscribers for shorts?
# optional
# allows you to notify subscribers when transferring shorts, defaults to false
[youtube.oauth2]
refresh_token="" # leave empty for now refresh_token="" # leave empty for now
client_id="<YOUR CLIENT_ID>" client_id="<YOUR CLIENT_ID>"
client_secret="<YOUR CLIENT_SECRET>" client_secret="<YOUR CLIENT_SECRET>"

View File

@@ -40,6 +40,13 @@ pub struct PeertubeConfigOauth2 {
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct YoutubeConfig { pub struct YoutubeConfig {
#[serde(default)]
pub notify_subscribers_on_shorts: bool,
pub oauth2: YoutubeConfigOauth2,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct YoutubeConfigOauth2 {
pub refresh_token: String, pub refresh_token: String,
pub client_id: String, pub client_id: String,
pub client_secret: String, pub client_secret: String,
@@ -79,3 +86,33 @@ impl Config {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use std::fs::{remove_file, write};
#[test]
fn test_minimal_conf() {
let file_name = "/tmp/test_minimal_conf.toml";
let data = r#"
[peertube]
base_url = 'https://p.nintendojo.fr'
[youtube.oauth2]
refresh_token = 'rt_N/A'
client_id = 'ci_N/A'
client_secret = 'cs_N/A'
"#;
write(file_name, data).unwrap();
let config = Config::new(file_name);
assert_eq!(config.youtube.notify_subscribers_on_shorts, false);
assert_eq!(config.tootube.progress_chars, "#>-");
assert_eq!(config.youtube.oauth2.refresh_token, "rt_N/A");
assert_eq!(config.youtube.oauth2.client_id, "ci_N/A");
assert_eq!(config.youtube.oauth2.client_secret, "cs_N/A");
remove_file(file_name).unwrap();
}
}

View File

@@ -66,15 +66,24 @@ pub async fn run(config: &Config, pl: Vec<String>, pt_video_id: Option<&str>) ->
debug!("PT download URL: {}", &dl_url); debug!("PT download URL: {}", &dl_url);
let youtube = YouTube::new( let youtube = YouTube::new(
&config.youtube.client_id, &config.youtube.oauth2.client_id,
&config.youtube.client_secret, &config.youtube.oauth2.client_secret,
&config.youtube.refresh_token, &config.youtube.oauth2.refresh_token,
) )
.await .await
.unwrap_or_else(|e| panic!("Cannot instantiate YouTube struct: {}", e)); .unwrap_or_else(|e| panic!("Cannot instantiate YouTube struct: {}", e));
// Do not notify when notify_subscribers_on_shorts = False and latest_id is a short
debug!(
"Will user get notified? {}",
!(!config.youtube.notify_subscribers_on_shorts & latest_vid.is_short())
);
let resumable_upload_url = youtube let resumable_upload_url = youtube
.create_resumable_upload(&latest_vid) .create_resumable_upload(
&latest_vid,
!(!config.youtube.notify_subscribers_on_shorts & latest_vid.is_short()),
)
.await .await
.unwrap_or_else(|e| panic!("Cannot retrieve the uploads resumable id: {e}")); .unwrap_or_else(|e| panic!("Cannot retrieve the uploads resumable id: {e}"));
debug!("YT upload URL: {}", &resumable_upload_url); debug!("YT upload URL: {}", &resumable_upload_url);

View File

@@ -69,10 +69,10 @@ fn main() {
let mut config = Config::new(sub_m.get_one::<String>("config").unwrap()); let mut config = Config::new(sub_m.get_one::<String>("config").unwrap());
if sub_m.get_flag("youtube") { if sub_m.get_flag("youtube") {
let yt_refresh_token = register_youtube(&config.youtube) let yt_refresh_token = register_youtube(&config.youtube.oauth2)
.unwrap_or_else(|e| panic!("Cannot register to YouTube API: {}", e)); .unwrap_or_else(|e| panic!("Cannot register to YouTube API: {}", e));
config.youtube.refresh_token = yt_refresh_token; config.youtube.oauth2.refresh_token = yt_refresh_token;
} }
if sub_m.get_flag("peertube") { if sub_m.get_flag("peertube") {

View File

@@ -19,6 +19,9 @@ pub struct PeerTubeVideo {
pub name: String, pub name: String,
pub uuid: String, pub uuid: String,
pub description: String, pub description: String,
pub duration: u64,
#[serde(rename = "aspectRatio")]
pub aspect_ratio: Option<f32>,
#[serde(rename = "previewPath")] #[serde(rename = "previewPath")]
pub preview_path: String, pub preview_path: String,
#[serde(rename = "streamingPlaylists")] #[serde(rename = "streamingPlaylists")]
@@ -27,6 +30,15 @@ pub struct PeerTubeVideo {
pub channel: PeerTubeVideoChannel, pub channel: PeerTubeVideoChannel,
} }
impl PeerTubeVideo {
pub fn is_short(&self) -> bool {
if self.duration < 60 && self.aspect_ratio.is_some_and(|x| x == 0.5625) {
return true;
}
false
}
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct PeerTubeVideoChannel { pub struct PeerTubeVideoChannel {
pub id: u8, pub id: u8,

View File

@@ -1,5 +1,5 @@
use crate::{ use crate::{
config::{TootubeConfig, YoutubeConfig}, config::{TootubeConfig, YoutubeConfigOauth2},
error::TootubeError, error::TootubeError,
peertube::PeerTubeVideo, peertube::PeerTubeVideo,
}; };
@@ -162,7 +162,7 @@ struct YoutubePlaylistListResponseItemSnippet {
/// This function makes the registration process a little bit easier /// This function makes the registration process a little bit easier
/// It returns the expected refresh_token so that it can be written back to the file /// It returns the expected refresh_token so that it can be written back to the file
#[tokio::main] #[tokio::main]
pub async fn register(config: &YoutubeConfig) -> Result<String, Box<dyn Error>> { pub async fn register(config: &YoutubeConfigOauth2) -> Result<String, Box<dyn Error>> {
println!("Click on the link below to authorize {} to upload to YouTube and deal with your playlists:", env!("CARGO_PKG_NAME")); println!("Click on the link below to authorize {} to upload to YouTube and deal with your playlists:", env!("CARGO_PKG_NAME"));
println!("https://accounts.google.com/o/oauth2/v2/auth?client_id={}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/youtube%20https://www.googleapis.com/auth/youtube.upload&response_type=code", config.client_id); println!("https://accounts.google.com/o/oauth2/v2/auth?client_id={}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/youtube%20https://www.googleapis.com/auth/youtube.upload&response_type=code", config.client_id);
println!("Paste the returned authorization code:"); println!("Paste the returned authorization code:");
@@ -340,6 +340,7 @@ impl YouTube {
pub async fn create_resumable_upload( pub async fn create_resumable_upload(
&self, &self,
vid: &PeerTubeVideo, vid: &PeerTubeVideo,
notify: bool,
) -> Result<String, Box<dyn Error>> { ) -> Result<String, Box<dyn Error>> {
if vid.name.chars().count() > 100 { if vid.name.chars().count() > 100 {
warn!( warn!(
@@ -365,7 +366,9 @@ impl YouTube {
}; };
debug!("YT upload params: {:?}", &upload_params); debug!("YT upload params: {:?}", &upload_params);
let res = self.client.post("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet%2Cstatus") let notify_subscriber = notify.then_some(()).map_or("False", |_| "True");
let res = self.client.post(format!("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet%2Cstatus&notifySubscribers={}", notify_subscriber))
.json(&upload_params) .json(&upload_params)
.send().await?; .send().await?;