Skip to content

Commit

Permalink
Merge pull request #455 from RustyNova016/release/v0.4.5
Browse files Browse the repository at this point in the history
Release/v0.4.5
  • Loading branch information
RustyNova016 authored Jan 14, 2025
2 parents 2223e61 + eb8dfd9 commit 286f66b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "alistral"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
repository = "https://github.com/RustyNova016/alistral"
publish = true
Expand Down
15 changes: 3 additions & 12 deletions src/models/cli/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use crate::models::config::Config;
use crate::tools::cache::copy_to_debug;
use crate::tools::cache::delete_database;
use crate::tools::cache::refresh_data::refresh_data;
use crate::tools::listens::import::import_listen_dump;
//use crate::tools::listens::import::import_listen_dump;
use clap::ValueEnum;
use clap::{Parser, Subcommand};
use color_eyre::owo_colors::OwoColorize;

#[derive(Parser, Debug, Clone)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -90,17 +90,8 @@ impl CacheCommand {
}
get_conn().await;
}
CacheSubcommands::LoadDump {
username: _,
path: _,
} => {
println!();
println!(" {}", "Temporary unavailable :(".black().on_red().bold());
println!("The data dumps currently don't have enough information to be useful, and may give false informations.");
println!("So the feature is temporarily disabled");
println!();
println!("Please see: https://tickets.metabrainz.org/browse/LB-1687");
//import_listen_dump(path, &Config::check_username(username)).await;
CacheSubcommands::LoadDump { username, path } => {
import_listen_dump(conn, path, &Config::check_username(username)).await;
}
CacheSubcommands::Clear => {
delete_database(&DB_LOCATION).expect("Failed to delete the database");
Expand Down
97 changes: 68 additions & 29 deletions src/tools/listens/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub async fn import_listen_dump(
let zip_file = File::open(dump_path).expect("Couldn't access zip file.");
let mut archive = zip::ZipArchive::new(zip_file).expect("Couldn't read zip file.");

let mut import_trans = conn.begin().await.expect("Couldn't start transaction");

// We read the zip file
for i in 0..archive.len() {
let file = archive.by_index(i).unwrap();
Expand Down Expand Up @@ -52,12 +54,20 @@ pub async fn import_listen_dump(

// Then save the content
let mut count = 0;
let mut trans = conn.begin().await.expect("Couldn't start transaction");
let mut trans = import_trans
.begin()
.await
.expect("Couldn't start transaction");
for line in content {
let line = line.expect("Couldn't read line");
//println!("{line}");
let data: ImportListen =
serde_json::from_str(&line).expect("Couldn't convert line from JSON");
let data: ImportListen = serde_json::from_str(&line).unwrap_or_else(|err| {
panic!(
"Couldn't convert line #{} of {}. Error: {err}",
count + 1,
outpath.display()
)
});

data.save(&mut trans, username)
.await
Expand All @@ -68,8 +78,13 @@ pub async fn import_listen_dump(

println_cli_info(format!("Loaded {count} listens"));
}
import_trans
.commit()
.await
.expect("Couldn't save transaction");
}

//TODO: #449 Move ImportListen to models
#[derive(Debug, Deserialize, Serialize)]
struct ImportListen {
listened_at: i64,
Expand All @@ -83,6 +98,25 @@ struct ImportListenMetaData {
release_name: Option<String>,
recording_msid: String,
additional_info: HashMap<String, serde_json::Value>,
mbid_mapping: Option<ImportListenMapping>,
}

#[derive(Debug, Deserialize, Serialize)]
struct ImportListenMapping {
caa_id: Option<u64>,
caa_release_mbid: Option<String>,
artists: Vec<ImportListenMappingArtists>,
artist_mbids: Vec<String>,
release_mbid: Option<String>,
recording_mbid: String,
recording_name: String,
}

#[derive(Debug, Deserialize, Serialize)]
struct ImportListenMappingArtists {
artist_mbid: String,
join_phrase: String,
artist_credit_name: String,
}

impl ImportListen {
Expand All @@ -109,12 +143,11 @@ impl ImportListen {

messybrainz.insert_or_ignore(&mut *conn).await.unwrap();

// Check if we have a recording MBID. If so, we can map it
let additional_info = &self.track_metadata.additional_info;

if let Some(serde_json::Value::String(recording)) = additional_info.get("recording_mbid") {
if let Some(mapping) = self.track_metadata.mbid_mapping {
// First insert the mbid
Recording::add_redirect_mbid(conn, recording).await.unwrap();
Recording::add_redirect_mbid(conn, &mapping.recording_mbid)
.await
.unwrap();

let user = User::find_by_name(&mut *conn, user_name)
.await?
Expand All @@ -124,10 +157,9 @@ impl ImportListen {
&mut *conn,
user.id,
self.track_metadata.recording_msid.clone(),
recording.to_string(),
mapping.recording_mbid.to_string(),
)
.await
.unwrap();
.await?;
}

let listen = Listen {
Expand All @@ -146,22 +178,29 @@ impl ImportListen {

#[cfg(test)]
mod tests {
// use std::path::PathBuf;
// use musicbrainz_db_lite::models::listenbrainz::listen::Listen;
// use crate::database::get_conn;
// use crate::tools::listens::import::import_listen_dump;

// #[tokio::test]
// async fn load_listen_dump_test() {
// import_listen_dump(
// &PathBuf::from("tests/data/listen_dump.zip".to_string()),
// "TestNova",
// )
// .await;
//
// let conn = &mut *get_conn().await;
//
// let listen = sqlx::query_as!(Listen, "SELECT * FROM listens WHERE listened_at = 1705054374").fetch_one(&mut *conn).await.expect("This listen should exist");
// listen.get_recording_or_fetch(conn).await.expect("The listen should be mapped");
// }
use crate::database::get_conn;
use crate::tools::listens::import::import_listen_dump;
use musicbrainz_db_lite::models::listenbrainz::listen::Listen;
use std::path::PathBuf;

#[sqlx::test]
async fn load_listen_dump_test() {
let conn = &mut *get_conn().await;
import_listen_dump(
conn,
&PathBuf::from("tests/data/listen_dump.zip".to_string()),
"TestNova",
)
.await;

//TODO: #451 Make sqlx prepare query macros in tests + Convert the queries
let listen: Listen = sqlx::query_as("SELECT * FROM listens WHERE listened_at = 1705054374")
.fetch_one(&mut *conn)
.await
.expect("This listen should exist");
listen
.get_recording_or_fetch(conn)
.await
.expect("The listen should be mapped");
}
}
Binary file modified tests/data/listen_dump.zip
Binary file not shown.

0 comments on commit 286f66b

Please sign in to comment.