mirror of
https://framagit.org/veretcle/tootube.git
synced 2025-07-20 20:41:17 +02:00
feat: add register command
This commit is contained in:
@@ -12,6 +12,7 @@ mod peertube;
|
|||||||
use peertube::{get_latest_video, get_max_resolution_dl};
|
use peertube::{get_latest_video, get_max_resolution_dl};
|
||||||
|
|
||||||
mod youtube;
|
mod youtube;
|
||||||
|
pub use youtube::register;
|
||||||
use youtube::{create_resumable_upload, now_kiss};
|
use youtube::{create_resumable_upload, now_kiss};
|
||||||
|
|
||||||
async fn get_dl_video_stream(
|
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)
|
.default_value(DEFAULT_CONFIG_PATH)
|
||||||
.display_order(1),
|
.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();
|
.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());
|
let config = parse_toml(matches.get_one::<String>("config").unwrap());
|
||||||
|
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
use crate::{config::YoutubeConfig, error::TootubeError, peertube::PeerTubeVideo};
|
use crate::{config::YoutubeConfig, error::TootubeError, peertube::PeerTubeVideo};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::stream::Stream;
|
use futures_core::stream::Stream;
|
||||||
use reqwest::{Body, Client};
|
use reqwest::{multipart::Form, Body, Client};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::error::Error;
|
use std::{error::Error, io::stdin};
|
||||||
use tokio::sync::OnceCell;
|
use tokio::sync::OnceCell;
|
||||||
|
|
||||||
static ACCESS_TOKEN: OnceCell<String> = OnceCell::const_new();
|
static ACCESS_TOKEN: OnceCell<String> = OnceCell::const_new();
|
||||||
@@ -32,6 +32,7 @@ impl Default for RefreshTokenRequest {
|
|||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct AccessTokenResponse {
|
struct AccessTokenResponse {
|
||||||
access_token: String,
|
access_token: String,
|
||||||
|
refresh_token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
@@ -82,6 +83,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 access_token: AccessTokenResponse = res.json().await?;
|
||||||
|
|
||||||
|
println!("You can now paste the following line inside the `youtube` section of your tootube.toml file:");
|
||||||
|
|
||||||
|
println!("refresh_token=\"{}\"", access_token.refresh_token);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Ensures that Token has been refreshed and that it is unique
|
/// Ensures that Token has been refreshed and that it is unique
|
||||||
async fn refresh_token(config: &YoutubeConfig) -> Result<String, reqwest::Error> {
|
async fn refresh_token(config: &YoutubeConfig) -> Result<String, reqwest::Error> {
|
||||||
ACCESS_TOKEN
|
ACCESS_TOKEN
|
||||||
|
Reference in New Issue
Block a user