SongIterator Connection::GetDirectoryRecursive(const std::string &directory) { prechecksNoCommandsList(); mpd_send_list_all_meta(m_connection.get(), directory.c_str()); checkErrors(); return SongIterator(m_connection.get(), fetchItemSong); }
int cmd_listall(int argc, char **argv, struct mpd_connection *conn) { const char * listall = ""; int i = 0; if (argc > 0) listall = charset_to_utf8(argv[i]); do { char *tmp = strdup(listall); strip_trailing_slash(tmp); if (options.custom_format) { if (!mpd_send_list_all_meta(conn, tmp)) printErrorAndExit(conn); print_entity_list(conn, MPD_ENTITY_TYPE_UNKNOWN); } else { if (!mpd_send_list_all(conn, tmp)) printErrorAndExit(conn); print_filenames(conn); } my_finishCommand(conn); free(tmp); } while (++i < argc && (listall = charset_to_utf8(argv[i])) != NULL); return 0; }
Song Connection::GetSong(const std::string &path) { prechecksNoCommandsList(); mpd_send_list_all_meta(m_connection.get(), path.c_str()); mpd_song *s = mpd_recv_song(m_connection.get()); mpd_response_finish(m_connection.get()); checkErrors(); return Song(s); }
void get_random_song(struct mpd_connection *conn, char *str, char *path) { struct mpd_entity *entity; int listened0 = 65000, skipnum, numberofsongs = 0; struct mpd_stats *stats = mpd_run_stats(conn); if (stats == NULL) return; numberofsongs = mpd_stats_get_number_of_songs(stats); mpd_stats_free(stats); skipnum = rand() % numberofsongs; syslog(LOG_DEBUG, "%s: path %s; number of songs: %i skip: %i\n", __func__, path, numberofsongs, skipnum); if (!mpd_send_list_all_meta(conn, ""))//path)) { syslog(LOG_ERR, "%s: error: mpd_send_list_meta %s\n", __func__, path); return; } while((entity = mpd_recv_entity(conn)) != NULL) { const struct mpd_song *song; if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) { if (skipnum-- > 0) continue; int listened; song = mpd_entity_get_song(entity); listened = db_get_song_numplayed(mpd_get_title(song), mpd_get_artist(song)); if (listened < listened0) { listened0 = listened; syslog(LOG_DEBUG, "listened: %i ", listened); int probability = 50 + db_get_song_rating(mpd_get_title(song), mpd_get_artist(song)); syslog(LOG_DEBUG, "probability: %i ", probability); bool Yes = (rand() % 100) < probability; if (Yes) { sprintf(str, "%s", mpd_song_get_uri(song)); syslog(LOG_DEBUG, "uri: %s ", str); syslog(LOG_DEBUG, "title: %s ", mpd_get_title(song)); syslog(LOG_DEBUG, "artist: %s", mpd_get_artist(song)); } } } mpd_entity_free(entity); } }
int main(int argc, char ** argv) { struct mpd_connection *conn = setup_connection(); int id = -1; if (!mpd_send_list_all_meta(conn, "radio")) printErrorAndExit(conn); print_entity_list(conn, MPD_ENTITY_TYPE_SONG);//MPD_ENTITY_TYPE_UNKNOWN); id = find_songname_id(conn, "artist Radiohead"); printf("id = %i\n", id); mpd_connection_free(conn); return 0; }
/* fetch a new db tree on root */ static int fetch_db_tree(DB_NODE **p_root,struct mpd_connection *conn) { if (*p_root) return 1; /* create root */ DB_NODE *root = (DB_NODE*)malloc(sizeof(DB_NODE)); root->type = DB_DIRECTORY; /* use calloc for root->uri to ease free_db_tree() */ root->uri = (char *)calloc(2,sizeof(char)); strncpy(root->uri,"/",1); root->rating = 0; root->duration = 0; root->p = NULL; root->s = NULL; root->c = NULL; root->num_c = 0; /* set top node */ DB_NODE *top = root; if (!mpd_send_list_all_meta(conn,"/")) { clear_or_exit_on_error(conn); return 1; } /* add nodes recursively */ struct mpd_entity *entity; while ((entity = mpd_recv_entity(conn))) { DB_NODE *node = (DB_NODE*)malloc(sizeof(DB_NODE)); const char *name = NULL; switch (mpd_entity_get_type(entity)) { case MPD_ENTITY_TYPE_DIRECTORY: node->type = DB_DIRECTORY; name = mpd_directory_get_path(mpd_entity_get_directory(entity)); assert (name && strlen(name)); node->uri = (char *)calloc(strlen(name)+1,sizeof(char)); strncpy(node->uri,name,strlen(name)); node->rating = 0; node->duration = 0; break; case MPD_ENTITY_TYPE_SONG: node->type = DB_SONG; name = mpd_song_get_uri(mpd_entity_get_song(entity)); assert (name && strlen(name)); node->uri = (char *)calloc(strlen(name)+1,sizeof(char)); strncpy(node->uri,name,strlen(name)); node->rating = 0; node->duration = mpd_song_get_duration(mpd_entity_get_song(entity)); break; case MPD_ENTITY_TYPE_PLAYLIST: node->type = DB_PLAYLIST; name = mpd_playlist_get_path(mpd_entity_get_playlist(entity)); assert (name && strlen(name)); node->uri = (char *)calloc(strlen(name)+1,sizeof(char)); strncpy(node->uri,name,strlen(name)); node->rating = 0; node->duration = 0; break; case MPD_ENTITY_TYPE_UNKNOWN: break; } /* backtrace top until it's parent of node */ while (top != root && !(strstr(node->uri,top->uri) && node->uri[strlen(top->uri)] == '/')) top = top->p; /* if current top already has chlidren */ if (top->c) { DB_NODE *node2 = top->c; /* find the last child */ while (node2->s) node2 = node2->s; node2->s = node; node->p = node2->p; node->p->num_c++; } else { top->c = node; node->p = top; node->p->num_c++; } /* no child, no next sibling */ node->c = NULL; node->s = NULL; node->num_c = 0; /* update top node */ if (node->type == DB_DIRECTORY) top = node; mpd_entity_free(entity); } if (mpd_connection_get_error(conn) == MPD_ERROR_SUCCESS) mpd_response_finish(conn); else if (!mpd_connection_clear_error(conn)) exit_on_error(conn); *p_root = root; return 0; }
/* * Retrieves the entire song library from MPD */ bool Control::update_library() { uint32_t total = 0; Song * song; struct mpd_entity * ent; const struct mpd_directory * ent_directory; const struct mpd_song * ent_song; const struct mpd_playlist * ent_playlist; Directory * dir = rootdir; EXIT_IDLE; pms->log(MSG_DEBUG, 0, "Updating library from DB time %d to %d\n", st->last_db_update_time, st->db_update_time); st->last_db_update_time = st->db_update_time; if (!mpd_send_list_all_meta(conn->h(), "")) { return false; } _library->clear(); while ((ent = mpd_recv_entity(conn->h())) != NULL) { switch(mpd_entity_get_type(ent)) { case MPD_ENTITY_TYPE_SONG: ent_song = mpd_entity_get_song(ent); song = new Song(ent_song); song->id = MPD_SONG_NO_ID; song->pos = MPD_SONG_NO_NUM; _library->add_local(song); dir->songs.push_back(song); break; case MPD_ENTITY_TYPE_PLAYLIST: /* Issue #8: https://github.com/ambientsound/pms/issues/8 */ ent_playlist = mpd_entity_get_playlist(ent); //pms->log(MSG_DEBUG, 0, "NOT IMPLEMENTED in update_library(): got playlist entity in update_library(): %s\n", mpd_playlist_get_path(ent_playlist)); break; case MPD_ENTITY_TYPE_DIRECTORY: ent_directory = mpd_entity_get_directory(ent); dir = rootdir->add(mpd_directory_get_path(ent_directory)); assert(dir != NULL); /* if (dir == NULL) { dir = rootdir; } */ break; case MPD_ENTITY_TYPE_UNKNOWN: pms->log(MSG_DEBUG, 0, "BUG in update_library(): entity type not implemented by libmpdclient\n"); break; default: pms->log(MSG_DEBUG, 0, "BUG in update_library(): entity type not implemented by PMS\n"); break; } mpd_entity_free(ent); ++total; } pms->log(MSG_DEBUG, 0, "Processed a total of %d entities during library update\n", total); return get_error_bool(); }