diff --git a/src/module/module_repo.c b/src/module/module_repo.c index 0f06462957bb1f392e57912f4531ba7a05b963a3..67c19a67183d304cc1918027edf8b12b353ad32d 100644 --- a/src/module/module_repo.c +++ b/src/module/module_repo.c @@ -80,6 +80,12 @@ struct module_repo_internal { void (*craft_filename)(char str[PATH_MAX], size_t, struct kara *); }; +struct __uri { + char *fav; + struct module_repo_internal *repo; + struct lkt_uri uri; +}; + struct __memory { void *mem; size_t size; @@ -510,10 +516,27 @@ end_no_lock: pthread_exit(NULL); } +static void +__handle_fav_list_internal(const char UNUSED *key, const char *val, int comp, void *user) +{ + if (comp) { + /* Already processed because we only want one line */ + return; + } + + struct __uri *uri = (struct __uri *) user; + uri->uri.id = strtol(val, NULL, 0); + if (!database_plt_add_uri(uri->repo->db, uri->fav, &uri->uri)) { + LOG_ERROR("REPO", "Failed to add kara %ld to playlist %s", uri->uri.id, uri->fav); + return; + } + +} + static inline void __handle_fav_list(struct module_repo_internal *repo, char *fav, size_t fav_size) { - struct json_object *json, *item_json = NULL; + char *json; char fav_url[LKT_LINE_MAX]; char *fav_origin = NULL; @@ -537,32 +560,19 @@ __handle_fav_list(struct module_repo_internal *repo, char *fav, size_t fav_size) } database_plt_touch(repo->db, fav); - struct lkt_uri uri = { - .type = uri_id, - .is_int = true, + struct __uri uri = { + .fav = fav, + .repo = repo, + .uri = { + .type = uri_id, + .is_int = true, + }, }; - size_t len = json_object_array_length(json), i; - long id; - for (i = 0; i < len; ++i) { - item_json = json_object_array_get_idx(json, i); - if (item_json == NULL) { - LOG_ERROR("REPO", "There is no kara at index %ld in fav list %s", i, fav); - continue; - } - if (__safe_json_get_long(item_json, "id", &id)) { - LOG_ERROR("REPO", "Failed to get the id of the kara in fav list %s", fav_origin); - continue; - } - uri.id = id; - if (!database_plt_add_uri(repo->db, fav, &uri)) { - LOG_ERROR("REPO", "Failed to add kara %ld to playlist %s", id, fav); - continue; - } - } - + size_t len = json_parse_get_count(json, 1); + json_parse(json, 1, __handle_fav_list_internal, (void *) &uri); LOG_INFO("REPO", "Finished importing fav list '%s' as '%s'", fav_origin, fav); - json_object_put(json); + free(json); } static void *