core/tags/album: Query MusicBrainz for album release information

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-04-30 10:56:18 -04:00
parent d79eb2c9b8
commit 570efda299
6 changed files with 81 additions and 3 deletions

View File

@ -6,4 +6,5 @@ res += SConscript("tags/Sconscript")
res += SConscript("containers/Sconscript")
env.UsePackage("gstreamer-1.0")
env.UsePackage("libmusicbrainz5");
Return("res")

View File

@ -1,11 +1,15 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <core/string.h>
#include <core/version.h>
#include <core/tags/album.h>
#include <musicbrainz5/mb5_c.h>
static struct database album_db;
static inline void __album_init_file(struct album *al, struct cache_file *f)
{
cache_file_init(f, g_strdup_printf("%d", al->al_year),
@ -18,6 +22,56 @@ static inline void __album_deinit_file(struct cache_file *f)
g_free(f->cf_name);
}
static Mb5Metadata __album_query_releaseid(struct album *album,
Mb5Query *mb5)
{
gchar *param, *query = "query";
Mb5Metadata metadata = NULL;
tQueryResult result;
gchar error[256];
if (album->al_artist != NULL)
param = g_strdup_printf("release:\"%s\" AND artist:%s",
album->al_lower, album->al_artist->ar_lower);
else if (album->al_year != 0)
param = g_strdup_printf("release:\"%s\" AND date:%d",
album->al_lower, album->al_year);
else
param = g_strdup_printf("release:\"%s\"", album->al_lower);
metadata = mb5_query_query(mb5, "release", "", "", 1, &query, &param);
result = mb5_query_get_lastresult(mb5);
if (result != 0) {
mb5_query_get_lasterrormessage(mb5, error, sizeof(error));
g_printf("MusicBrainz: %s\n", error);
}
g_free(param);
return metadata;
}
static bool __album_fetch_artwork(struct album *album)
{
Mb5Metadata metadata;
Mb5Query *mb5;
bool ret;
if (album_artwork_exists(album))
return true;
mb5 = mb5_query_new(OCARINA_NAME, NULL, 0);
if (!mb5)
return true;
metadata = __album_query_releaseid(album, mb5);
if (metadata)
mb5_metadata_delete(metadata);
ret = mb5_query_get_lasthttpcode(mb5) != 503;
mb5_query_delete(mb5);
return ret;
}
static gchar *__album_key(const gchar *name, unsigned int year)
{
return g_strdup_printf("%u/%s", year, name);
@ -33,6 +87,8 @@ static struct album *__album_alloc(gchar *name, unsigned int year)
album->al_lower = string_lowercase(album->al_name);
album->al_artist = NULL;
if (!album_artwork_exists(album))
idle_schedule(IDLE_ASYNC, IDLE_FUNC(__album_fetch_artwork), album);
return album;
}

View File

@ -14,6 +14,7 @@ static void test_init()
struct queue *q = collection_get_queue();
GSList *list;
idle_init();
filter_init();
tags_init();
playlist_init(NULL);

View File

@ -29,6 +29,8 @@ static void test_album()
struct album *album;
struct file f;
idle_init();
album = ALBUM(album_ops->dbe_alloc("1998/Hyrule Symphony"));
test_verify_hyrule(album);
@ -99,15 +101,31 @@ static void test_album_db()
static void test_album_artwork()
{
struct album *album;
struct album *ocarina, *majora, *wind;
struct artist *koji;
artist_db_init();
idle_deinit();
idle_init();
album = album_find("Hyrule Symphony", 1998);
test_equal(album_artwork_exists(album), (bool)false);
ocarina = album_find("Ocarina of Time", 1998);
majora = album_find("Majora's Mask", 2000);
wind = album_find("Wind Waker", 0);
koji = artist_find("Koji Kondo");
ocarina->al_artist = koji;
test_equal(album_artwork_exists(ocarina), (bool)false);
test_equal(album_artwork_exists(majora), (bool)false);
test_equal(album_artwork_exists(wind), (bool)false);
while (idle_run_task()) {}
test_equal(album_artwork_exists(ocarina), (bool)false);
test_equal(album_artwork_exists(majora), (bool)false);
test_equal(album_artwork_exists(wind), (bool)false);
album_db_deinit();
artist_db_deinit();
idle_deinit();
}

View File

@ -72,6 +72,7 @@ static void test_track()
gchar *date;
setlocale(LC_TIME, "C");
idle_init();
filter_init();
tags_init();
file_init(&f, "track_tag", 0, 0);

View File

@ -77,6 +77,7 @@ static void test_collection_sidebar()
test_equal(gtk_widget_is_visible(GTK_WIDGET(progress)), true);
g_main_loop_run(main_loop);
test_equal((float)gtk_progress_bar_get_fraction(progress), (float)0.5);
while (idle_run_task()) {}
g_main_loop_run(main_loop);
test_equal(gtk_widget_is_visible(GTK_WIDGET(progress)), false);