Add search-media-items verb to list all photos in an album.

This commit is contained in:
Bill Thiede 2020-02-05 09:54:18 -08:00
parent 076a947b08
commit 9691e7b456

View File

@ -3,6 +3,7 @@ use std::path::PathBuf;
use google_api_auth; use google_api_auth;
use google_photoslibrary1; use google_photoslibrary1;
use google_photoslibrary1::schemas::SearchMediaItemsRequest;
use regex::Regex; use regex::Regex;
use structopt::StructOpt; use structopt::StructOpt;
use yup_oauth2::{Authenticator, InstalledFlow}; use yup_oauth2::{Authenticator, InstalledFlow};
@ -10,7 +11,12 @@ use yup_oauth2::{Authenticator, InstalledFlow};
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
enum Command { enum Command {
/// List albums for the user of the given credentials. Optionally title filter. /// List albums for the user of the given credentials. Optionally title filter.
ListAlbums { title_filter: Option<Regex> }, ListAlbums {
title_filter: Option<Regex>,
},
SearchMediaItems {
album_id: String,
},
} }
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
@ -59,6 +65,40 @@ fn new_client(
Ok(google_photoslibrary1::Client::new(auth)) Ok(google_photoslibrary1::Client::new(auth))
} }
fn search_media_items(
client: google_photoslibrary1::Client,
album_id: String,
) -> Result<(), Box<dyn Error>> {
let mut page_token = None;
let mut total = 0;
loop {
let resp = client
.media_items()
.search(SearchMediaItemsRequest {
album_id: Some(album_id.clone()),
page_token,
..Default::default()
})
.execute_with_all_fields()?;
let media_items = resp.media_items.ok_or("no results")?;
println!("got ({}) items", media_items.len());
total += media_items.len();
for mi in media_items {
println!(
"{} {}",
mi.id.unwrap_or("NO ID".to_string()),
mi.filename.unwrap_or("NO FILENAME".to_string())
);
}
page_token = resp.next_page_token;
if page_token.is_none() {
println!("({}) items total", total);
return Ok(());
}
}
}
fn list_albums( fn list_albums(
client: google_photoslibrary1::Client, client: google_photoslibrary1::Client,
title_filter: Option<Regex>, title_filter: Option<Regex>,
@ -80,9 +120,10 @@ fn list_albums(
} }
} }
println!( println!(
"album: {} {}", "album: {} {} ({} items)",
a.id.unwrap_or("NO ID".to_string()), a.id.unwrap_or("NO ID".to_string()),
a.title.unwrap_or("NO TITLE".to_string()).to_string() a.title.unwrap_or("NO TITLE".to_string()).to_string(),
a.media_items_count.unwrap_or(0)
); );
} }
Ok(()) Ok(())
@ -94,5 +135,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = new_client(&opt.credentials, &opt.token_cache)?; let client = new_client(&opt.credentials, &opt.token_cache)?;
match opt.cmd { match opt.cmd {
Command::ListAlbums { title_filter } => list_albums(client, title_filter), Command::ListAlbums { title_filter } => list_albums(client, title_filter),
Command::SearchMediaItems { album_id } => search_media_items(client, album_id),
} }
} }