mirror of
https://framagit.org/veretcle/tootube.git
synced 2025-07-20 20:41:17 +02:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
8d4d683f55 | ||
![]() |
2b508e90c1 | ||
![]() |
3482456adb | ||
![]() |
568ca00d98 | ||
![]() |
7fda7069a9 | ||
![]() |
3d9ebcc2a5 | ||
![]() |
9ca27a262f | ||
![]() |
d39c78c197 | ||
![]() |
6867882ca0 | ||
![]() |
a00753b9eb | ||
![]() |
8a9a365a04 | ||
![]() |
4a47eb1d08 |
26
Cargo.lock
generated
26
Cargo.lock
generated
@@ -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"
|
||||
|
@@ -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"] }
|
||||
|
@@ -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(
|
||||
|
22
src/main.rs
22
src/main.rs
@@ -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();
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user