structopt-ify, move list ablum to subcommand add filtering by regex.
This commit is contained in:
74
src/main.rs
74
src/main.rs
@@ -1,15 +1,44 @@
|
||||
use std::path::Path;
|
||||
use std::error::Error;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use google_api_auth;
|
||||
use google_photoslibrary1;
|
||||
use yup_oauth2::GetToken;
|
||||
use regex::Regex;
|
||||
use structopt::StructOpt;
|
||||
use yup_oauth2::{Authenticator, InstalledFlow};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Read application secret from a file. Sometimes it's easier to compile it directly into
|
||||
// the binary. The clientsecret file contains JSON like `{"installed":{"client_id": ... }}`
|
||||
let credentials = "/home/wathiede/src/xinu.tv/photosync/auth/oob-credentials.json";
|
||||
let secret = yup_oauth2::read_application_secret(Path::new(credentials)).expect(credentials);
|
||||
#[derive(Debug, StructOpt)]
|
||||
enum Command {
|
||||
/// List albums for the user of the given credentials. Optionally title filter.
|
||||
ListAlbums { title_filter: Option<Regex> },
|
||||
}
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(
|
||||
name = "photosync",
|
||||
about = "Utility for interacting with Google Photos API."
|
||||
)]
|
||||
struct Opt {
|
||||
/// Activate debug mode
|
||||
#[structopt(short, parse(from_occurrences))]
|
||||
verbose: u32,
|
||||
|
||||
/// Path to json file containing Google client ID and secrets for out of band auth flow.
|
||||
#[structopt(long)]
|
||||
credentials: PathBuf,
|
||||
/// Path to json file where photosync will store auth tokens refreshed from Google.
|
||||
#[structopt(long)]
|
||||
token_cache: PathBuf,
|
||||
|
||||
#[structopt(subcommand)]
|
||||
cmd: Command,
|
||||
}
|
||||
|
||||
fn new_client(
|
||||
credentials: &PathBuf,
|
||||
token_cache: &PathBuf,
|
||||
) -> Result<google_photoslibrary1::Client, Box<dyn Error>> {
|
||||
let secret = yup_oauth2::read_application_secret(credentials)?;
|
||||
|
||||
// Create an authenticator that uses an InstalledFlow to authenticate. The
|
||||
// authentication tokens are persisted to a file named tokencache.json. The
|
||||
@@ -19,7 +48,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
secret,
|
||||
yup_oauth2::InstalledFlowReturnMethod::Interactive,
|
||||
))
|
||||
.persist_tokens_to_disk("/tmp/tokencache.json")
|
||||
.persist_tokens_to_disk(token_cache)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
@@ -27,18 +56,43 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let auth = google_api_auth::yup_oauth2::from_authenticator(auth, scopes);
|
||||
|
||||
let client = google_photoslibrary1::Client::new(auth);
|
||||
Ok(google_photoslibrary1::Client::new(auth))
|
||||
}
|
||||
|
||||
fn list_albums(
|
||||
client: google_photoslibrary1::Client,
|
||||
title_filter: Option<Regex>,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
for album in client
|
||||
.shared_albums()
|
||||
.list()
|
||||
.iter_shared_albums_with_all_fields()
|
||||
{
|
||||
let a = album?;
|
||||
if let Some(title_filter) = &title_filter {
|
||||
match &a.title {
|
||||
Some(title) => {
|
||||
if !title_filter.is_match(&title) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
None => continue,
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"album: {} {}",
|
||||
a.id.unwrap_or("NO ID".to_string()),
|
||||
a.title.unwrap_or("NO TITLE".to_string())
|
||||
a.title.unwrap_or("NO TITLE".to_string()).to_string()
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let opt = Opt::from_args();
|
||||
println!("opt: {:?}", opt);
|
||||
let client = new_client(&opt.credentials, &opt.token_cache)?;
|
||||
match opt.cmd {
|
||||
Command::ListAlbums { title_filter } => list_albums(client, title_filter),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user