Skip to content

Commit

Permalink
feat(anni): starry api
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Sep 6, 2024
1 parent 668e9d0 commit d84aec9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions anni-workspace/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct WorkspaceConfig {
#[serde(rename = "workspace")]
inner: WorkspaceConfigInner,
#[serde(rename = "library")]
#[serde(default)]
libraries: HashMap<String, LibraryConfig>,
}

Expand Down
51 changes: 37 additions & 14 deletions anni/src/subcommands/workspace/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use clap_handler::handler;
use colored::Colorize;
use inquire::Confirm;
use ptree::TreeBuilder;
use serde::Deserialize;
use std::path::PathBuf;
use std::str::FromStr;

Expand Down Expand Up @@ -122,7 +123,6 @@ fn handle_workspace_add(me: WorkspaceAddAction) -> anyhow::Result<()> {

match config.metadata() {
anni_workspace::config::WorkspaceMetadata::Repo => {
// TODO: do not enforce this folder name
let AlbumFolderInfo {
release_date,
catalog,
Expand Down Expand Up @@ -155,25 +155,48 @@ fn handle_workspace_add(me: WorkspaceAddAction) -> anyhow::Result<()> {
}
}
anni_workspace::config::WorkspaceMetadata::Remote { endpoint, token } => {
let AlbumFolderInfo {
release_date,
catalog,
#[derive(Deserialize)]
struct StarryDirnameResponse {
#[serde(rename = "t")]
title: String,
#[serde(rename = "a")]
artist: Option<String>,
#[serde(rename = "c")]
catalog: Option<String>,
#[serde(rename = "y")]
year: Option<i32>,
#[serde(rename = "m")]
month: Option<i32>,
#[serde(rename = "d")]
day: Option<i32>,
}

let StarryDirnameResponse {
title,
edition,
..
} = AlbumFolderInfo::from_str(&folder_name)?;
artist,
catalog,
year,
month,
day,
} = reqwest::Client::new()
.post("https://starry.anni.rs/api/v1/dirname")
.body(folder_name.clone())
.send()
.await?
.json()
.await?;

let client = AnnimClient::new(endpoint, token.as_deref());
let input = AddAlbumInput {
album_id: Some(album_id),
title: &title,
edition: edition.as_deref(),
catalog: Some(catalog.as_ref()),
artist: UNKNOWN_ARTIST,
year: release_date.year() as i32,
month: release_date.month().map(i32::from),
day: release_date.day().map(i32::from),
extra: None,
edition: None,
catalog: catalog.as_deref(),
artist: artist.as_deref().unwrap_or(UNKNOWN_ARTIST),
year: year.unwrap_or(0),
month: month,
day: day,
extra: Some(serde_json::json!({ "rawdir": folder_name })),
discs: discs.iter().map(Into::into).collect(),
};
client.add_album_input(input, true).await?;
Expand Down

0 comments on commit d84aec9

Please sign in to comment.