mirror of
https://framagit.org/veretcle/tootube.git
synced 2025-07-20 20:41:17 +02:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d70fbdac98 | ||
![]() |
982dc8b954 | ||
![]() |
bf622d6989 | ||
![]() |
a687f008df | ||
![]() |
e2ab93c04e | ||
![]() |
9e1c27be29 | ||
![]() |
ab9ea1a8ee | ||
![]() |
66f288c069 |
657
Cargo.lock
generated
657
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,12 +1,13 @@
|
||||
[package]
|
||||
name = "tootube"
|
||||
authors = ["VC <veretcle+framagit@mateu.be>"]
|
||||
version = "0.5.2"
|
||||
version = "0.6.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rpassword = "^7.3"
|
||||
reqwest = { version = "^0.11", features = ["json", "stream", "multipart"] }
|
||||
tokio = { version = "^1", features = ["full"] }
|
||||
clap = "^4"
|
||||
|
24
README.md
24
README.md
@@ -13,6 +13,7 @@ So consider this a work in progress that will slowly get better with time.
|
||||
# What does it do exactly?
|
||||
|
||||
* it retrieves the latest PeerTube video download URL from an instance
|
||||
* if you register the app into your PeerTube instance (optional), you can retrieve the latest video source instead of the highest quality encoded video
|
||||
* it creates a resumable upload into the target YouTube account
|
||||
* it downloads/uploads the latest PeerTube video to YouTube without using a cache (stream-to-stream)
|
||||
|
||||
@@ -46,6 +47,15 @@ Create your `tootube.toml` config file:
|
||||
```toml
|
||||
[peertube]
|
||||
base_url="https://p.nintendojo.fr"
|
||||
# optional
|
||||
# allows you to delete the original video source file once it’s uploaded to YouTube
|
||||
delete_video_source_after_transfer=true # this option is only available if you have Administrator privileges
|
||||
# optional
|
||||
# everything below is given by the register command with --peertube option
|
||||
[peertube.oauth2]
|
||||
client_id="<YOUR CLIENT_ID>"
|
||||
client_secret="<YOUR CLIENT_SECRET"
|
||||
refresh_token="/var/lib/tootube/refresh_token" # refresh_token are single use only in PeerTube so we need to store it in a separate file
|
||||
|
||||
[youtube]
|
||||
refresh_token="" # leave empty for now
|
||||
@@ -56,7 +66,19 @@ client_secret="<YOUR CLIENT_SECRET>"
|
||||
Then run:
|
||||
|
||||
```bash
|
||||
tootube register --config <PATH TO YOUR TOOTUBE.TOML FILE>
|
||||
tootube register --youtube --config <PATH TO YOUR TOOTUBE.TOML FILE>
|
||||
```
|
||||
|
||||
You’ll be then prompted with all the necessary information to register `tootube`. You’ll end with a `refresh_token` that you can now paste inside your tootube configuration.
|
||||
|
||||
If you wish to register `tootube` on PeerTube, you can do so by using:
|
||||
|
||||
```bash
|
||||
tootube register --peertube --config <PATH TO YOUR TOOTUBE.TOML FILE>
|
||||
```
|
||||
|
||||
It will require your username/password (beware that 2FA is not supported for this feature as of now) and generate a first `refresh_token` that you will put inside the aformentioned file:
|
||||
|
||||
```bash
|
||||
echo -n '<REFRESH_TOKEN>' > /var/lib/tootube/refresh_token
|
||||
```
|
||||
|
@@ -6,11 +6,33 @@ use std::fs::read_to_string;
|
||||
pub struct Config {
|
||||
pub peertube: PeertubeConfig,
|
||||
pub youtube: YoutubeConfig,
|
||||
#[serde(default)]
|
||||
pub tootube: TootubeConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PeertubeConfig {
|
||||
pub base_url: String,
|
||||
#[serde(default)]
|
||||
pub delete_video_source_after_transfer: bool,
|
||||
pub oauth2: Option<PeertubeConfigOauth2>,
|
||||
}
|
||||
|
||||
impl Default for PeertubeConfig {
|
||||
fn default() -> Self {
|
||||
PeertubeConfig {
|
||||
base_url: "".to_string(),
|
||||
delete_video_source_after_transfer: false,
|
||||
oauth2: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct PeertubeConfigOauth2 {
|
||||
pub client_id: String,
|
||||
pub client_secret: String,
|
||||
pub refresh_token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -20,6 +42,21 @@ pub struct YoutubeConfig {
|
||||
pub client_secret: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct TootubeConfig {
|
||||
pub progress_bar: String,
|
||||
pub progress_chars: String,
|
||||
}
|
||||
|
||||
impl Default for TootubeConfig {
|
||||
fn default() -> Self {
|
||||
TootubeConfig {
|
||||
progress_bar: "{msg}\n{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})".to_string(),
|
||||
progress_chars: "#>-".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the TOML file into a Config struct
|
||||
pub fn parse_toml(toml_file: &str) -> Config {
|
||||
let toml_config =
|
||||
|
39
src/lib.rs
39
src/lib.rs
@@ -7,10 +7,11 @@ pub use config::parse_toml;
|
||||
use config::Config;
|
||||
|
||||
mod peertube;
|
||||
use peertube::get_latest_video;
|
||||
pub use peertube::register as register_peertube;
|
||||
use peertube::{delete_original_video_source, get_latest_video, get_original_video_source};
|
||||
|
||||
mod youtube;
|
||||
pub use youtube::register;
|
||||
pub use youtube::register as register_youtube;
|
||||
use youtube::{add_video_to_playlists, create_resumable_upload, now_kiss};
|
||||
|
||||
#[tokio::main]
|
||||
@@ -22,13 +23,29 @@ pub async fn run(config: Config, pl: Vec<String>) {
|
||||
panic!("Cannot retrieve the latest video, something must have gone terribly wrong: {e}")
|
||||
});
|
||||
|
||||
let dl_url = latest_vid.streaming_playlists.as_ref().unwrap()[0]
|
||||
// We have a refresh_token, try to use it
|
||||
let source_url = match &config.peertube.oauth2 {
|
||||
Some(_) => get_original_video_source(&latest_vid.uuid, &config.peertube)
|
||||
.await
|
||||
.ok(),
|
||||
None => None,
|
||||
};
|
||||
|
||||
// Whatever happens, collect the highest quality possible
|
||||
let high_quality_url = latest_vid.streaming_playlists.as_ref().unwrap()[0]
|
||||
.files
|
||||
.iter()
|
||||
.max()
|
||||
.unwrap()
|
||||
.file_download_url
|
||||
.clone();
|
||||
|
||||
// dl_url corresponds to source url if available, best quality if not
|
||||
let dl_url = match source_url {
|
||||
Some(s) => s,
|
||||
None => high_quality_url,
|
||||
};
|
||||
|
||||
debug!("PT download URL: {}", &dl_url);
|
||||
|
||||
let resumable_upload_url = create_resumable_upload(&config.youtube, &latest_vid)
|
||||
@@ -36,7 +53,12 @@ pub async fn run(config: Config, pl: Vec<String>) {
|
||||
.unwrap_or_else(|e| panic!("Cannot retrieve the upload’s resumable id: {e}"));
|
||||
debug!("YT upload URL: {}", &resumable_upload_url);
|
||||
|
||||
let yt_video_id = now_kiss(&dl_url, &resumable_upload_url, &config.youtube)
|
||||
let yt_video_id = now_kiss(
|
||||
&dl_url,
|
||||
&resumable_upload_url,
|
||||
&config.youtube,
|
||||
&config.tootube,
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot resume upload!: {e}"));
|
||||
debug!("YT video ID: {}", &yt_video_id);
|
||||
@@ -46,4 +68,13 @@ pub async fn run(config: Config, pl: Vec<String>) {
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot add video to playlist(s): {e}"));
|
||||
}
|
||||
|
||||
// delete the source video if requested (it won’t be used anymore)
|
||||
if config.peertube.delete_video_source_after_transfer {
|
||||
delete_original_video_source(&latest_vid.uuid, &config.peertube)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot delete source video: {e}"));
|
||||
|
||||
debug!("Original Video {} has been deleted", &latest_vid.uuid);
|
||||
}
|
||||
}
|
||||
|
49
src/main.rs
49
src/main.rs
@@ -10,6 +10,7 @@ fn main() {
|
||||
.arg(
|
||||
Arg::new("config")
|
||||
.short('c')
|
||||
.global(true)
|
||||
.long("config")
|
||||
.value_name("CONFIG_FILE")
|
||||
.help("TOML config file for tootube")
|
||||
@@ -26,31 +27,57 @@ fn main() {
|
||||
.num_args(0..)
|
||||
.display_order(2),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("vice")
|
||||
.long("vice")
|
||||
.aliases(["chybrare", "coquinou"])
|
||||
.action(clap::ArgAction::SetTrue)
|
||||
.display_order(3),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("register")
|
||||
.version(env!("CARGO_PKG_VERSION"))
|
||||
.about("Command to register to YouTube OAuth2.0")
|
||||
.about("Command to register to YouTube or PeerTube 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),
|
||||
Arg::new("youtube")
|
||||
.long("youtube")
|
||||
.short('y')
|
||||
.required(true)
|
||||
.conflicts_with("peertube")
|
||||
.action(clap::ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("peertube")
|
||||
.long("peertube")
|
||||
.short('p')
|
||||
.required(true)
|
||||
.action(clap::ArgAction::SetTrue),
|
||||
),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
if let Some(("register", sub_m)) = matches.subcommand() {
|
||||
let config = parse_toml(sub_m.get_one::<String>("config").unwrap());
|
||||
register(&config.youtube)
|
||||
|
||||
if sub_m.get_flag("youtube") {
|
||||
register_youtube(&config.youtube)
|
||||
.unwrap_or_else(|e| panic!("Cannot register to YouTube API: {}", e));
|
||||
}
|
||||
|
||||
if sub_m.get_flag("peertube") {
|
||||
register_peertube(&config.peertube)
|
||||
.unwrap_or_else(|e| panic!("Cannot register to PeerTube API: {}", e));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let config = parse_toml(matches.get_one::<String>("config").unwrap());
|
||||
let mut config = parse_toml(matches.get_one::<String>("config").unwrap());
|
||||
|
||||
if matches.get_flag("vice") {
|
||||
config.tootube.progress_bar = "{msg}\n[{elapsed_precise}] 8{wide_bar:.magenta}ᗺ {bytes}/{total_bytes} ({bytes_per_sec}, {eta})".to_string();
|
||||
config.tootube.progress_chars = "=D ".to_string();
|
||||
}
|
||||
|
||||
let playlists: Vec<String> = matches
|
||||
.get_many::<String>("playlists")
|
||||
|
207
src/peertube.rs
207
src/peertube.rs
@@ -1,5 +1,10 @@
|
||||
use serde::Deserialize;
|
||||
use std::{boxed::Box, cmp::Ordering, error::Error};
|
||||
use crate::{config::PeertubeConfig, error::TootubeError};
|
||||
use log::debug;
|
||||
use reqwest::Client;
|
||||
use rpassword::prompt_password;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{boxed::Box, cmp::Ordering, error::Error, io::stdin};
|
||||
use tokio::fs::{read_to_string, write};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PeerTubeVideos {
|
||||
@@ -22,6 +27,44 @@ pub struct PeerTubeVideoStreamingPlaylists {
|
||||
pub files: Vec<PeerTubeVideoStreamingPlaylistsFiles>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PeerTubeOauthClientsLocalResponse {
|
||||
client_id: String,
|
||||
client_secret: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct PeerTubeUsersToken {
|
||||
client_id: String,
|
||||
client_secret: String,
|
||||
grant_type: String,
|
||||
password: Option<String>,
|
||||
username: Option<String>,
|
||||
refresh_token: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PeerTubeUsersTokenResponse {
|
||||
refresh_token: String,
|
||||
access_token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PeerTubeVideoSourceResponse {
|
||||
#[serde(rename = "fileDownloadUrl")]
|
||||
pub file_download_url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PeerTubeVideoTokenResponse {
|
||||
files: PeerTubeVideoTokenResponseFiles,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PeerTubeVideoTokenResponseFiles {
|
||||
token: String,
|
||||
}
|
||||
|
||||
#[derive(Eq, Debug, Deserialize)]
|
||||
pub struct PeerTubeVideoStreamingPlaylistsFiles {
|
||||
pub id: u64,
|
||||
@@ -74,3 +117,163 @@ async fn get_video_detail(u: &str, v: &str) -> Result<PeerTubeVideo, Box<dyn Err
|
||||
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// This function makes the registration process a little bit easier
|
||||
#[tokio::main]
|
||||
pub async fn register(config: &PeertubeConfig) -> Result<(), Box<dyn Error>> {
|
||||
// Get client ID/secret
|
||||
let oauth2_client = reqwest::get(format!("{}/api/v1/oauth-clients/local", config.base_url))
|
||||
.await?
|
||||
.json::<PeerTubeOauthClientsLocalResponse>()
|
||||
.await?;
|
||||
|
||||
println!(
|
||||
"Please type your PeerTube username for instance {}:",
|
||||
config.base_url
|
||||
);
|
||||
let mut username = String::new();
|
||||
stdin()
|
||||
.read_line(&mut username)
|
||||
.expect("Unable to read back the username!");
|
||||
|
||||
let password = prompt_password("Your password: ").expect("Unable to read back the password!");
|
||||
|
||||
let params = PeerTubeUsersToken {
|
||||
client_id: oauth2_client.client_id.clone(),
|
||||
client_secret: oauth2_client.client_secret.clone(),
|
||||
grant_type: "password".to_string(),
|
||||
username: Some(username.trim().to_string()),
|
||||
password: Some(password.clone()),
|
||||
refresh_token: None,
|
||||
};
|
||||
|
||||
let client = Client::new();
|
||||
let oauth2_token = client
|
||||
.post(format!("{}/api/v1/users/token", config.base_url))
|
||||
.form(¶ms)
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeUsersTokenResponse>()
|
||||
.await?;
|
||||
|
||||
println!("You can now paste the following lines inside the `peertube` section of your tootube.toml file:");
|
||||
|
||||
println!();
|
||||
|
||||
println!("[peertube.oauth2]");
|
||||
println!("client_id=\"{}\"", oauth2_client.client_id);
|
||||
println!("client_secret=\"{}\"", oauth2_client.client_secret);
|
||||
println!("refresh_token=<path to refresh_token>");
|
||||
|
||||
println!();
|
||||
|
||||
println!("Finally, add the refresh token inside the refresh_token path:");
|
||||
println!("{}", oauth2_token.refresh_token);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_refresh_token(config: &PeertubeConfig) -> Result<String, Box<dyn Error>> {
|
||||
// unwrap is safe here, this function is only called when oauth2 is present
|
||||
let oauth2_config = config.oauth2.as_ref().unwrap().clone();
|
||||
// retrieve the refresh_token from the file
|
||||
let refresh_token = read_to_string(&oauth2_config.refresh_token).await?;
|
||||
|
||||
debug!(
|
||||
"Opened file {} to retrieve Token",
|
||||
&oauth2_config.refresh_token
|
||||
);
|
||||
|
||||
let params = PeerTubeUsersToken {
|
||||
client_id: oauth2_config.client_id.clone(),
|
||||
client_secret: oauth2_config.client_secret.clone(),
|
||||
grant_type: "refresh_token".to_string(),
|
||||
refresh_token: Some(refresh_token),
|
||||
username: None,
|
||||
password: None,
|
||||
};
|
||||
|
||||
let client = Client::new();
|
||||
let oauth2_token = client
|
||||
.post(format!("{}/api/v1/users/token", config.base_url))
|
||||
.form(¶ms)
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeUsersTokenResponse>()
|
||||
.await?;
|
||||
|
||||
debug!("Retrieved access_token: {}", &oauth2_token.access_token);
|
||||
|
||||
// write the new refresh token to the file
|
||||
write(&oauth2_config.refresh_token, oauth2_token.refresh_token).await?;
|
||||
|
||||
debug!(
|
||||
"Written refresh_token to file {}",
|
||||
&oauth2_config.refresh_token
|
||||
);
|
||||
|
||||
Ok(oauth2_token.access_token)
|
||||
}
|
||||
|
||||
pub async fn get_original_video_source(
|
||||
uuid: &str,
|
||||
config: &PeertubeConfig,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
let access_token = get_refresh_token(config).await?;
|
||||
|
||||
let client = Client::new();
|
||||
|
||||
let source_vid = client
|
||||
.get(format!("{}/api/v1/videos/{}/source", config.base_url, uuid))
|
||||
.header("Authorization", format!("Bearer {}", &access_token))
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeVideoSourceResponse>()
|
||||
.await?;
|
||||
|
||||
debug!("Got the Source Vid URL: {}", &source_vid.file_download_url);
|
||||
|
||||
let video_file_token = client
|
||||
.post(format!("{}/api/v1/videos/{}/token", config.base_url, uuid))
|
||||
.header("Authorization", format!("Bearer {}", &access_token))
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeVideoTokenResponse>()
|
||||
.await?;
|
||||
|
||||
debug!("Got the File Token: {}", &video_file_token.files.token);
|
||||
|
||||
Ok(format!(
|
||||
"{}?videoFileToken={}",
|
||||
source_vid.file_download_url, video_file_token.files.token
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn delete_original_video_source(
|
||||
uuid: &str,
|
||||
config: &PeertubeConfig,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let access_token = get_refresh_token(config).await?;
|
||||
|
||||
let client = Client::new();
|
||||
|
||||
let res = client
|
||||
.delete(format!(
|
||||
"{}/api/v1/videos/{}/source/file",
|
||||
config.base_url, uuid
|
||||
))
|
||||
.header("Authorization", format!("Bearer {}", &access_token))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !res.status().is_success() {
|
||||
return Err(TootubeError::new(&format!(
|
||||
"Cannot delete source video file {}: {}",
|
||||
uuid,
|
||||
res.text().await?
|
||||
))
|
||||
.into());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@@ -1,8 +1,12 @@
|
||||
use crate::{config::YoutubeConfig, error::TootubeError, peertube::PeerTubeVideo};
|
||||
use crate::{
|
||||
config::{TootubeConfig, YoutubeConfig},
|
||||
error::TootubeError,
|
||||
peertube::PeerTubeVideo,
|
||||
};
|
||||
use async_stream::stream;
|
||||
use futures_util::StreamExt;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use log::debug;
|
||||
use log::{debug, warn};
|
||||
use reqwest::{multipart::Form, Body, Client};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{cmp::min, error::Error, io::stdin};
|
||||
@@ -249,7 +253,7 @@ async fn get_playlist_ids(
|
||||
// if nextPageToken is present, continue the loop
|
||||
match local_pl.next_page_token {
|
||||
None => break,
|
||||
Some(a) => page_token = a.clone(),
|
||||
Some(a) => page_token.clone_from(&a),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,10 +309,17 @@ pub async fn create_resumable_upload(
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
let access_token = refresh_token(config).await?;
|
||||
|
||||
if vid.name.chars().count() > 100 {
|
||||
warn!(
|
||||
"PT Video Title ({}) is too long, it will be truncated",
|
||||
&vid.name
|
||||
);
|
||||
}
|
||||
|
||||
let upload_params = YoutubeUploadParams {
|
||||
snippet: {
|
||||
YoutubeUploadParamsSnippet {
|
||||
title: vid.name.clone(),
|
||||
title: vid.name.chars().take(100).collect::<String>(),
|
||||
description: vid.description.clone(),
|
||||
tags: vid.tags.clone(),
|
||||
..Default::default()
|
||||
@@ -345,6 +356,7 @@ pub async fn now_kiss(
|
||||
dl_url: &str,
|
||||
r_url: &str,
|
||||
config: &YoutubeConfig,
|
||||
pg_conf: &TootubeConfig,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
// Get access token
|
||||
let access_token = refresh_token(config).await?;
|
||||
@@ -358,9 +370,11 @@ pub async fn now_kiss(
|
||||
|
||||
// Create the progress bar
|
||||
let pb = ProgressBar::new(content_lengh);
|
||||
pb.set_style(ProgressStyle::default_bar()
|
||||
.template("{msg}\n{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")?
|
||||
.progress_chars("#>-"));
|
||||
pb.set_style(
|
||||
ProgressStyle::default_bar()
|
||||
.template(&pg_conf.progress_bar)?
|
||||
.progress_chars(&pg_conf.progress_chars),
|
||||
);
|
||||
pb.set_message("Transferring…");
|
||||
let mut transferring: u64 = 0;
|
||||
|
||||
|
Reference in New Issue
Block a user