mirror of
https://github.com/OMGeeky/google_youtube.git
synced 2025-12-26 16:17:24 +01:00
logging & use upload_resumable to not get OOM exceptions (out of memory) with bigger files.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
/target
|
||||
/Cargo.lock
|
||||
/auth/
|
||||
/test/
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
[package]
|
||||
name = "google_youtube"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
downloader_config = { path = "../downloader_config" }
|
||||
exponential_backoff = { path = "../exponential_backoff" }
|
||||
exponential_backoff = { version="0.1.3", path = "../exponential_backoff" }
|
||||
|
||||
google-youtube3 = "4.0.1"
|
||||
google-youtube3 = "5.0.2"
|
||||
|
||||
reqwest = { version = "0.11.13", features = ["default", "json"] }
|
||||
tokio = { version = "1.23.0", features = ["full"] }
|
||||
@@ -19,7 +19,8 @@ serde_json = "1.0"
|
||||
async-trait = "0.1.60"
|
||||
strfmt = "0.2.2"
|
||||
|
||||
|
||||
log = "0.4.17"
|
||||
simplelog = "0.12.1"
|
||||
#[patch.crates-io.yup-oauth2]
|
||||
#path = "../../Documents/GitHub/OMGeeky/yup-oauth2"
|
||||
#version = "7.1.0"
|
||||
|
||||
39
src/lib.rs
39
src/lib.rs
@@ -1,3 +1,4 @@
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use std::default::Default;
|
||||
use std::error::Error;
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -173,7 +174,7 @@ impl YoutubeClient {
|
||||
tags: V,
|
||||
privacy_status: PrivacyStatus,
|
||||
) -> Result<Video, Box<dyn Error>> {
|
||||
println!("test 123");
|
||||
info!("test 123");
|
||||
let video = Video {
|
||||
snippet: Some(VideoSnippet {
|
||||
title: Some(title.into()),
|
||||
@@ -208,21 +209,21 @@ impl YoutubeClient {
|
||||
client: &YouTube<HttpsConnector<HttpConnector>>,
|
||||
para: &UploadParameters,
|
||||
) -> Result<(Response<Body>, Video), google_youtube3::Error> {
|
||||
println!("Opening file: {:?}", para.path);
|
||||
info!("Opening file: {:?}", para.path);
|
||||
let stream = std::fs::File::open(¶.path)?;
|
||||
println!("Uploading file: {:?}", para.path);
|
||||
let insert_call = client
|
||||
.videos()
|
||||
.insert(para.video.clone());
|
||||
println!("Insert call created");
|
||||
let res = insert_call
|
||||
.upload(stream, "video/mp4".parse().unwrap());
|
||||
println!("Upload request");
|
||||
info!("Uploading file: {:?}", para.path);
|
||||
|
||||
let insert_call = client.videos().insert(para.video.clone());
|
||||
info!("Insert call created");
|
||||
let upload_call = insert_call.upload_resumable(stream, "video/mp4".parse().unwrap());
|
||||
// .upload(stream, "video/mp4".parse().unwrap());
|
||||
info!("Upload request");
|
||||
let res = upload_call.await;
|
||||
info!("Upload request done");
|
||||
res
|
||||
.await
|
||||
}
|
||||
|
||||
println!("Starting upload...");
|
||||
info!("Starting upload...");
|
||||
let (response, video) =
|
||||
generic_check_backoff_youtube(&self.client, ¶ms, upload_fn).await??;
|
||||
|
||||
@@ -235,13 +236,13 @@ impl YoutubeClient {
|
||||
// .await??;
|
||||
|
||||
if response.status().is_success() {
|
||||
println!("Upload successful!");
|
||||
info!("Upload successful!");
|
||||
Ok(video)
|
||||
} else {
|
||||
println!("Upload failed!\n=====================================\n");
|
||||
println!("Status: {}", response.status());
|
||||
println!("Body: {:?}", response);
|
||||
println!("Video: {:?}", video);
|
||||
info!("Upload failed!\n=====================================\n");
|
||||
info!("Status: {}", response.status());
|
||||
info!("Body: {:?}", response);
|
||||
info!("Video: {:?}", video);
|
||||
Err(format!("got status: {}", response.status().as_u16()).into())
|
||||
}
|
||||
|
||||
@@ -256,7 +257,7 @@ impl YoutubeClient {
|
||||
// match insert {
|
||||
// Ok(insert) => Ok(insert),
|
||||
// Err(e) => {
|
||||
// println!("Error: {:?}", e);
|
||||
// info!("Error: {:?}", e);
|
||||
// Err(Box::new(e))
|
||||
// }
|
||||
// }
|
||||
@@ -289,6 +290,6 @@ impl YoutubeClient {
|
||||
}
|
||||
|
||||
pub async fn sample() -> Result<(), Box<dyn Error>> {
|
||||
println!("Hello from the youtube lib!");
|
||||
info!("Hello from the youtube lib!");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
64
src/main.rs
64
src/main.rs
@@ -1,14 +1,23 @@
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use std::error::Error;
|
||||
use std::path::Path;
|
||||
use google_youtube3::api::Playlist;
|
||||
|
||||
use google_youtube3::api::Playlist;
|
||||
use simplelog::ColorChoice;
|
||||
use tokio::fs::File;
|
||||
|
||||
use google_youtube::{PrivacyStatus, scopes, YoutubeClient};
|
||||
use google_youtube::{scopes, PrivacyStatus, YoutubeClient};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
println!("Hello, world!");
|
||||
simplelog::TermLogger::init(
|
||||
simplelog::LevelFilter::Debug,
|
||||
simplelog::Config::default(),
|
||||
simplelog::TerminalMode::Mixed,
|
||||
ColorChoice::Auto,
|
||||
)
|
||||
.expect("TermLogger init failed");
|
||||
info!("Hello, world!");
|
||||
sample().await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -16,9 +25,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
pub async fn sample() -> Result<(), Box<dyn Error>> {
|
||||
// get client
|
||||
let scopes = vec![
|
||||
// google_youtube::scopes::YOUTUBE,
|
||||
google_youtube::scopes::YOUTUBE_UPLOAD,
|
||||
google_youtube::scopes::YOUTUBE_READONLY,
|
||||
scopes::YOUTUBE,
|
||||
scopes::YOUTUBE_UPLOAD,
|
||||
scopes::YOUTUBE_READONLY,
|
||||
];
|
||||
// let client_secret_path = "auth/youtube_client_secret.json";
|
||||
let client_secret_path = "auth/test_rust_client_secret_2.json";
|
||||
@@ -36,47 +45,54 @@ pub async fn sample() -> Result<(), Box<dyn Error>> {
|
||||
.doit()
|
||||
.await?;
|
||||
for element in channels.items.unwrap() {
|
||||
println!(
|
||||
info!(
|
||||
"channel name: {:?}",
|
||||
element.snippet.unwrap().title.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
println!("Channels done!\n\n");
|
||||
info!("Channels done!\n\n");
|
||||
*/
|
||||
|
||||
// get a playlist by name or create it if it does not exist('LunaOni Clips' for example)
|
||||
let playlist = client.find_playlist_or_create_by_name("LunaOni Clips").await;
|
||||
println!("playlist: {:?}", playlist);
|
||||
let playlist = client
|
||||
.find_playlist_or_create_by_name("LunaOni Clips")
|
||||
.await;
|
||||
info!("playlist: {:?}", playlist);
|
||||
|
||||
println!("Playlist done!\n\n");
|
||||
println!("Uploading video... (30 times");
|
||||
info!("Playlist done!\n\n");
|
||||
info!("Uploading video... (30 times");
|
||||
for i in 0..30 {
|
||||
println!("+==={:2}==;uploading video...", i);
|
||||
let path = Path::new("test/test.mp4");
|
||||
info!("+==={:2}==;uploading video...", i);
|
||||
// let path = Path::new("test/test.mp4");
|
||||
let path = Path::new("D:/1740252892.mp4_000.mp4");
|
||||
// let file = File::open(path).await?;
|
||||
let title = format!("test video {}", i);
|
||||
let description = "test video description";
|
||||
let title = "test video2";
|
||||
let tags = vec!["test".to_string(), "test2".to_string()];
|
||||
let privacy_status = PrivacyStatus::Private;
|
||||
|
||||
println!("uploading video...");
|
||||
info!("uploading video...");
|
||||
let insert = client
|
||||
.upload_video(&path, description, title, tags, privacy_status)
|
||||
.upload_video(&path, title.as_str(), description, tags, privacy_status)
|
||||
.await;
|
||||
println!("uploading video... (done)");
|
||||
info!("uploading video... (done)");
|
||||
|
||||
println!("adding to playlist...");
|
||||
if let Ok(video) = &insert{
|
||||
info!("adding to playlist...");
|
||||
if let Ok(video) = &insert {
|
||||
if let Ok(playlist) = &playlist {
|
||||
println!("adding video to playlist: {:?}", playlist);
|
||||
info!("adding video to playlist: {:?}", playlist);
|
||||
let _ = client.add_video_to_playlist(&video, &playlist).await;
|
||||
info!("adding video to playlist: (done)");
|
||||
} else {
|
||||
info!("playlist not found");
|
||||
}
|
||||
} else {
|
||||
info!("video upload failed");
|
||||
}
|
||||
println!("adding to playlist... (done)");
|
||||
|
||||
println!("\n\n{:?}\n\n/==={:2}========;", insert, i);
|
||||
info!("\n\n{:?}\n\n/==={:2}========;", insert, i);
|
||||
}
|
||||
println!("Done!");
|
||||
info!("Done!");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user