mirror of
https://framagit.org/veretcle/tootube.git
synced 2025-07-21 13:24:19 +02:00
Compare commits
40 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d70fbdac98 | ||
![]() |
982dc8b954 | ||
![]() |
bf622d6989 | ||
![]() |
a687f008df | ||
![]() |
e2ab93c04e | ||
![]() |
9e1c27be29 | ||
![]() |
ab9ea1a8ee | ||
![]() |
66f288c069 | ||
![]() |
b208daa0ea | ||
![]() |
f3ad81716d | ||
![]() |
f18ce899aa | ||
![]() |
bf0b1c4e3f | ||
![]() |
eb398a880a | ||
![]() |
1db91ec3dc | ||
![]() |
1d4ae8f152 | ||
![]() |
58f36a56f7 | ||
![]() |
e17ba6cf7e | ||
![]() |
3d53ad7bbd | ||
![]() |
3f482ff258 | ||
![]() |
ae3fa74d9b | ||
![]() |
62641f526f | ||
![]() |
d83985394c | ||
![]() |
ebc42f915d | ||
![]() |
8d4d683f55 | ||
![]() |
2b508e90c1 | ||
![]() |
3482456adb | ||
![]() |
568ca00d98 | ||
![]() |
7fda7069a9 | ||
![]() |
3d9ebcc2a5 | ||
![]() |
9ca27a262f | ||
![]() |
d39c78c197 | ||
![]() |
6867882ca0 | ||
![]() |
a00753b9eb | ||
![]() |
8a9a365a04 | ||
![]() |
4a47eb1d08 | ||
![]() |
1bdd747202 | ||
![]() |
0640c4c263 | ||
![]() |
59ad8994b1 | ||
![]() |
f1d05e9785 | ||
![]() |
0451543d6d |
851
Cargo.lock
generated
851
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
11
Cargo.toml
@@ -1,18 +1,23 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "tootube"
|
name = "tootube"
|
||||||
authors = ["VC <veretcle+framagit@mateu.be>"]
|
authors = ["VC <veretcle+framagit@mateu.be>"]
|
||||||
version = "0.2.0"
|
version = "0.6.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
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
reqwest = { version = "^0.11", features = ["blocking", "json"] }
|
rpassword = "^7.3"
|
||||||
|
reqwest = { version = "^0.11", features = ["json", "stream", "multipart"] }
|
||||||
|
tokio = { version = "^1", features = ["full"] }
|
||||||
clap = "^4"
|
clap = "^4"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "^1", features = ["derive"] }
|
||||||
toml = "^0.5"
|
toml = "^0.5"
|
||||||
log = "^0.4"
|
log = "^0.4"
|
||||||
env_logger = "^0.10"
|
env_logger = "^0.10"
|
||||||
|
indicatif = "^0.17"
|
||||||
|
async-stream = "^0.3"
|
||||||
|
futures-util = "^0.3"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
strip = true
|
strip = true
|
||||||
|
75
README.md
75
README.md
@@ -6,50 +6,79 @@ This program takes the very last video published on a PeerTube instance and push
|
|||||||
|
|
||||||
This is an early prototype not really suited for production purposes for now:
|
This is an early prototype not really suited for production purposes for now:
|
||||||
* it still relies way too much on pre-determined value to upload the video to YouTube
|
* it still relies way too much on pre-determined value to upload the video to YouTube
|
||||||
* it cannot determine the playlists it needs to put them in
|
|
||||||
* it cannot determine the recording date (believe me, I tried!)
|
* it cannot determine the recording date (believe me, I tried!)
|
||||||
* there are still a lot of static values that I would rather not have static (like the cache directory…)
|
|
||||||
* it is 100% sync, meaning it’s clearly not optimal for now and probably won’t be for the next releases
|
|
||||||
|
|
||||||
So consider this a work in progress that will slowly get better with time
|
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 downloads the latest PeerTube video from an instance
|
* it retrieves the latest PeerTube video download URL from an instance
|
||||||
* stores it in cache directory
|
* if you register the app into your PeerTube instance (optional), you can retrieve the latest video source instead of the highest quality encoded video
|
||||||
* uploads it to YouTube
|
* 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)
|
||||||
|
|
||||||
# What doesn’t it do exactly?
|
# What doesn’t it do exactly?
|
||||||
|
|
||||||
* it cannot register the original key (see below)
|
|
||||||
* it relies on a local cache despite the fact that it might well be possible to just download from PT/upload to YT at the same time (I don’t see why not in fact)
|
|
||||||
* it doesn’t retrieve ALL the original PeerTube video properties like licences, languages, categories, etc… again: early prototype
|
* it doesn’t retrieve ALL the original PeerTube video properties like licences, languages, categories, etc… again: early prototype
|
||||||
|
|
||||||
# Obtain Authorization Token from Google
|
# Howtos
|
||||||
|
## General usage
|
||||||
|
|
||||||
That the complicated part:
|
Once you fill your `tootube.toml` config file (see below), you can run `tootube` like this:
|
||||||
* create an OAuth2.0 application with authorization for Youtube DATA Api v3 Upload
|
|
||||||
|
```bash
|
||||||
|
tootube --config tootube.toml --playlist playlist_1 "Things You Might Like"
|
||||||
|
```
|
||||||
|
|
||||||
|
You can turn on debug using env var `RUST_LOG`.
|
||||||
|
|
||||||
|
## Obtain Authorization Token from Google
|
||||||
|
|
||||||
|
The complicated part:
|
||||||
|
* create an OAuth2.0 application with authorization for Youtube DATA Api v3 Upload and Youtube DATA Api v3 (generally referenced as `../auth/youtube.upload` and `../auth/youtube`)
|
||||||
* create a OAuth2.0 client with Desktop client
|
* create a OAuth2.0 client with Desktop client
|
||||||
|
|
||||||
You’ll need:
|
You’ll need:
|
||||||
* the `client_id` from your OAuth2.0 client
|
* the `client_id` from your OAuth2.0 client
|
||||||
* the `client_secret` from you OAuth2.0 client
|
* the `client_secret` from you OAuth2.0 client
|
||||||
|
|
||||||
Then enter in:
|
Create your `tootube.toml` config file:
|
||||||
|
|
||||||
```
|
```toml
|
||||||
https://accounts.google.com/o/oauth2/v2/auth?client_id=XXX.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/youtube.upload&response_type=code
|
[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
|
||||||
|
client_id="<YOUR CLIENT_ID>"
|
||||||
|
client_secret="<YOUR CLIENT_SECRET>"
|
||||||
```
|
```
|
||||||
|
|
||||||
And accept that your YouTube account might be modified. You’ll get a code then; enter this `curl` post:
|
Then run:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
curl -s \
|
tootube register --youtube --config <PATH TO YOUR TOOTUBE.TOML FILE>
|
||||||
--request POST \
|
|
||||||
--data "code=[THE_CODE]&client_id=XXX.apps.googleusercontent.com&client_secret=[THE_CLIENT_SECRET]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
|
|
||||||
https://accounts.google.com/o/oauth2/token
|
|
||||||
```
|
```
|
||||||
|
|
||||||
You’ll get a Token. The only important part is the `refresh_token`
|
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.
|
||||||
|
|
||||||
In your `tootube.toml` config file, put the `refresh_token`.
|
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
|
||||||
|
```
|
||||||
|
@@ -1,17 +1,38 @@
|
|||||||
use std::fs::read_to_string;
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
// General configuration Struct
|
// General configuration Struct
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub peertube: PeertubeConfig,
|
pub peertube: PeertubeConfig,
|
||||||
pub youtube: YoutubeConfig,
|
pub youtube: YoutubeConfig,
|
||||||
|
#[serde(default)]
|
||||||
|
pub tootube: TootubeConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct PeertubeConfig {
|
pub struct PeertubeConfig {
|
||||||
pub base_url: String,
|
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)]
|
#[derive(Debug, Deserialize)]
|
||||||
@@ -21,6 +42,21 @@ pub struct YoutubeConfig {
|
|||||||
pub client_secret: String,
|
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
|
/// Parses the TOML file into a Config struct
|
||||||
pub fn parse_toml(toml_file: &str) -> Config {
|
pub fn parse_toml(toml_file: &str) -> Config {
|
||||||
let toml_config =
|
let toml_config =
|
||||||
|
114
src/lib.rs
114
src/lib.rs
@@ -1,74 +1,80 @@
|
|||||||
use std::{
|
use log::debug;
|
||||||
error::Error,
|
|
||||||
fs::create_dir_all,
|
|
||||||
fs::{remove_file, File},
|
|
||||||
};
|
|
||||||
|
|
||||||
use reqwest::Url;
|
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
use error::TootubeError;
|
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
pub use config::parse_toml;
|
pub use config::parse_toml;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
|
||||||
mod peertube;
|
mod peertube;
|
||||||
use peertube::{get_latest_video, get_max_resolution_dl};
|
pub use peertube::register as register_peertube;
|
||||||
|
use peertube::{delete_original_video_source, get_latest_video, get_original_video_source};
|
||||||
|
|
||||||
mod youtube;
|
mod youtube;
|
||||||
use youtube::{create_resumable_upload, upload_video};
|
pub use youtube::register as register_youtube;
|
||||||
|
use youtube::{add_video_to_playlists, create_resumable_upload, now_kiss};
|
||||||
|
|
||||||
const TMP_DIR: &str = "/tmp/tootube";
|
#[tokio::main]
|
||||||
|
pub async fn run(config: Config, pl: Vec<String>) {
|
||||||
fn dl_video(u: &str) -> Result<String, Box<dyn Error>> {
|
|
||||||
// create dir
|
|
||||||
create_dir_all(TMP_DIR)?;
|
|
||||||
|
|
||||||
// get file
|
|
||||||
let mut response = reqwest::blocking::get(u)?;
|
|
||||||
|
|
||||||
// create local file
|
|
||||||
let url = Url::parse(u)?;
|
|
||||||
let dest_filename = url
|
|
||||||
.path_segments()
|
|
||||||
.ok_or_else(|| {
|
|
||||||
TootubeError::new(&format!(
|
|
||||||
"Cannot determine the destination filename for {u}"
|
|
||||||
))
|
|
||||||
})?
|
|
||||||
.last()
|
|
||||||
.ok_or_else(|| {
|
|
||||||
TootubeError::new(&format!(
|
|
||||||
"Cannot determine the destination filename for {u}"
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let dest_filepath = format!("{TMP_DIR}/{dest_filename}");
|
|
||||||
|
|
||||||
let mut dest_file = File::create(&dest_filepath)?;
|
|
||||||
|
|
||||||
response.copy_to(&mut dest_file)?;
|
|
||||||
|
|
||||||
Ok(dest_filepath)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(config: Config) {
|
|
||||||
// Get the latest video object
|
// Get the latest video object
|
||||||
let latest_vid = get_latest_video(&config.peertube.base_url).unwrap_or_else(|e| {
|
let latest_vid = get_latest_video(&config.peertube.base_url)
|
||||||
panic!("Cannot retrieve the latest video, something must have gone terribly wrong: {e}")
|
.await
|
||||||
});
|
.unwrap_or_else(|e| {
|
||||||
|
panic!("Cannot retrieve the latest video, something must have gone terribly wrong: {e}")
|
||||||
|
});
|
||||||
|
|
||||||
let dl_url = get_max_resolution_dl(latest_vid.streaming_playlists.as_ref().unwrap());
|
// 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,
|
||||||
|
};
|
||||||
|
|
||||||
let local_path = dl_video(&dl_url)
|
// Whatever happens, collect the highest quality possible
|
||||||
.unwrap_or_else(|e| panic!("Cannot download video at URL {}: {}", dl_url, e));
|
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 resumable_upload_url = create_resumable_upload(&config.youtube, &latest_vid)
|
||||||
|
.await
|
||||||
.unwrap_or_else(|e| panic!("Cannot retrieve the upload’s resumable id: {e}"));
|
.unwrap_or_else(|e| panic!("Cannot retrieve the upload’s resumable id: {e}"));
|
||||||
|
debug!("YT upload URL: {}", &resumable_upload_url);
|
||||||
|
|
||||||
upload_video(&local_path, &resumable_upload_url, &config.youtube)
|
let yt_video_id = now_kiss(
|
||||||
.unwrap_or_else(|e| panic!("Cannot resume upload!: {e}"));
|
&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);
|
||||||
|
|
||||||
remove_file(&local_path).unwrap_or_else(|e| panic!("Cannot delete file {}: {}", local_path, e));
|
if !pl.is_empty() {
|
||||||
|
add_video_to_playlists(&config.youtube, &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 {
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
68
src/main.rs
68
src/main.rs
@@ -10,6 +10,7 @@ fn main() {
|
|||||||
.arg(
|
.arg(
|
||||||
Arg::new("config")
|
Arg::new("config")
|
||||||
.short('c')
|
.short('c')
|
||||||
|
.global(true)
|
||||||
.long("config")
|
.long("config")
|
||||||
.value_name("CONFIG_FILE")
|
.value_name("CONFIG_FILE")
|
||||||
.help("TOML config file for tootube")
|
.help("TOML config file for tootube")
|
||||||
@@ -17,11 +18,74 @@ fn main() {
|
|||||||
.default_value(DEFAULT_CONFIG_PATH)
|
.default_value(DEFAULT_CONFIG_PATH)
|
||||||
.display_order(1),
|
.display_order(1),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("playlists")
|
||||||
|
.short('p')
|
||||||
|
.long("playlist")
|
||||||
|
.value_name("PLAYLIST")
|
||||||
|
.help("List of playlists to add the video to")
|
||||||
|
.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 or PeerTube OAuth2.0")
|
||||||
|
.arg(
|
||||||
|
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();
|
.get_matches();
|
||||||
|
|
||||||
let config = parse_toml(matches.get_one::<String>("config").unwrap());
|
if let Some(("register", sub_m)) = matches.subcommand() {
|
||||||
|
let config = parse_toml(sub_m.get_one::<String>("config").unwrap());
|
||||||
|
|
||||||
|
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 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")
|
||||||
|
.unwrap_or_default()
|
||||||
|
.map(|v| v.to_string())
|
||||||
|
.collect();
|
||||||
|
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
run(config);
|
run(config, playlists);
|
||||||
}
|
}
|
||||||
|
283
src/peertube.rs
283
src/peertube.rs
@@ -1,5 +1,10 @@
|
|||||||
use serde::Deserialize;
|
use crate::{config::PeertubeConfig, error::TootubeError};
|
||||||
use std::{boxed::Box, error::Error};
|
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)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct PeerTubeVideos {
|
pub struct PeerTubeVideos {
|
||||||
@@ -23,6 +28,44 @@ pub struct PeerTubeVideoStreamingPlaylists {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[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 struct PeerTubeVideoStreamingPlaylistsFiles {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub resolution: PeerTubeVideoStreamingPlaylistsFilesResolution,
|
pub resolution: PeerTubeVideoStreamingPlaylistsFilesResolution,
|
||||||
@@ -30,71 +73,207 @@ pub struct PeerTubeVideoStreamingPlaylistsFiles {
|
|||||||
pub file_download_url: String,
|
pub file_download_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
impl Ord for PeerTubeVideoStreamingPlaylistsFiles {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
self.resolution.id.cmp(&other.resolution.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for PeerTubeVideoStreamingPlaylistsFiles {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for PeerTubeVideoStreamingPlaylistsFiles {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.resolution.id == other.resolution.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Eq, Debug, Deserialize, PartialEq)]
|
||||||
pub struct PeerTubeVideoStreamingPlaylistsFilesResolution {
|
pub struct PeerTubeVideoStreamingPlaylistsFilesResolution {
|
||||||
pub id: u16,
|
pub id: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This gets the last video uploaded to the PeerTube server
|
/// This gets the last video uploaded to the PeerTube server
|
||||||
pub fn get_latest_video(u: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
pub async fn get_latest_video(u: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
||||||
let body = reqwest::blocking::get(format!("{}/api/v1/videos?count=1&sort=-publishedAt", u))?
|
let body = reqwest::get(format!("{}/api/v1/videos?count=1&sort=-publishedAt", u))
|
||||||
.json::<PeerTubeVideos>()?;
|
.await?
|
||||||
|
.json::<PeerTubeVideos>()
|
||||||
|
.await?;
|
||||||
|
|
||||||
let vid = get_video_detail(u, &body.data[0].uuid)?;
|
let vid = get_video_detail(u, &body.data[0].uuid).await?;
|
||||||
|
|
||||||
Ok(vid)
|
Ok(vid)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This returns the direct download URL for with the maximum resolution
|
|
||||||
pub fn get_max_resolution_dl(p: &[PeerTubeVideoStreamingPlaylists]) -> String {
|
|
||||||
let mut res = 0;
|
|
||||||
let mut dl_url = String::new();
|
|
||||||
|
|
||||||
for i in p[0].files.iter() {
|
|
||||||
if i.resolution.id > res {
|
|
||||||
res = i.resolution.id;
|
|
||||||
dl_url = i.file_download_url.clone();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dl_url
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This gets all the crispy details about one particular video
|
/// This gets all the crispy details about one particular video
|
||||||
fn get_video_detail(u: &str, v: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
async fn get_video_detail(u: &str, v: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
||||||
let body =
|
let body = reqwest::get(format!("{}/api/v1/videos/{}", u, v))
|
||||||
reqwest::blocking::get(format!("{}/api/v1/videos/{}", u, v))?.json::<PeerTubeVideo>()?;
|
.await?
|
||||||
|
.json::<PeerTubeVideo>()
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(body)
|
Ok(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
/// This function makes the registration process a little bit easier
|
||||||
mod tests {
|
#[tokio::main]
|
||||||
use super::*;
|
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?;
|
||||||
|
|
||||||
#[test]
|
println!(
|
||||||
fn test_get_max_resolution_dl() {
|
"Please type your PeerTube username for instance {}:",
|
||||||
let str_plist: Vec<PeerTubeVideoStreamingPlaylists> =
|
config.base_url
|
||||||
vec![PeerTubeVideoStreamingPlaylists {
|
);
|
||||||
files: vec![
|
let mut username = String::new();
|
||||||
PeerTubeVideoStreamingPlaylistsFiles {
|
stdin()
|
||||||
id: 1025,
|
.read_line(&mut username)
|
||||||
resolution: PeerTubeVideoStreamingPlaylistsFilesResolution { id: 990 },
|
.expect("Unable to read back the username!");
|
||||||
file_download_url: "meh!".to_string(),
|
|
||||||
},
|
|
||||||
PeerTubeVideoStreamingPlaylistsFiles {
|
|
||||||
id: 1027,
|
|
||||||
resolution: PeerTubeVideoStreamingPlaylistsFilesResolution { id: 1720 },
|
|
||||||
file_download_url: "blah".to_string(),
|
|
||||||
},
|
|
||||||
PeerTubeVideoStreamingPlaylistsFiles {
|
|
||||||
id: 1026,
|
|
||||||
resolution: PeerTubeVideoStreamingPlaylistsFilesResolution { id: 360 },
|
|
||||||
file_download_url: "nope".to_string(),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}];
|
|
||||||
|
|
||||||
assert_eq!("blah".to_string(), get_max_resolution_dl(&str_plist));
|
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(())
|
||||||
}
|
}
|
||||||
|
297
src/youtube.rs
297
src/youtube.rs
@@ -1,8 +1,18 @@
|
|||||||
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, warn};
|
||||||
|
use reqwest::{multipart::Form, Body, Client};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{error::Error, fs::File, sync::Mutex};
|
use std::{cmp::min, error::Error, io::stdin};
|
||||||
|
use tokio::sync::OnceCell;
|
||||||
|
|
||||||
static ACCESS_TOKEN: Mutex<String> = Mutex::new(String::new());
|
static ACCESS_TOKEN: OnceCell<String> = OnceCell::const_new();
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
struct RefreshTokenRequest {
|
struct RefreshTokenRequest {
|
||||||
@@ -30,6 +40,11 @@ struct AccessTokenResponse {
|
|||||||
access_token: String,
|
access_token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
struct RegistrationAccessTokenResponse {
|
||||||
|
refresh_token: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
struct YoutubeUploadParams {
|
struct YoutubeUploadParams {
|
||||||
snippet: YoutubeUploadParamsSnippet,
|
snippet: YoutubeUploadParamsSnippet,
|
||||||
@@ -78,10 +93,110 @@ impl Default for YoutubeUploadParamsStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Debug)]
|
||||||
|
struct YoutubePlaylistItemsParams {
|
||||||
|
snippet: YoutubePlaylistItemsParamsSnippet,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Debug)]
|
||||||
|
struct YoutubePlaylistItemsParamsSnippet {
|
||||||
|
#[serde(rename = "playlistId")]
|
||||||
|
playlist_id: String,
|
||||||
|
position: u16,
|
||||||
|
#[serde(rename = "resourceId")]
|
||||||
|
resource_id: YoutubePlaylistItemsParamsSnippetResourceId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for YoutubePlaylistItemsParamsSnippet {
|
||||||
|
fn default() -> Self {
|
||||||
|
YoutubePlaylistItemsParamsSnippet {
|
||||||
|
playlist_id: "".to_string(),
|
||||||
|
position: 0,
|
||||||
|
resource_id: YoutubePlaylistItemsParamsSnippetResourceId {
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Debug)]
|
||||||
|
struct YoutubePlaylistItemsParamsSnippetResourceId {
|
||||||
|
kind: String,
|
||||||
|
#[serde(rename = "videoId")]
|
||||||
|
video_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for YoutubePlaylistItemsParamsSnippetResourceId {
|
||||||
|
fn default() -> Self {
|
||||||
|
YoutubePlaylistItemsParamsSnippetResourceId {
|
||||||
|
kind: "youtube#video".to_string(),
|
||||||
|
video_id: "".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
struct YoutubeVideos {
|
||||||
|
id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
struct YoutubePlaylistListResponse {
|
||||||
|
#[serde(rename = "nextPageToken")]
|
||||||
|
next_page_token: Option<String>,
|
||||||
|
items: Vec<YoutubePlaylistListResponseItem>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
struct YoutubePlaylistListResponseItem {
|
||||||
|
id: String,
|
||||||
|
snippet: YoutubePlaylistListResponseItemSnippet,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
struct YoutubePlaylistListResponseItemSnippet {
|
||||||
|
title: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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
|
/// Ensures that Token has been refreshed and that it is unique
|
||||||
fn refresh_token(config: &YoutubeConfig) -> Result<String, Box<dyn Error>> {
|
async fn refresh_token(config: &YoutubeConfig) -> Result<String, reqwest::Error> {
|
||||||
if let Ok(mut unlocked_access_token) = ACCESS_TOKEN.lock() {
|
ACCESS_TOKEN
|
||||||
if unlocked_access_token.is_empty() {
|
.get_or_try_init(|| async {
|
||||||
let refresh_token = RefreshTokenRequest {
|
let refresh_token = RefreshTokenRequest {
|
||||||
refresh_token: config.refresh_token.clone(),
|
refresh_token: config.refresh_token.clone(),
|
||||||
client_id: config.client_id.clone(),
|
client_id: config.client_id.clone(),
|
||||||
@@ -89,31 +204,122 @@ fn refresh_token(config: &YoutubeConfig) -> Result<String, Box<dyn Error>> {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let client = reqwest::blocking::Client::new();
|
let client = Client::new();
|
||||||
let res = client
|
let res = client
|
||||||
.post("https://accounts.google.com/o/oauth2/token")
|
.post("https://accounts.google.com/o/oauth2/token")
|
||||||
.json(&refresh_token)
|
.json(&refresh_token)
|
||||||
.send()?;
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
let access_token: AccessTokenResponse = res.json()?;
|
let access_token: AccessTokenResponse = res.json().await?;
|
||||||
|
|
||||||
*unlocked_access_token = access_token.access_token.clone();
|
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.clone_from(&a),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(ACCESS_TOKEN.lock().unwrap().to_string())
|
debug!("Playlists IDs: {:?}", &playlists);
|
||||||
|
Ok(playlists)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_resumable_upload(
|
/// 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(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
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)
|
||||||
|
.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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This function creates a resumable YT upload, putting all the parameters in
|
||||||
|
pub async fn create_resumable_upload(
|
||||||
config: &YoutubeConfig,
|
config: &YoutubeConfig,
|
||||||
vid: &PeerTubeVideo,
|
vid: &PeerTubeVideo,
|
||||||
) -> Result<String, Box<dyn Error>> {
|
) -> Result<String, Box<dyn Error>> {
|
||||||
let access_token = refresh_token(config)?;
|
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 {
|
let upload_params = YoutubeUploadParams {
|
||||||
snippet: {
|
snippet: {
|
||||||
YoutubeUploadParamsSnippet {
|
YoutubeUploadParamsSnippet {
|
||||||
title: vid.name.clone(),
|
title: vid.name.chars().take(100).collect::<String>(),
|
||||||
description: vid.description.clone(),
|
description: vid.description.clone(),
|
||||||
tags: vid.tags.clone(),
|
tags: vid.tags.clone(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@@ -125,13 +331,13 @@ pub fn create_resumable_upload(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
debug!("YT upload params: {:?}", &upload_params);
|
||||||
|
|
||||||
let client = reqwest::blocking::Client::new();
|
let client = Client::new();
|
||||||
|
|
||||||
let res = client.post("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet%2Cstatus")
|
let res = client.post("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet%2Cstatus")
|
||||||
.header("Authorization", format!("Bearer {}", access_token))
|
.header("Authorization", format!("Bearer {}", access_token))
|
||||||
.json(&upload_params)
|
.json(&upload_params)
|
||||||
.send()?;
|
.send().await?;
|
||||||
|
|
||||||
if res.status().is_success() {
|
if res.status().is_success() {
|
||||||
Ok(res
|
Ok(res
|
||||||
@@ -145,28 +351,61 @@ pub fn create_resumable_upload(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn upload_video(
|
/// This takes the PT stream for download, connects it to YT stream for upload
|
||||||
f_path: &str,
|
pub async fn now_kiss(
|
||||||
|
dl_url: &str,
|
||||||
r_url: &str,
|
r_url: &str,
|
||||||
config: &YoutubeConfig,
|
config: &YoutubeConfig,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
pg_conf: &TootubeConfig,
|
||||||
|
) -> Result<String, Box<dyn Error>> {
|
||||||
// Get access token
|
// Get access token
|
||||||
let access_token = refresh_token(config)?;
|
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
|
// Create client
|
||||||
let client = reqwest::blocking::Client::new();
|
let client = Client::new();
|
||||||
|
|
||||||
let file = File::open(f_path)?;
|
|
||||||
|
|
||||||
let res = client
|
let res = client
|
||||||
.put(r_url)
|
.put(r_url)
|
||||||
.header("Authorization", format!("Bearer {}", access_token))
|
.header("Authorization", format!("Bearer {}", access_token))
|
||||||
.body(file)
|
.body(Body::wrap_stream(async_stream))
|
||||||
.send()?;
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
if res.status().is_success() {
|
if res.status().is_success() {
|
||||||
Ok(())
|
let yt_videos: YoutubeVideos = res.json().await?;
|
||||||
|
Ok(yt_videos.id)
|
||||||
} else {
|
} else {
|
||||||
Err(TootubeError::new(&format!("Cannot upload video: {:?}", res.text()?)).into())
|
Err(TootubeError::new(&format!("Cannot upload video: {:?}", res.text().await?)).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user