Mercurial > lbo > hg > async-google-apis
view example_crates/youtube_example/src/main.rs @ 171:714165191d0d
Merge pull request #6 from mike-kfed/youtube_example
minimal youtube API example listing top 5 videos on youtube
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Thu, 10 Dec 2020 13:19:58 +0100 |
parents | 139c5e1dc358 |
children | b3cc6e0793bd |
line wrap: on
line source
//! List most famous Youtube videos. //! //! When run with no arguments, the top 5 videos id, title and duration will be shown //! mod youtube_v3_types; use youtube_v3_types as yt; use env_logger; use async_google_apis_common as common; use std::rc::Rc; /// Create a new HTTPS client. fn https_client() -> common::TlsClient { let conn = hyper_rustls::HttpsConnector::new(); let cl = hyper::Client::builder().build(conn); cl } #[tokio::main] async fn main() { env_logger::init(); let https = https_client(); // Put your client secret in the working directory! let sec = common::yup_oauth2::read_application_secret("client_secret.json") .await .expect("client secret couldn't be read."); let auth = common::yup_oauth2::InstalledFlowAuthenticator::builder( sec, common::yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect, ) .persist_tokens_to_disk("tokencache.json") .hyper_client(https.clone()) .build() .await .expect("InstalledFlowAuthenticator failed to build"); let scopes = vec![ yt::YoutubeScopes::YoutubeUpload, yt::YoutubeScopes::YoutubeForceSsl, ]; let mut cl = yt::VideosService::new(https, Rc::new(auth)); cl.set_scopes(&scopes); { // By default, list most popular videos let mut general_params = yt::YoutubeParams::default(); general_params.fields = Some("*".to_string()); let mut p = yt::VideosListParams::default(); p.youtube_params = Some(general_params); p.part = "id,contentDetails,snippet".into(); p.chart = Some("mostPopular".to_string()); let resp = cl.list(&p).await.expect("listing your yt failed!"); if let Some(videos) = resp.items { for f in videos { println!( "{} => duration: {} title: '{}'", f.id.unwrap(), f.content_details .map(|cd| cd.duration.unwrap_or("n.a.".to_string())) .unwrap(), f.snippet .map(|s| s.title.unwrap_or("n.a.".to_string())) .unwrap() ); } } } }