From 8bdfdae1aa99213f00f0445447423a9592db783f Mon Sep 17 00:00:00 2001 From: VC Date: Tue, 3 Oct 2023 15:46:07 +0200 Subject: [PATCH 1/9] feat: reverse publishAt sort --- src/peertube.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/peertube.rs b/src/peertube.rs index 78c94a0..ca945f4 100644 --- a/src/peertube.rs +++ b/src/peertube.rs @@ -37,7 +37,7 @@ pub struct PeerTubeVideoStreamingPlaylistsFilesResolution { /// This gets the last video uploaded to the PeerTube server pub fn get_latest_video(u: &str) -> Result> { - let body = reqwest::blocking::get(format!("{}/api/v1/videos?count=1&sort=publishedAt", u))? + let body = reqwest::blocking::get(format!("{}/api/v1/videos?count=1&sort=-publishedAt", u))? .json::()?; let vid = get_video_detail(u, &body.data[0].uuid)?; From 1ed86085bc982f19c8295af08eff5e2d16294e67 Mon Sep 17 00:00:00 2001 From: VC Date: Tue, 3 Oct 2023 15:49:57 +0200 Subject: [PATCH 2/9] feat: add license --- LICENSE | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5a8e332 --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. + From 8664b169f02e35b3af70001b59626b32d4d08bf5 Mon Sep 17 00:00:00 2001 From: VC Date: Tue, 3 Oct 2023 16:09:47 +0200 Subject: [PATCH 3/9] fix: get the whole upload URL instead of just the ID --- src/lib.rs | 4 ++-- src/youtube.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 100bcfe..fd7b5fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,10 +64,10 @@ pub fn run(config: Config) { let local_path = dl_video(&dl_url) .unwrap_or_else(|e| panic!("Cannot download video at URL {}: {}", dl_url, e)); - let resumable_upload_id = create_resumable_upload(&config.youtube, &latest_vid) + let resumable_upload_url = create_resumable_upload(&config.youtube, &latest_vid) .unwrap_or_else(|e| panic!("Cannot retrieve the upload’s resumable id: {e}")); - upload_video(&local_path, &resumable_upload_id, &config.youtube) + upload_video(&local_path, &resumable_upload_url, &config.youtube) .unwrap_or_else(|e| panic!("Cannot resume upload!: {e}")); remove_file(&local_path).unwrap_or_else(|e| panic!("Cannot delete file {}: {}", local_path, e)); diff --git a/src/youtube.rs b/src/youtube.rs index 2f7f177..90f23ea 100644 --- a/src/youtube.rs +++ b/src/youtube.rs @@ -136,7 +136,7 @@ pub fn create_resumable_upload( if res.status().is_success() { Ok(res .headers() - .get("x-guploader-uploadid") + .get("location") .ok_or("Cannot find suitable header")? .to_str()? .to_string()) @@ -147,14 +147,14 @@ pub fn create_resumable_upload( pub fn upload_video( f_path: &str, - r_id: &str, + r_url: &str, config: &YoutubeConfig, ) -> Result<(), Box> { let access_token = refresh_token(config)?; let client = reqwest::blocking::Client::new(); - let res = client.put(format!("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet%2Cstatus&upload_id={}", r_id)) + let res = client.put(r_url) .header("Authorization", format!("Bearer {}", access_token)) .body(f_path.to_string()) .send()?; From 301e6d1e320579b7d057b5ba301e4dbcc9dad96c Mon Sep 17 00:00:00 2001 From: VC Date: Tue, 3 Oct 2023 16:16:34 +0200 Subject: [PATCH 4/9] style: make fmt happy --- src/youtube.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/youtube.rs b/src/youtube.rs index 90f23ea..543c433 100644 --- a/src/youtube.rs +++ b/src/youtube.rs @@ -154,10 +154,11 @@ pub fn upload_video( let client = reqwest::blocking::Client::new(); - let res = client.put(r_url) - .header("Authorization", format!("Bearer {}", access_token)) - .body(f_path.to_string()) - .send()?; + let res = client + .put(r_url) + .header("Authorization", format!("Bearer {}", access_token)) + .body(f_path.to_string()) + .send()?; if res.status().is_success() { Ok(()) From 1b4ef5e9201fd699e02da1c70d40fd3e7fa684e3 Mon Sep 17 00:00:00 2001 From: VC Date: Wed, 4 Oct 2023 10:11:59 +0200 Subject: [PATCH 5/9] fix: post instead of put --- src/youtube.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/youtube.rs b/src/youtube.rs index 543c433..2903e3a 100644 --- a/src/youtube.rs +++ b/src/youtube.rs @@ -155,7 +155,7 @@ pub fn upload_video( let client = reqwest::blocking::Client::new(); let res = client - .put(r_url) + .post(r_url) .header("Authorization", format!("Bearer {}", access_token)) .body(f_path.to_string()) .send()?; From fdee8d7fe917136ee4e4030d6c76839d6319490a Mon Sep 17 00:00:00 2001 From: VC Date: Wed, 4 Oct 2023 10:27:34 +0200 Subject: [PATCH 6/9] test: to be dropped --- src/peertube.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/peertube.rs b/src/peertube.rs index ca945f4..78c94a0 100644 --- a/src/peertube.rs +++ b/src/peertube.rs @@ -37,7 +37,7 @@ pub struct PeerTubeVideoStreamingPlaylistsFilesResolution { /// This gets the last video uploaded to the PeerTube server pub fn get_latest_video(u: &str) -> Result> { - let body = reqwest::blocking::get(format!("{}/api/v1/videos?count=1&sort=-publishedAt", u))? + let body = reqwest::blocking::get(format!("{}/api/v1/videos?count=1&sort=publishedAt", u))? .json::()?; let vid = get_video_detail(u, &body.data[0].uuid)?; From 06e5c5a5ba6158bbe1e35cc7ae80b4075d1ccde1 Mon Sep 17 00:00:00 2001 From: VC Date: Wed, 4 Oct 2023 10:28:04 +0200 Subject: [PATCH 7/9] =?UTF-8?q?fix:=E2=80=AFsend=20file,=20not=20just=20na?= =?UTF-8?q?me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/youtube.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/youtube.rs b/src/youtube.rs index 2903e3a..77f2987 100644 --- a/src/youtube.rs +++ b/src/youtube.rs @@ -1,6 +1,6 @@ use crate::{config::YoutubeConfig, error::TootubeError, peertube::PeerTubeVideo}; use serde::{Deserialize, Serialize}; -use std::{error::Error, sync::Mutex}; +use std::{error::Error, fs::File, sync::Mutex}; static ACCESS_TOKEN: Mutex = Mutex::new(String::new()); @@ -150,14 +150,18 @@ pub fn upload_video( r_url: &str, config: &YoutubeConfig, ) -> Result<(), Box> { + // Get access token let access_token = refresh_token(config)?; + // Create client let client = reqwest::blocking::Client::new(); + let file = File::open(f_path)?; + let res = client - .post(r_url) + .put(r_url) .header("Authorization", format!("Bearer {}", access_token)) - .body(f_path.to_string()) + .body(file) .send()?; if res.status().is_success() { From 328ab8ba7b441ab8977e7db30ebd2f871c430f28 Mon Sep 17 00:00:00 2001 From: VC Date: Wed, 4 Oct 2023 13:54:04 +0200 Subject: [PATCH 8/9] debug functions --- src/youtube.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/youtube.rs b/src/youtube.rs index 77f2987..781a96b 100644 --- a/src/youtube.rs +++ b/src/youtube.rs @@ -165,6 +165,10 @@ pub fn upload_video( .send()?; if res.status().is_success() { + /* debug */ + println!("{:?}", res.status()); + println!("{:?}", res.text()?); + /* debug */ Ok(()) } else { Err(TootubeError::new(&format!("Cannot upload video: {:?}", res.text())).into()) From 54c926fd1ed4f06d6640f6f214e5be1dbf486d3f Mon Sep 17 00:00:00 2001 From: VC Date: Wed, 4 Oct 2023 13:59:38 +0200 Subject: [PATCH 9/9] feat: add log --- Cargo.lock | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 2 ++ src/youtube.rs | 4 --- 4 files changed, 92 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e6e436..6a6cf83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.1" @@ -189,6 +198,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + [[package]] name = "errno" version = "0.3.3" @@ -365,6 +387,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.27" @@ -428,6 +456,17 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6" +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys", +] + [[package]] name = "itoa" version = "1.0.9" @@ -637,6 +676,35 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "regex" +version = "1.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" + [[package]] name = "reqwest" version = "0.11.20" @@ -833,6 +901,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "termcolor" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" +dependencies = [ + "winapi-util", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -902,6 +979,8 @@ name = "tootube" version = "0.1.0" dependencies = [ "clap", + "env_logger", + "log", "reqwest", "serde", "toml", @@ -1090,6 +1169,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 852204f..b4cf450 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ reqwest = { version = "^0.11", features = ["blocking", "json"] } clap = "^4" serde = { version = "1.0", features = ["derive"] } toml = "^0.5" +log = "^0.4" +env_logger = "^0.10" [profile.release] strip = true diff --git a/src/main.rs b/src/main.rs index fb8fe58..3f3b5cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,5 +21,7 @@ fn main() { let config = parse_toml(matches.get_one::("config").unwrap()); + env_logger::init(); + run(config); } diff --git a/src/youtube.rs b/src/youtube.rs index 781a96b..77f2987 100644 --- a/src/youtube.rs +++ b/src/youtube.rs @@ -165,10 +165,6 @@ pub fn upload_video( .send()?; if res.status().is_success() { - /* debug */ - println!("{:?}", res.status()); - println!("{:?}", res.text()?); - /* debug */ Ok(()) } else { Err(TootubeError::new(&format!("Cannot upload video: {:?}", res.text())).into())