12 Commits

Author SHA1 Message Date
VC
8d4d683f55 Merge branch 'wtf' into 'master'
feat: add reqwest multipart feature

See merge request veretcle/tootube!10
2023-10-14 17:30:11 +00:00
VC
2b508e90c1 feat: add reqwest multipart feature 2023-10-14 19:28:49 +02:00
VC
3482456adb Merge branch 'register_function' into 'master'
feat: add register command

See merge request veretcle/tootube!9
2023-10-14 17:22:17 +00:00
VC
568ca00d98 feat: add register command 2023-10-14 19:20:53 +02:00
VC
7fda7069a9 Merge branch 'revert-9ca27a26' into 'master'
Revert "Merge branch 'register_function' into 'master'"

See merge request veretcle/tootube!8
2023-10-14 17:16:47 +00:00
VC
3d9ebcc2a5 Revert "Merge branch 'register_function' into 'master'"
This reverts merge request !7
2023-10-14 17:15:34 +00:00
VC
9ca27a262f Merge branch 'register_function' into 'master'
Register function

See merge request veretcle/tootube!7
2023-10-14 17:14:14 +00:00
VC
d39c78c197 feat: add register command 2023-10-14 19:12:36 +02:00
VC
6867882ca0 doc: add registration documentation + update general documentation 2023-10-14 19:11:34 +02:00
VC
a00753b9eb feat: add multipart reqwest feature 2023-10-14 19:10:21 +02:00
VC
8a9a365a04 chore: update version 2023-10-14 18:17:11 +02:00
VC
4a47eb1d08 chore: cargo update 2023-10-14 18:16:20 +02:00
5 changed files with 92 additions and 3 deletions

26
Cargo.lock generated
View File

@@ -530,6 +530,16 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "miniz_oxide"
version = "0.7.1"
@@ -760,6 +770,7 @@ dependencies = [
"js-sys",
"log",
"mime",
"mime_guess",
"native-tls",
"once_cell",
"percent-encoding",
@@ -1115,6 +1126,15 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed"
[[package]]
name = "unicase"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.13"
@@ -1159,6 +1179,12 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "want"
version = "0.3.1"

View File

@@ -7,7 +7,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "^0.11", features = ["json", "stream"] }
reqwest = { version = "^0.11", features = ["json", "stream", "multipart"] }
tokio = { version = "^1", features = ["full"] }
clap = "^4"
serde = { version = "1.0", features = ["derive"] }

View File

@@ -12,6 +12,7 @@ mod peertube;
use peertube::{get_latest_video, get_max_resolution_dl};
mod youtube;
pub use youtube::register;
use youtube::{create_resumable_upload, now_kiss};
async fn get_dl_video_stream(

View File

@@ -17,8 +17,30 @@ fn main() {
.default_value(DEFAULT_CONFIG_PATH)
.display_order(1),
)
.subcommand(
Command::new("register")
.version(env!("CARGO_PKG_VERSION"))
.about("Command to register to YouTube OAuth2.0")
.arg(
Arg::new("config")
.short('c')
.long("config")
.value_name("CONFIG_FILE")
.help("TOML config file for tootube")
.num_args(1)
.default_value(DEFAULT_CONFIG_PATH)
.display_order(1),
),
)
.get_matches();
if let Some(("register", sub_m)) = matches.subcommand() {
let config = parse_toml(sub_m.get_one::<String>("config").unwrap());
register(&config.youtube)
.unwrap_or_else(|e| panic!("Cannot register to YouTube API: {}", e));
return;
}
let config = parse_toml(matches.get_one::<String>("config").unwrap());
env_logger::init();

View File

@@ -1,9 +1,9 @@
use crate::{config::YoutubeConfig, error::TootubeError, peertube::PeerTubeVideo};
use bytes::Bytes;
use futures_core::stream::Stream;
use reqwest::{Body, Client};
use reqwest::{multipart::Form, Body, Client};
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::{error::Error, io::stdin};
use tokio::sync::OnceCell;
static ACCESS_TOKEN: OnceCell<String> = OnceCell::const_new();
@@ -34,6 +34,11 @@ struct AccessTokenResponse {
access_token: String,
}
#[derive(Deserialize, Debug)]
struct RegistrationAccessTokenResponse {
refresh_token: String,
}
#[derive(Serialize, Debug)]
struct YoutubeUploadParams {
snippet: YoutubeUploadParamsSnippet,
@@ -82,6 +87,41 @@ impl Default for YoutubeUploadParamsStatus {
}
}
/// This function makes the registration process a little bit easier
#[tokio::main]
pub async fn register(config: &YoutubeConfig) -> Result<(), 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!("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:");
let mut input = String::new();
stdin()
.read_line(&mut input)
.expect("Unable to read back the authorization code!");
let form = Form::new()
.text("code", input)
.text("client_id", config.client_id.clone())
.text("client_secret", config.client_secret.clone())
.text("redirect_uri", "urn:ietf:wg:oauth:2.0:oob")
.text("grant_type", "authorization_code");
let client = Client::new();
let res = client
.post("https://accounts.google.com/o/oauth2/token")
.multipart(form)
.send()
.await?;
let refresh_token: RegistrationAccessTokenResponse = res.json().await?;
println!("You can now paste the following line inside the `youtube` section of your tootube.toml file:");
println!("refresh_token=\"{}\"", refresh_token.refresh_token);
Ok(())
}
/// Ensures that Token has been refreshed and that it is unique
async fn refresh_token(config: &YoutubeConfig) -> Result<String, reqwest::Error> {
ACCESS_TOKEN