15 Commits

Author SHA1 Message Date
VC
6db9009d56 Merge branch '12-write-tokens-inside-the-main-config-file' into 'master'
Resolve "Write Tokens inside the main config file"

Closes #12

See merge request veretcle/tootube!27
2024-09-09 12:32:18 +00:00
VC
4c2433a79e : write back tokens to the config file 2024-09-09 14:20:54 +02:00
VC
5f03c3b8f1 💄: make clippy happy 2024-09-09 11:07:20 +02:00
VC
be1e55650e ⬆️: update version 2024-09-09 11:06:43 +02:00
VC
83eae99f33 Merge branch '11-select-a-pt-video-id-directly' into 'master'
: upload specific video

Closes #11

See merge request veretcle/tootube!26
2024-08-20 18:49:28 +00:00
VC
fee6b51f45 : upload specific video 2024-08-20 20:47:46 +02:00
VC
ef40d7ad9a Merge branch '10-handle-video-thumbnail' into 'master'
: upload thumbnail

Closes #10

See merge request veretcle/tootube!25
2024-08-20 08:35:10 +00:00
VC
d1e86de937 : upload thumbnail 2024-08-20 10:33:38 +02:00
VC
59ea11f770 Merge branch 'fix/warning' into 'master'
🔥: remove unused IDs

See merge request veretcle/tootube!24
2024-08-20 05:45:26 +00:00
VC
ea59d9be3f Merge branch 'fix/warning' into 'master'
🔥: remove unused IDs

See merge request veretcle/tootube!24
2024-07-22 08:08:47 +00:00
VC
2b70e6e32f Merge branch 'fix/warning' into 'master'
🔥: remove unused IDs

See merge request veretcle/tootube!24
2024-07-22 08:01:32 +00:00
VC
f5e0c660c9 🔥: remove unused IDs 2024-07-22 09:53:56 +02:00
VC
e2cf84a889 Merge branch '8-this-lacks-tests' into 'master'
Resolve "This lacks tests"

Closes #8

See merge request veretcle/tootube!22
2024-05-17 10:02:57 +00:00
VC
cc86a88771 🔖: bump version 2024-05-17 11:59:26 +02:00
VC
73572cde30 : add tests to peertube lib 2024-05-17 11:58:12 +02:00
8 changed files with 423 additions and 284 deletions

410
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
[package] [package]
name = "tootube" name = "tootube"
authors = ["VC <veretcle+framagit@mateu.be>"] authors = ["VC <veretcle+framagit@mateu.be>"]
version = "0.7.0" version = "0.9.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -1,6 +1,6 @@
# What is it? # What is it?
This program takes the very last video published on a PeerTube instance and pushes it to a corresponding YouTube channel. This program takes the very last or a specified video published on a PeerTube instance and pushes it to a corresponding YouTube channel.
# What the status of this? # What the status of this?
@@ -12,7 +12,7 @@ So consider this a work in progress that will slowly get better with time.
# What does it do exactly? # What does it do exactly?
* it retrieves the latest PeerTube video download URL from an instance * it retrieves the latest (or a specified) 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 * 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 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) * it downloads/uploads the latest PeerTube video to YouTube without using a cache (stream-to-stream)
@@ -54,8 +54,8 @@ delete_video_source_after_transfer=true # this option is only available if you h
# everything below is given by the register command with --peertube option # everything below is given by the register command with --peertube option
[peertube.oauth2] [peertube.oauth2]
client_id="<YOUR CLIENT_ID>" client_id="<YOUR CLIENT_ID>"
client_secret="<YOUR CLIENT_SECRET" 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 refresh_token="<YOUR CLIENT TOKEN>"
[youtube] [youtube]
refresh_token="" # leave empty for now refresh_token="" # leave empty for now
@@ -69,7 +69,7 @@ Then run:
tootube register --youtube --config <PATH TO YOUR TOOTUBE.TOML FILE> tootube register --youtube --config <PATH TO YOUR TOOTUBE.TOML FILE>
``` ```
Youll be then prompted with all the necessary information to register `tootube`. Youll end with a `refresh_token` that you can now paste inside your tootube configuration. Youll be then prompted with all the necessary information to register `tootube`. Youll end with a `refresh_token` that you will be written back to the config file.
If you wish to register `tootube` on PeerTube, you can do so by using: If you wish to register `tootube` on PeerTube, you can do so by using:
@@ -77,8 +77,4 @@ If you wish to register `tootube` on PeerTube, you can do so by using:
tootube register --peertube --config <PATH TO YOUR TOOTUBE.TOML FILE> 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: 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 will be written back to the config file.
```bash
echo -n '<REFRESH_TOKEN>' > /var/lib/tootube/refresh_token
```

View File

@@ -1,8 +1,11 @@
use serde::Deserialize; use serde::{Deserialize, Serialize};
use std::fs::read_to_string; use std::{
error::Error,
fs::{read_to_string, write},
};
// General configuration Struct // General configuration Struct
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct Config { pub struct Config {
pub peertube: PeertubeConfig, pub peertube: PeertubeConfig,
pub youtube: YoutubeConfig, pub youtube: YoutubeConfig,
@@ -10,7 +13,7 @@ pub struct Config {
pub tootube: TootubeConfig, pub tootube: TootubeConfig,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct PeertubeConfig { pub struct PeertubeConfig {
pub base_url: String, pub base_url: String,
#[serde(default)] #[serde(default)]
@@ -28,21 +31,21 @@ impl Default for PeertubeConfig {
} }
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PeertubeConfigOauth2 { pub struct PeertubeConfigOauth2 {
pub client_id: String, pub client_id: String,
pub client_secret: String, pub client_secret: String,
pub refresh_token: String, pub refresh_token: String,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct YoutubeConfig { pub struct YoutubeConfig {
pub refresh_token: String, pub refresh_token: String,
pub client_id: String, pub client_id: String,
pub client_secret: String, pub client_secret: String,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct TootubeConfig { pub struct TootubeConfig {
pub progress_bar: String, pub progress_bar: String,
pub progress_chars: String, pub progress_chars: String,
@@ -57,13 +60,22 @@ impl Default for TootubeConfig {
} }
} }
/// Parses the TOML file into a Config struct impl Config {
pub fn parse_toml(toml_file: &str) -> Config { /// Parses the TOML file into a Config struct
let toml_config = pub fn new(toml_file: &str) -> Self {
read_to_string(toml_file).unwrap_or_else(|e| panic!("Cannot open file {toml_file}: {e}")); let toml_config = read_to_string(toml_file)
.unwrap_or_else(|e| panic!("Cannot open file {toml_file}: {e}"));
let config: Config = toml::from_str(&toml_config) let config: Config = toml::from_str(&toml_config)
.unwrap_or_else(|e| panic!("Cannot parse TOML file {toml_file}: {e}")); .unwrap_or_else(|e| panic!("Cannot parse TOML file {toml_file}: {e}"));
config config
}
pub fn dump(&mut self, toml_file: &str) -> Result<(), Box<dyn Error>> {
// bring back the default for progress bar
self.tootube = TootubeConfig::default();
write(toml_file, toml::to_string_pretty(self)?)?;
Ok(())
}
} }

View File

@@ -3,8 +3,8 @@ use log::debug;
mod error; mod error;
mod config; mod config;
pub use config::parse_toml; pub use config::Config;
use config::Config; pub use config::PeertubeConfigOauth2;
mod peertube; mod peertube;
pub use peertube::register as register_peertube; pub use peertube::register as register_peertube;
@@ -14,20 +14,30 @@ mod youtube;
pub use youtube::register as register_youtube; pub use youtube::register as register_youtube;
use youtube::YouTube; use youtube::YouTube;
/// This is where the magic happens
/// This takes the main options from config & command line args
/// and sends back the updated PeerTube refresh_token if any
#[tokio::main] #[tokio::main]
pub async fn run(config: Config, pl: Vec<String>) { pub async fn run(config: &Config, pl: Vec<String>, pt_video_id: Option<&str>) -> Option<String> {
// Create PeerTube struct // Create PeerTube struct
let peertube = match &config.peertube.oauth2 { let peertube = match &config.peertube.oauth2 {
Some(s) => PeerTube::new(&config.peertube.base_url) Some(s) => PeerTube::new(&config.peertube.base_url)
.with_client(&s.client_id, &s.client_secret, &s.refresh_token) .with_client(s)
.await .await
.unwrap_or_else(|e| panic!("Cannot instantiate PeerTube struct: {}", e)), .unwrap_or_else(|e| panic!("Cannot instantiate PeerTube struct: {}", e)),
None => PeerTube::new(&config.peertube.base_url), None => PeerTube::new(&config.peertube.base_url),
}; };
// Get the latest video object // Get the latest video object or the targeted pt_video_id
let latest_vid = peertube.get_latest_video().await.unwrap_or_else(|e| { let latest_vid = match pt_video_id {
panic!("Cannot retrieve the latest video, something must have gone terribly wrong: {e}") None => peertube.get_latest_video().await.unwrap_or_else(|e| {
}); panic!("Cannot retrieve the latest video, something must have gone terribly wrong: {e}")
}),
Some(v) => peertube.get_video_detail(v).await.unwrap_or_else(|e| {
panic!(
"Cannot retrieve the specified video, something must have gone terribly wrong: {e}"
)
}),
};
// We have a refresh_token, try to use it // We have a refresh_token, try to use it
let source_url = match &config.peertube.oauth2 { let source_url = match &config.peertube.oauth2 {
@@ -75,6 +85,14 @@ pub async fn run(config: Config, pl: Vec<String>) {
.unwrap_or_else(|e| panic!("Cannot resume upload!: {e}")); .unwrap_or_else(|e| panic!("Cannot resume upload!: {e}"));
debug!("YT video ID: {}", &yt_video_id); debug!("YT video ID: {}", &yt_video_id);
youtube
.set_thumbnail(
&yt_video_id,
&format!("{}{}", &config.peertube.base_url, latest_vid.preview_path),
)
.await
.unwrap_or_else(|e| panic!("Cannot upload the thumbnail: {}", e));
if !pl.is_empty() { if !pl.is_empty() {
youtube youtube
.add_video_to_playlists(&yt_video_id, &pl) .add_video_to_playlists(&yt_video_id, &pl)
@@ -105,4 +123,6 @@ pub async fn run(config: Config, pl: Vec<String>) {
} }
} }
} }
peertube.refresh_token
} }

View File

@@ -27,12 +27,21 @@ fn main() {
.num_args(0..) .num_args(0..)
.display_order(2), .display_order(2),
) )
.arg(
Arg::new("id")
.short('i')
.long("id")
.value_name("ID")
.help("Specify the PeerTube Video ID")
.num_args(0..=1)
.display_order(3),
)
.arg( .arg(
Arg::new("vice") Arg::new("vice")
.long("vice") .long("vice")
.aliases(["chybrare", "coquinou"]) .aliases(["chybrare", "coquinou"])
.action(clap::ArgAction::SetTrue) .action(clap::ArgAction::SetTrue)
.display_order(3), .display_order(4),
) )
.subcommand( .subcommand(
Command::new("register") Command::new("register")
@@ -57,22 +66,30 @@ fn main() {
.get_matches(); .get_matches();
if let Some(("register", sub_m)) = matches.subcommand() { if let Some(("register", sub_m)) = matches.subcommand() {
let config = parse_toml(sub_m.get_one::<String>("config").unwrap()); let mut config = Config::new(sub_m.get_one::<String>("config").unwrap());
if sub_m.get_flag("youtube") { if sub_m.get_flag("youtube") {
register_youtube(&config.youtube) let yt_refresh_token = register_youtube(&config.youtube)
.unwrap_or_else(|e| panic!("Cannot register to YouTube API: {}", e)); .unwrap_or_else(|e| panic!("Cannot register to YouTube API: {}", e));
config.youtube.refresh_token = yt_refresh_token;
} }
if sub_m.get_flag("peertube") { if sub_m.get_flag("peertube") {
register_peertube(&config.peertube) let pt_oauth2 = register_peertube(&config.peertube)
.unwrap_or_else(|e| panic!("Cannot register to PeerTube API: {}", e)); .unwrap_or_else(|e| panic!("Cannot register to PeerTube API: {}", e));
config.peertube.oauth2 = Some(pt_oauth2);
} }
config
.dump(sub_m.get_one::<String>("config").unwrap())
.unwrap_or_else(|e| panic!("Cannot write back to Tootube Config file: {}", e));
return; return;
} }
let mut config = parse_toml(matches.get_one::<String>("config").unwrap()); let mut config = Config::new(matches.get_one::<String>("config").unwrap());
if matches.get_flag("vice") { 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_bar = "{msg}\n[{elapsed_precise}] 8{wide_bar:.magenta}ᗺ {bytes}/{total_bytes} ({bytes_per_sec}, {eta})".to_string();
@@ -85,7 +102,16 @@ fn main() {
.map(|v| v.to_string()) .map(|v| v.to_string())
.collect(); .collect();
let pt_video_id = matches.get_one::<String>("id").map(|s| s.as_str());
env_logger::init(); env_logger::init();
run(config, playlists); // runs the main program logic & retrieves the updated PeerTube refresh_token
if let Some(x) = run(&config, playlists, pt_video_id) {
config.peertube.oauth2 = config.peertube.oauth2.map(|y| PeertubeConfigOauth2 {
client_id: y.client_id,
client_secret: y.client_secret,
refresh_token: x,
})
};
} }

View File

@@ -1,4 +1,4 @@
use crate::{config::PeertubeConfig, error::TootubeError}; use crate::{config::PeertubeConfig, config::PeertubeConfigOauth2, error::TootubeError};
use log::debug; use log::debug;
use reqwest::{ use reqwest::{
header::{HeaderMap, HeaderValue}, header::{HeaderMap, HeaderValue},
@@ -8,11 +8,9 @@ use reqwest::{
use rpassword::prompt_password; use rpassword::prompt_password;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{boxed::Box, cmp::Ordering, error::Error, io::stdin}; use std::{boxed::Box, cmp::Ordering, error::Error, io::stdin};
use tokio::fs::{read_to_string, write};
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct PeerTubeVideos { pub struct PeerTubeVideos {
pub total: u64,
pub data: Vec<PeerTubeVideo>, pub data: Vec<PeerTubeVideo>,
} }
@@ -21,6 +19,8 @@ pub struct PeerTubeVideo {
pub name: String, pub name: String,
pub uuid: String, pub uuid: String,
pub description: String, pub description: String,
#[serde(rename = "previewPath")]
pub preview_path: String,
#[serde(rename = "streamingPlaylists")] #[serde(rename = "streamingPlaylists")]
pub streaming_playlists: Option<Vec<PeerTubeVideoStreamingPlaylists>>, pub streaming_playlists: Option<Vec<PeerTubeVideoStreamingPlaylists>>,
pub tags: Option<Vec<String>>, pub tags: Option<Vec<String>>,
@@ -77,7 +77,6 @@ struct PeerTubeVideoTokenResponseFiles {
#[derive(Eq, Debug, Deserialize)] #[derive(Eq, Debug, Deserialize)]
pub struct PeerTubeVideoStreamingPlaylistsFiles { pub struct PeerTubeVideoStreamingPlaylistsFiles {
pub id: u64,
pub resolution: PeerTubeVideoStreamingPlaylistsFilesResolution, pub resolution: PeerTubeVideoStreamingPlaylistsFilesResolution,
#[serde(rename = "fileDownloadUrl")] #[serde(rename = "fileDownloadUrl")]
pub file_download_url: String, pub file_download_url: String,
@@ -114,7 +113,6 @@ pub struct PeerTubeVideoPlaylists {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct PeerTubeVideoPlaylist { pub struct PeerTubeVideoPlaylist {
pub id: u16,
pub uuid: String, pub uuid: String,
#[serde(rename = "displayName")] #[serde(rename = "displayName")]
pub display_name: String, pub display_name: String,
@@ -150,7 +148,7 @@ struct PeerTubeVideoPlaylistsPlaylistIdVideosData {
/// This function makes the registration process a little bit easier /// This function makes the registration process a little bit easier
#[tokio::main] #[tokio::main]
pub async fn register(config: &PeertubeConfig) -> Result<(), Box<dyn Error>> { pub async fn register(config: &PeertubeConfig) -> Result<PeertubeConfigOauth2, Box<dyn Error>> {
// Get client ID/secret // Get client ID/secret
let oauth2_client = reqwest::get(format!("{}/api/v1/oauth-clients/local", config.base_url)) let oauth2_client = reqwest::get(format!("{}/api/v1/oauth-clients/local", config.base_url))
.await? .await?
@@ -186,26 +184,25 @@ pub async fn register(config: &PeertubeConfig) -> Result<(), Box<dyn Error>> {
.json::<PeerTubeUsersTokenResponse>() .json::<PeerTubeUsersTokenResponse>()
.await?; .await?;
println!("You can now paste the following lines inside the `peertube` section of your tootube.toml file:"); println!(
"The following lines will be written to the `peertube` section of your tootube.toml file:"
println!(); );
println!("[peertube.oauth2]"); println!("[peertube.oauth2]");
println!("client_id=\"{}\"", oauth2_client.client_id); println!("client_id=\"{}\"", oauth2_client.client_id);
println!("client_secret=\"{}\"", oauth2_client.client_secret); println!("client_secret=\"{}\"", oauth2_client.client_secret);
println!("refresh_token=<path to refresh_token>"); println!("refresh_token=\"{}\"", oauth2_token.refresh_token);
println!(); Ok(PeertubeConfigOauth2 {
client_id: oauth2_client.client_id,
println!("Finally, add the refresh token inside the refresh_token path:"); client_secret: oauth2_client.client_secret,
println!("{}", oauth2_token.refresh_token); refresh_token: oauth2_token.refresh_token,
})
Ok(())
} }
#[derive(Debug)] #[derive(Debug)]
pub struct PeerTube { pub struct PeerTube {
base_url: String, base_url: String,
pub refresh_token: Option<String>,
client: Client, client: Client,
} }
@@ -214,6 +211,7 @@ impl PeerTube {
pub fn new(base_url: &str) -> Self { pub fn new(base_url: &str) -> Self {
PeerTube { PeerTube {
base_url: format!("{}/api/v1", base_url), base_url: format!("{}/api/v1", base_url),
refresh_token: None,
client: Client::new(), client: Client::new(),
} }
} }
@@ -222,32 +220,26 @@ impl PeerTube {
/// the default required header /// the default required header
pub async fn with_client( pub async fn with_client(
mut self, mut self,
client_id: &str, pt_oauth2: &PeertubeConfigOauth2,
client_secret: &str,
refresh_token_path: &str,
) -> Result<Self, Box<dyn Error>> { ) -> Result<Self, Box<dyn Error>> {
let refresh_token = read_to_string(refresh_token_path).await?;
let params = PeerTubeUsersToken { let params = PeerTubeUsersToken {
client_id: client_id.to_string(), client_id: pt_oauth2.client_id.to_owned(),
client_secret: client_secret.to_string(), client_secret: pt_oauth2.client_secret.to_owned(),
grant_type: "refresh_token".to_string(), grant_type: "refresh_token".to_string(),
refresh_token: Some(refresh_token), refresh_token: Some(pt_oauth2.refresh_token.to_owned()),
username: None, username: None,
password: None, password: None,
}; };
let req = self let req = self
.client .client
.post(&format!("{}/users/token", self.base_url)) .post(format!("{}/users/token", self.base_url))
.form(&params) .form(&params)
.send() .send()
.await? .await?
.json::<PeerTubeUsersTokenResponse>() .json::<PeerTubeUsersTokenResponse>()
.await?; .await?;
write(refresh_token_path, req.refresh_token).await?;
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert( headers.insert(
"Authorization", "Authorization",
@@ -258,6 +250,8 @@ impl PeerTube {
.default_headers(headers) .default_headers(headers)
.build()?; .build()?;
self.refresh_token = Some(req.refresh_token);
Ok(self) Ok(self)
} }
@@ -280,7 +274,7 @@ impl PeerTube {
} }
/// This gets all the crispy details about one particular video /// This gets all the crispy details about one particular video
async fn get_video_detail(&self, v: &str) -> Result<PeerTubeVideo, Box<dyn Error>> { pub async fn get_video_detail(&self, v: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
let body = self let body = self
.client .client
.get(format!("{}/videos/{}", self.base_url, v)) .get(format!("{}/videos/{}", self.base_url, v))
@@ -349,7 +343,7 @@ impl PeerTube {
while let Ok(mut local_pl) = self while let Ok(mut local_pl) = self
.client .client
.get(&format!( .get(format!(
"{}/video-playlists?count={}&playlistType=1&start={}", "{}/video-playlists?count={}&playlistType=1&start={}",
self.base_url, inc, start self.base_url, inc, start
)) ))
@@ -381,7 +375,7 @@ impl PeerTube {
let pl_created = self let pl_created = self
.client .client
.post(&format!("{}/video-playlists", self.base_url)) .post(format!("{}/video-playlists", self.base_url))
.multipart(form) .multipart(form)
.send() .send()
.await? .await?
@@ -403,7 +397,7 @@ impl PeerTube {
let res = self let res = self
.client .client
.post(&format!( .post(format!(
"{}/video-playlists/{}/videos", "{}/video-playlists/{}/videos",
self.base_url, pl_uuid self.base_url, pl_uuid
)) ))
@@ -433,7 +427,7 @@ impl PeerTube {
while let Ok(l_vid) = self while let Ok(l_vid) = self
.client .client
.get(&format!( .get(format!(
"{}/video-playlists/{}/videos?start={}&count={}", "{}/video-playlists/{}/videos?start={}&count={}",
&self.base_url, &uuid, start, inc &self.base_url, &uuid, start, inc
)) ))
@@ -503,3 +497,49 @@ pub async fn get_playlists_to_be_added_to(
Ok(playlist_to_be_added_to) Ok(playlist_to_be_added_to)
} }
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_get_latest_video() {
let peertube = PeerTube::new("https://peertube.cpy.re");
let vid = peertube.get_latest_video().await.unwrap();
assert_eq!(vid.uuid.len(), 36);
}
#[tokio::test]
#[should_panic]
async fn test_get_original_video_source() {
let peertube = PeerTube::new("https://peertube.cpy.re");
let _vid = peertube
.get_original_video_source("t")
.await
.expect("Should panic!");
}
#[tokio::test]
async fn test_list_video_playlists() {
let peertube = PeerTube::new("https://peertube.cpy.re");
let pl = peertube.list_video_playlists().await.unwrap();
assert!(!pl.is_empty());
}
#[tokio::test]
async fn test_list_videos_playlist() {
let peertube = PeerTube::new("https://peertube.cpy.re");
let pl_videos = peertube
.list_videos_playlist("73a5c1fa-64c5-462d-81e5-b120781c2d72")
.await
.unwrap();
assert!(!pl_videos.is_empty());
}
}

View File

@@ -160,8 +160,9 @@ struct YoutubePlaylistListResponseItemSnippet {
} }
/// This function makes the registration process a little bit easier /// This function makes the registration process a little bit easier
/// It returns the expected refresh_token so that it can be written back to the file
#[tokio::main] #[tokio::main]
pub async fn register(config: &YoutubeConfig) -> Result<(), Box<dyn Error>> { pub async fn register(config: &YoutubeConfig) -> Result<String, 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!("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!("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:"); println!("Paste the returned authorization code:");
@@ -187,11 +188,12 @@ pub async fn register(config: &YoutubeConfig) -> Result<(), Box<dyn Error>> {
let refresh_token: RegistrationAccessTokenResponse = res.json().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!(
"The following line will be written to the `youtube` section of your tootube.toml file:"
);
println!("refresh_token=\"{}\"", refresh_token.refresh_token); println!("refresh_token=\"{}\"", refresh_token.refresh_token);
Ok(()) Ok(refresh_token.refresh_token)
} }
pub struct YouTube { pub struct YouTube {
@@ -236,13 +238,39 @@ impl YouTube {
Ok(youtube) Ok(youtube)
} }
/// This function uploads the thumbnail from PeerTube
pub async fn set_thumbnail(
&self,
video_id: &str,
preview_url: &str,
) -> Result<(), Box<dyn Error>> {
let res = reqwest::get(preview_url).await?;
let stream = res.bytes_stream();
let res = self
.client
.post(format!(
"https://www.googleapis.com/upload/youtube/v3/thumbnails/set?videoId={}&uploadType=media",
video_id
))
.header("Content-Type", "application/octet-stream")
.body(Body::wrap_stream(stream))
.send()
.await?;
res.status().is_success().then_some(()).ok_or(
TootubeError::new(&format!("Thumbnail not uploaded: {}", res.text().await?)).into(),
)
}
/// This function takes a list of playlists keyword and returns a list of playlist ID /// 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>> { async fn get_playlist_ids(&self, pl: &[String]) -> Result<Vec<String>, Box<dyn Error>> {
let mut page_token = String::new(); let mut page_token = String::new();
let mut playlists: Vec<String> = vec![]; let mut playlists: Vec<String> = vec![];
while let Ok(local_pl) = self.client while let Ok(local_pl) = self.client
.get(&format!( .get(format!(
"https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true&pageToken={}", "https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true&pageToken={}",
page_token page_token
)) ))
@@ -341,17 +369,18 @@ impl YouTube {
.json(&upload_params) .json(&upload_params)
.send().await?; .send().await?;
if res.status().is_success() { res.status()
Ok(res .is_success()
.headers() .then_some(
.get("location") res.headers()
.ok_or("Cannot find suitable header")? .get("location")
.to_str()? .ok_or("Cannot find suitable header")?
.to_string()) .to_str()?
} else { .to_string(),
Err(TootubeError::new("Cannot create resumable upload!").into()) )
} .ok_or(TootubeError::new("Cannot create resumable upload!").into())
} }
/// This takes the PT stream for download, connects it to YT stream for upload /// This takes the PT stream for download, connects it to YT stream for upload
pub async fn now_kiss( pub async fn now_kiss(
&self, &self,