mirror of
https://framagit.org/veretcle/tootube.git
synced 2025-07-20 20:41:17 +02:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a83140145c | ||
![]() |
e46889f1e1 | ||
![]() |
61f19c0ea1 | ||
![]() |
0ae501f1b9 | ||
![]() |
5383c8d216 | ||
![]() |
d70fbdac98 | ||
![]() |
982dc8b954 | ||
![]() |
bf622d6989 | ||
![]() |
a687f008df |
536
Cargo.lock
generated
536
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.4"
|
||||
version = "0.7.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
|
||||
```
|
||||
|
@@ -13,6 +13,26 @@ pub struct Config {
|
||||
#[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)]
|
||||
|
92
src/lib.rs
92
src/lib.rs
@@ -7,48 +7,102 @@ pub use config::parse_toml;
|
||||
use config::Config;
|
||||
|
||||
mod peertube;
|
||||
use peertube::get_latest_video;
|
||||
pub use peertube::register as register_peertube;
|
||||
use peertube::{get_playlists_to_be_added_to, PeerTube};
|
||||
|
||||
mod youtube;
|
||||
pub use youtube::register;
|
||||
use youtube::{add_video_to_playlists, create_resumable_upload, now_kiss};
|
||||
pub use youtube::register as register_youtube;
|
||||
use youtube::YouTube;
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn run(config: Config, pl: Vec<String>) {
|
||||
// Create PeerTube struct
|
||||
let peertube = match &config.peertube.oauth2 {
|
||||
Some(s) => PeerTube::new(&config.peertube.base_url)
|
||||
.with_client(&s.client_id, &s.client_secret, &s.refresh_token)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot instantiate PeerTube struct: {}", e)),
|
||||
None => PeerTube::new(&config.peertube.base_url),
|
||||
};
|
||||
// Get the latest video object
|
||||
let latest_vid = get_latest_video(&config.peertube.base_url)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
panic!("Cannot retrieve the latest video, something must have gone terribly wrong: {e}")
|
||||
});
|
||||
let latest_vid = peertube.get_latest_video().await.unwrap_or_else(|e| {
|
||||
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(_) => peertube
|
||||
.get_original_video_source(&latest_vid.uuid)
|
||||
.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)
|
||||
let youtube = YouTube::new(
|
||||
&config.youtube.client_id,
|
||||
&config.youtube.client_secret,
|
||||
&config.youtube.refresh_token,
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot instantiate YouTube struct: {}", e));
|
||||
|
||||
let resumable_upload_url = youtube
|
||||
.create_resumable_upload(&latest_vid)
|
||||
.await
|
||||
.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,
|
||||
&config.tootube,
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot resume upload!: {e}"));
|
||||
let yt_video_id = youtube
|
||||
.now_kiss(&dl_url, &resumable_upload_url, &config.tootube)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot resume upload!: {e}"));
|
||||
debug!("YT video ID: {}", &yt_video_id);
|
||||
|
||||
if !pl.is_empty() {
|
||||
add_video_to_playlists(&config.youtube, &yt_video_id, &pl)
|
||||
youtube
|
||||
.add_video_to_playlists(&yt_video_id, &pl)
|
||||
.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 {
|
||||
peertube
|
||||
.delete_original_video_source(&latest_vid.uuid)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot delete source video: {e}"));
|
||||
|
||||
debug!("Original Video {} has been deleted", &latest_vid.uuid);
|
||||
}
|
||||
|
||||
// Updates the playlist of PeerTube if necessary
|
||||
if config.peertube.oauth2.is_some() && !pl.is_empty() {
|
||||
debug!("Updating playlists on PeerTube");
|
||||
|
||||
if let Ok(pl_to_add_to) =
|
||||
get_playlists_to_be_added_to(&peertube, &latest_vid.uuid, &pl, latest_vid.channel.id)
|
||||
.await
|
||||
{
|
||||
for p in pl_to_add_to {
|
||||
let _ = peertube.add_video_to_playlist(&latest_vid.uuid, &p).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
40
src/main.rs
40
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")
|
||||
@@ -29,32 +30,45 @@ fn main() {
|
||||
.arg(
|
||||
Arg::new("vice")
|
||||
.long("vice")
|
||||
.alias("chybrare")
|
||||
.alias("coquinou")
|
||||
.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)
|
||||
.unwrap_or_else(|e| panic!("Cannot register to YouTube API: {}", e));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
463
src/peertube.rs
463
src/peertube.rs
@@ -1,5 +1,14 @@
|
||||
use serde::Deserialize;
|
||||
use std::{boxed::Box, cmp::Ordering, error::Error};
|
||||
use crate::{config::PeertubeConfig, error::TootubeError};
|
||||
use log::debug;
|
||||
use reqwest::{
|
||||
header::{HeaderMap, HeaderValue},
|
||||
multipart::Form,
|
||||
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 {
|
||||
@@ -15,6 +24,12 @@ pub struct PeerTubeVideo {
|
||||
#[serde(rename = "streamingPlaylists")]
|
||||
pub streaming_playlists: Option<Vec<PeerTubeVideoStreamingPlaylists>>,
|
||||
pub tags: Option<Vec<String>>,
|
||||
pub channel: PeerTubeVideoChannel,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PeerTubeVideoChannel {
|
||||
pub id: u8,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -22,6 +37,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,
|
||||
@@ -53,24 +106,400 @@ pub struct PeerTubeVideoStreamingPlaylistsFilesResolution {
|
||||
pub id: u16,
|
||||
}
|
||||
|
||||
/// This gets the last video uploaded to the PeerTube server
|
||||
pub async fn get_latest_video(u: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
||||
let body = reqwest::get(format!("{}/api/v1/videos?count=1&sort=-publishedAt", u))
|
||||
.await?
|
||||
.json::<PeerTubeVideos>()
|
||||
.await?;
|
||||
|
||||
let vid = get_video_detail(u, &body.data[0].uuid).await?;
|
||||
|
||||
Ok(vid)
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PeerTubeVideoPlaylists {
|
||||
pub total: u16,
|
||||
pub data: Vec<PeerTubeVideoPlaylist>,
|
||||
}
|
||||
|
||||
/// This gets all the crispy details about one particular video
|
||||
async fn get_video_detail(u: &str, v: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
||||
let body = reqwest::get(format!("{}/api/v1/videos/{}", u, v))
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PeerTubeVideoPlaylist {
|
||||
pub id: u16,
|
||||
pub uuid: String,
|
||||
#[serde(rename = "displayName")]
|
||||
pub display_name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct PeerTubeVideoPlaylistsVideos {
|
||||
#[serde(rename = "videoId")]
|
||||
video_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PeerTubeVideoPlaylistResponse {
|
||||
#[serde(rename = "videoPlaylist")]
|
||||
video_playlist: PeerTubeVideoPlaylistResponseVideoPlaylist,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PeerTubeVideoPlaylistResponseVideoPlaylist {
|
||||
uuid: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PeerTubeVideoPlaylistsPlaylistIdVideos {
|
||||
total: u32,
|
||||
data: Vec<PeerTubeVideoPlaylistsPlaylistIdVideosData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PeerTubeVideoPlaylistsPlaylistIdVideosData {
|
||||
video: PeerTubeVideo,
|
||||
}
|
||||
|
||||
/// 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::<PeerTubeVideo>()
|
||||
.json::<PeerTubeOauthClientsLocalResponse>()
|
||||
.await?;
|
||||
|
||||
Ok(body)
|
||||
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(())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PeerTube {
|
||||
base_url: String,
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl PeerTube {
|
||||
/// Create a new PeerTube struct with a basic embedded reqwest::Client
|
||||
pub fn new(base_url: &str) -> Self {
|
||||
PeerTube {
|
||||
base_url: format!("{}/api/v1", base_url),
|
||||
client: Client::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve the refresh_token and access_token and update the embedded reqwest::Client to have
|
||||
/// the default required header
|
||||
pub async fn with_client(
|
||||
mut self,
|
||||
client_id: &str,
|
||||
client_secret: &str,
|
||||
refresh_token_path: &str,
|
||||
) -> Result<Self, Box<dyn Error>> {
|
||||
let refresh_token = read_to_string(refresh_token_path).await?;
|
||||
|
||||
let params = PeerTubeUsersToken {
|
||||
client_id: client_id.to_string(),
|
||||
client_secret: client_secret.to_string(),
|
||||
grant_type: "refresh_token".to_string(),
|
||||
refresh_token: Some(refresh_token),
|
||||
username: None,
|
||||
password: None,
|
||||
};
|
||||
|
||||
let req = self
|
||||
.client
|
||||
.post(&format!("{}/users/token", self.base_url))
|
||||
.form(¶ms)
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeUsersTokenResponse>()
|
||||
.await?;
|
||||
|
||||
write(refresh_token_path, req.refresh_token).await?;
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"Authorization",
|
||||
HeaderValue::from_str(&format!("Bearer {}", req.access_token))?,
|
||||
);
|
||||
|
||||
self.client = reqwest::Client::builder()
|
||||
.default_headers(headers)
|
||||
.build()?;
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// This gets the last video uploaded to the PeerTube server
|
||||
pub async fn get_latest_video(&self) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
||||
let body = self
|
||||
.client
|
||||
.get(format!(
|
||||
"{}/videos?count=1&sort=-publishedAt",
|
||||
self.base_url
|
||||
))
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeVideos>()
|
||||
.await?;
|
||||
|
||||
let vid = self.get_video_detail(&body.data[0].uuid).await?;
|
||||
|
||||
Ok(vid)
|
||||
}
|
||||
|
||||
/// This gets all the crispy details about one particular video
|
||||
async fn get_video_detail(&self, v: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
||||
let body = self
|
||||
.client
|
||||
.get(format!("{}/videos/{}", self.base_url, v))
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeVideo>()
|
||||
.await?;
|
||||
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// Get the original video source
|
||||
pub async fn get_original_video_source(&self, uuid: &str) -> Result<String, Box<dyn Error>> {
|
||||
let source_vid = self
|
||||
.client
|
||||
.get(format!("{}/videos/{}/source", self.base_url, uuid))
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeVideoSourceResponse>()
|
||||
.await?;
|
||||
|
||||
debug!("Got the Source Vid URL: {}", &source_vid.file_download_url);
|
||||
|
||||
let video_file_token = self
|
||||
.client
|
||||
.post(format!("{}/videos/{}/token", self.base_url, uuid))
|
||||
.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
|
||||
))
|
||||
}
|
||||
|
||||
/// Delete the original video source
|
||||
pub async fn delete_original_video_source(&self, uuid: &str) -> Result<(), Box<dyn Error>> {
|
||||
let res = self
|
||||
.client
|
||||
.delete(format!("{}/videos/{}/source/file", self.base_url, uuid))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !res.status().is_success() {
|
||||
return Err(TootubeError::new(&format!(
|
||||
"Cannot delete source video file {}: {}",
|
||||
uuid,
|
||||
res.text().await?
|
||||
))
|
||||
.into());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// List every playlists on PeerTube
|
||||
pub async fn list_video_playlists(&self) -> Result<Vec<PeerTubeVideoPlaylist>, Box<dyn Error>> {
|
||||
let mut playlists: Vec<PeerTubeVideoPlaylist> = vec![];
|
||||
|
||||
let mut start = 0;
|
||||
let inc = 15;
|
||||
|
||||
while let Ok(mut local_pl) = self
|
||||
.client
|
||||
.get(&format!(
|
||||
"{}/video-playlists?count={}&playlistType=1&start={}",
|
||||
self.base_url, inc, start
|
||||
))
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeVideoPlaylists>()
|
||||
.await
|
||||
{
|
||||
start += inc;
|
||||
playlists.append(&mut local_pl.data);
|
||||
if start >= local_pl.total {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(playlists)
|
||||
}
|
||||
|
||||
/// Add a public playlist
|
||||
pub async fn create_video_playlist(
|
||||
&self,
|
||||
c_id: u8,
|
||||
display_name: &str,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
let form = Form::new()
|
||||
.text("displayName", display_name.to_string())
|
||||
.text("privacy", "1")
|
||||
.text("videoChannelId", format!("{}", c_id));
|
||||
|
||||
let pl_created = self
|
||||
.client
|
||||
.post(&format!("{}/video-playlists", self.base_url))
|
||||
.multipart(form)
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeVideoPlaylistResponse>()
|
||||
.await?;
|
||||
|
||||
Ok(pl_created.video_playlist.uuid)
|
||||
}
|
||||
|
||||
/// Add a video into a playlist
|
||||
pub async fn add_video_to_playlist(
|
||||
&self,
|
||||
vid_uuid: &str,
|
||||
pl_uuid: &str,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let video_to_add = PeerTubeVideoPlaylistsVideos {
|
||||
video_id: vid_uuid.to_string(),
|
||||
};
|
||||
|
||||
let res = self
|
||||
.client
|
||||
.post(&format!(
|
||||
"{}/video-playlists/{}/videos",
|
||||
self.base_url, pl_uuid
|
||||
))
|
||||
.json(&video_to_add)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !res.status().is_success() {
|
||||
return Err(TootubeError::new(&format!(
|
||||
"Cannot add video {} to playlist {}: {}",
|
||||
vid_uuid,
|
||||
pl_uuid,
|
||||
res.text().await?
|
||||
))
|
||||
.into());
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// List all videos of a playlist
|
||||
pub async fn list_videos_playlist(&self, uuid: &str) -> Result<Vec<String>, Box<dyn Error>> {
|
||||
let mut videos: Vec<PeerTubeVideo> = vec![];
|
||||
|
||||
let mut start = 0;
|
||||
let inc = 15;
|
||||
|
||||
while let Ok(l_vid) = self
|
||||
.client
|
||||
.get(&format!(
|
||||
"{}/video-playlists/{}/videos?start={}&count={}",
|
||||
&self.base_url, &uuid, start, inc
|
||||
))
|
||||
.send()
|
||||
.await?
|
||||
.json::<PeerTubeVideoPlaylistsPlaylistIdVideos>()
|
||||
.await
|
||||
{
|
||||
start += inc;
|
||||
videos.append(&mut l_vid.data.into_iter().map(|x| x.video).collect());
|
||||
if start >= l_vid.total {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(videos.into_iter().map(|x| x.uuid).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// Given a PeerTube instance, video UUID, list of named playlists and channel ID, this function:
|
||||
/// * adds playlists if they do not exists
|
||||
/// * adds the video to said playlists if they’re not in it already
|
||||
pub async fn get_playlists_to_be_added_to(
|
||||
peertube: &PeerTube,
|
||||
vid_uuid: &str,
|
||||
pl: &[String],
|
||||
c_id: u8,
|
||||
) -> Result<Vec<String>, Box<dyn Error>> {
|
||||
let mut playlist_to_be_added_to: Vec<String> = vec![];
|
||||
|
||||
if let Ok(local_pl) = peertube.list_video_playlists().await {
|
||||
// list the displayNames of each playlist
|
||||
let current_playlist: Vec<String> =
|
||||
local_pl.iter().map(|s| s.display_name.clone()).collect();
|
||||
// get the playlist whose displayName does not exist yet
|
||||
let pl_to_create: Vec<_> = pl
|
||||
.iter()
|
||||
.filter(|x| !current_playlist.contains(x))
|
||||
.collect();
|
||||
|
||||
debug!("Playlists to be added: {:?}", &pl_to_create);
|
||||
|
||||
playlist_to_be_added_to = local_pl
|
||||
.into_iter()
|
||||
.filter(|x| pl.contains(&x.display_name))
|
||||
.map(|x| x.uuid.clone())
|
||||
.collect();
|
||||
|
||||
// create the missing playlists
|
||||
for p in pl_to_create {
|
||||
if let Ok(s) = peertube.create_video_playlist(c_id, p).await {
|
||||
playlist_to_be_added_to.push(s);
|
||||
}
|
||||
}
|
||||
|
||||
for p in playlist_to_be_added_to.clone().iter() {
|
||||
if let Ok(s) = peertube.list_videos_playlist(p).await {
|
||||
// if a video already exists inside a playlist, drop it
|
||||
if s.contains(&vid_uuid.to_string()) {
|
||||
playlist_to_be_added_to.retain(|i| *i != *p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Playlists to be added to: {:?}", &playlist_to_be_added_to);
|
||||
};
|
||||
|
||||
Ok(playlist_to_be_added_to)
|
||||
}
|
||||
|
400
src/youtube.rs
400
src/youtube.rs
@@ -7,12 +7,13 @@ use async_stream::stream;
|
||||
use futures_util::StreamExt;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use log::{debug, warn};
|
||||
use reqwest::{multipart::Form, Body, Client};
|
||||
use reqwest::{
|
||||
header::{HeaderMap, HeaderValue},
|
||||
multipart::Form,
|
||||
Body, Client,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{cmp::min, error::Error, io::stdin};
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
static ACCESS_TOKEN: OnceCell<String> = OnceCell::const_new();
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct RefreshTokenRequest {
|
||||
@@ -193,219 +194,216 @@ pub async fn register(config: &YoutubeConfig) -> Result<(), Box<dyn Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Ensures that Token has been refreshed and that it is unique
|
||||
async fn refresh_token(config: &YoutubeConfig) -> Result<String, reqwest::Error> {
|
||||
ACCESS_TOKEN
|
||||
.get_or_try_init(|| async {
|
||||
let refresh_token = RefreshTokenRequest {
|
||||
refresh_token: config.refresh_token.clone(),
|
||||
client_id: config.client_id.clone(),
|
||||
client_secret: config.client_secret.clone(),
|
||||
..Default::default()
|
||||
pub struct YouTube {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl YouTube {
|
||||
pub async fn new(
|
||||
client_id: &str,
|
||||
client_secret: &str,
|
||||
refresh_token: &str,
|
||||
) -> Result<Self, Box<dyn Error>> {
|
||||
let mut youtube = YouTube {
|
||||
client: Client::new(),
|
||||
};
|
||||
|
||||
let refresh_token = RefreshTokenRequest {
|
||||
refresh_token: refresh_token.to_string(),
|
||||
client_id: client_id.to_string(),
|
||||
client_secret: client_secret.to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let access_token = youtube
|
||||
.client
|
||||
.post("https://accounts.google.com/o/oauth2/token")
|
||||
.json(&refresh_token)
|
||||
.send()
|
||||
.await?
|
||||
.json::<AccessTokenResponse>()
|
||||
.await?;
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"Authorization",
|
||||
HeaderValue::from_str(&format!("Bearer {}", access_token.access_token))?,
|
||||
);
|
||||
|
||||
youtube.client = reqwest::Client::builder()
|
||||
.default_headers(headers)
|
||||
.build()?;
|
||||
|
||||
Ok(youtube)
|
||||
}
|
||||
/// This function takes a list of playlists keyword and returns a list of playlist ID
|
||||
async fn get_playlist_ids(&self, pl: &[String]) -> Result<Vec<String>, Box<dyn Error>> {
|
||||
let mut page_token = String::new();
|
||||
let mut playlists: Vec<String> = vec![];
|
||||
|
||||
while let Ok(local_pl) = self.client
|
||||
.get(&format!(
|
||||
"https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true&pageToken={}",
|
||||
page_token
|
||||
))
|
||||
.send()
|
||||
.await?
|
||||
.json::<YoutubePlaylistListResponse>()
|
||||
.await
|
||||
{
|
||||
playlists.append(
|
||||
&mut local_pl
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|s| pl.contains(&s.snippet.title).then_some(s.id.clone()))
|
||||
.collect(),
|
||||
);
|
||||
// if nextPageToken is present, continue the loop
|
||||
match local_pl.next_page_token {
|
||||
None => break,
|
||||
Some(a) => page_token.clone_from(&a),
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Playlists IDs: {:?}", &playlists);
|
||||
Ok(playlists)
|
||||
}
|
||||
|
||||
/// This function adds the video id to the corresponding named playlist(s)
|
||||
pub async fn add_video_to_playlists(
|
||||
&self,
|
||||
v: &str,
|
||||
pl: &[String],
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let playlists_ids = self.get_playlist_ids(pl).await?;
|
||||
|
||||
for pl_id in playlists_ids {
|
||||
let yt_pl_upload_params = YoutubePlaylistItemsParams {
|
||||
snippet: YoutubePlaylistItemsParamsSnippet {
|
||||
playlist_id: pl_id.clone(),
|
||||
resource_id: YoutubePlaylistItemsParamsSnippetResourceId {
|
||||
video_id: v.to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
|
||||
let client = Client::new();
|
||||
let res = client
|
||||
.post("https://accounts.google.com/o/oauth2/token")
|
||||
.json(&refresh_token)
|
||||
let res = self
|
||||
.client
|
||||
.post("https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet")
|
||||
.json(&yt_pl_upload_params)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let access_token: AccessTokenResponse = res.json().await?;
|
||||
|
||||
debug!("YT Access Token: {}", &access_token.access_token);
|
||||
Ok(access_token.access_token)
|
||||
})
|
||||
.await
|
||||
.cloned()
|
||||
}
|
||||
|
||||
/// This function takes a list of playlists keyword and returns a list of playlist ID
|
||||
async fn get_playlist_ids(
|
||||
config: &YoutubeConfig,
|
||||
pl: &[String],
|
||||
) -> Result<Vec<String>, Box<dyn Error>> {
|
||||
let mut page_token = String::new();
|
||||
let mut playlists: Vec<String> = vec![];
|
||||
|
||||
let access_token = refresh_token(config).await?;
|
||||
let client = Client::new();
|
||||
|
||||
while let Ok(local_pl) = client
|
||||
.get(&format!(
|
||||
"https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true&pageToken={}",
|
||||
page_token
|
||||
))
|
||||
.header("Authorization", format!("Bearer {}", access_token))
|
||||
.send()
|
||||
.await?
|
||||
.json::<YoutubePlaylistListResponse>()
|
||||
.await
|
||||
{
|
||||
playlists.append(
|
||||
&mut local_pl
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|s| pl.contains(&s.snippet.title).then_some(s.id.clone()))
|
||||
.collect(),
|
||||
);
|
||||
|
||||
// if nextPageToken is present, continue the loop
|
||||
match local_pl.next_page_token {
|
||||
None => break,
|
||||
Some(a) => page_token = a.clone(),
|
||||
if !res.status().is_success() {
|
||||
return Err(TootubeError::new(&format!(
|
||||
"Something went wrong when trying to add the video to a playlist: {}",
|
||||
res.text().await?
|
||||
))
|
||||
.into());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
debug!("Playlists IDs: {:?}", &playlists);
|
||||
Ok(playlists)
|
||||
}
|
||||
/// This function creates a resumable YT upload, putting all the parameters in
|
||||
pub async fn create_resumable_upload(
|
||||
&self,
|
||||
vid: &PeerTubeVideo,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
if vid.name.chars().count() > 100 {
|
||||
warn!(
|
||||
"PT Video Title ({}) is too long, it will be truncated",
|
||||
&vid.name
|
||||
);
|
||||
}
|
||||
|
||||
/// This function adds the video id to the corresponding named playlist(s)
|
||||
pub async fn add_video_to_playlists(
|
||||
config: &YoutubeConfig,
|
||||
v: &str,
|
||||
pl: &[String],
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let access_token = refresh_token(config).await?;
|
||||
let playlists_ids = get_playlist_ids(config, pl).await?;
|
||||
|
||||
for pl_id in playlists_ids {
|
||||
let yt_pl_upload_params = YoutubePlaylistItemsParams {
|
||||
snippet: YoutubePlaylistItemsParamsSnippet {
|
||||
playlist_id: pl_id.clone(),
|
||||
resource_id: YoutubePlaylistItemsParamsSnippetResourceId {
|
||||
video_id: v.to_string(),
|
||||
let upload_params = YoutubeUploadParams {
|
||||
snippet: {
|
||||
YoutubeUploadParamsSnippet {
|
||||
title: vid.name.chars().take(100).collect::<String>(),
|
||||
description: vid.description.clone(),
|
||||
tags: vid.tags.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
}
|
||||
},
|
||||
status: {
|
||||
YoutubeUploadParamsStatus {
|
||||
..Default::default()
|
||||
}
|
||||
},
|
||||
};
|
||||
debug!("YT upload params: {:?}", &upload_params);
|
||||
|
||||
let client = Client::new();
|
||||
let res = client
|
||||
.post("https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet")
|
||||
.header("Authorization", format!("Bearer {}", access_token))
|
||||
.json(&yt_pl_upload_params)
|
||||
let res = self.client.post("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet%2Cstatus")
|
||||
.json(&upload_params)
|
||||
.send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
Ok(res
|
||||
.headers()
|
||||
.get("location")
|
||||
.ok_or("Cannot find suitable header")?
|
||||
.to_str()?
|
||||
.to_string())
|
||||
} else {
|
||||
Err(TootubeError::new("Cannot create resumable upload!").into())
|
||||
}
|
||||
}
|
||||
/// This takes the PT stream for download, connects it to YT stream for upload
|
||||
pub async fn now_kiss(
|
||||
&self,
|
||||
dl_url: &str,
|
||||
r_url: &str,
|
||||
pg_conf: &TootubeConfig,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
// Get the upstream bytes stream
|
||||
let res = reqwest::get(dl_url).await?;
|
||||
let content_lengh = res
|
||||
.content_length()
|
||||
.ok_or(format!("Cannot get content length from {}", dl_url))?;
|
||||
let mut stream = res.bytes_stream();
|
||||
|
||||
// Create the progress bar
|
||||
let pb = ProgressBar::new(content_lengh);
|
||||
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;
|
||||
|
||||
// yields the stream chunk by chunk, updating the progress bar at the same time
|
||||
let async_stream = stream! {
|
||||
while let Some(chunk) = stream.next().await {
|
||||
if let Ok(chunk) = &chunk {
|
||||
let new = min(transferring + (chunk.len() as u64), content_lengh);
|
||||
transferring = new;
|
||||
pb.set_position(new);
|
||||
if transferring >= content_lengh {
|
||||
pb.finish();
|
||||
}
|
||||
}
|
||||
yield chunk;
|
||||
}
|
||||
};
|
||||
|
||||
// Create client
|
||||
let res = self
|
||||
.client
|
||||
.put(r_url)
|
||||
.body(Body::wrap_stream(async_stream))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !res.status().is_success() {
|
||||
return Err(TootubeError::new(&format!(
|
||||
"Something went wrong when trying to add the video to a playlist: {}",
|
||||
res.text().await?
|
||||
))
|
||||
.into());
|
||||
if res.status().is_success() {
|
||||
let yt_videos: YoutubeVideos = res.json().await?;
|
||||
Ok(yt_videos.id)
|
||||
} else {
|
||||
Err(TootubeError::new(&format!("Cannot upload video: {:?}", res.text().await?)).into())
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// This function creates a resumable YT upload, putting all the parameters in
|
||||
pub async fn create_resumable_upload(
|
||||
config: &YoutubeConfig,
|
||||
vid: &PeerTubeVideo,
|
||||
) -> 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.chars().take(100).collect::<String>(),
|
||||
description: vid.description.clone(),
|
||||
tags: vid.tags.clone(),
|
||||
..Default::default()
|
||||
}
|
||||
},
|
||||
status: {
|
||||
YoutubeUploadParamsStatus {
|
||||
..Default::default()
|
||||
}
|
||||
},
|
||||
};
|
||||
debug!("YT upload params: {:?}", &upload_params);
|
||||
|
||||
let client = Client::new();
|
||||
let res = client.post("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet%2Cstatus")
|
||||
.header("Authorization", format!("Bearer {}", access_token))
|
||||
.json(&upload_params)
|
||||
.send().await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
Ok(res
|
||||
.headers()
|
||||
.get("location")
|
||||
.ok_or("Cannot find suitable header")?
|
||||
.to_str()?
|
||||
.to_string())
|
||||
} else {
|
||||
Err(TootubeError::new("Cannot create resumable upload!").into())
|
||||
}
|
||||
}
|
||||
|
||||
/// This takes the PT stream for download, connects it to YT stream for upload
|
||||
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?;
|
||||
|
||||
// Get the upstream bytes stream
|
||||
let res = reqwest::get(dl_url).await?;
|
||||
let content_lengh = res
|
||||
.content_length()
|
||||
.ok_or(format!("Cannot get content length from {}", dl_url))?;
|
||||
let mut stream = res.bytes_stream();
|
||||
|
||||
// Create the progress bar
|
||||
let pb = ProgressBar::new(content_lengh);
|
||||
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;
|
||||
|
||||
// yields the stream chunk by chunk, updating the progress bar at the same time
|
||||
let async_stream = stream! {
|
||||
while let Some(chunk) = stream.next().await {
|
||||
if let Ok(chunk) = &chunk {
|
||||
let new = min(transferring + (chunk.len() as u64), content_lengh);
|
||||
transferring = new;
|
||||
pb.set_position(new);
|
||||
if transferring >= content_lengh {
|
||||
pb.finish();
|
||||
}
|
||||
}
|
||||
yield chunk;
|
||||
}
|
||||
};
|
||||
|
||||
// Create client
|
||||
let client = Client::new();
|
||||
let res = client
|
||||
.put(r_url)
|
||||
.header("Authorization", format!("Bearer {}", access_token))
|
||||
.body(Body::wrap_stream(async_stream))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if res.status().is_success() {
|
||||
let yt_videos: YoutubeVideos = res.json().await?;
|
||||
Ok(yt_videos.id)
|
||||
} else {
|
||||
Err(TootubeError::new(&format!("Cannot upload video: {:?}", res.text().await?)).into())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user