From da0663773809cdb727db014ee20b4dc03f96ed92 Mon Sep 17 00:00:00 2001 From: Avuton Olrich Date: Wed, 21 Jan 2009 06:03:57 -0800 Subject: [PATCH] New hack: mpd_random_playlist.c mpd_random_playlist.c - None Author: Max Christian Pohle License: GPL-2 Description: A small C program which creates a random playlist. Version: 19.01.2009 --- mpd-hacks.txt | 6 ++ mpd_random_playlist/Makefile | 2 + mpd_random_playlist/mpd_random_playlist.c | 148 ++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 mpd_random_playlist/Makefile create mode 100644 mpd_random_playlist/mpd_random_playlist.c diff --git a/mpd-hacks.txt b/mpd-hacks.txt index 55d4664..8bcefc3 100644 --- a/mpd-hacks.txt +++ b/mpd-hacks.txt @@ -24,6 +24,12 @@ mpdnp.py - http://mpd.wikia.com/index.php?title=Hack:mpdnp&oldid=5394 Description: This plugin lets you inform all users in weechat the song which MPD is currently playing. Version: 0.3 +mpd_random_playlist.c - None + Author: Max Christian Pohle + License: GPL-2 + Description: A small C program which creates a random playlist. + Version: 19.01.2009 + weechat-mpd-ctl.py - http://mpd.wikia.com/index.php?title=Hack:mpd&oldid=5391 Author: Skippy the Kangoo License: GPL-2 diff --git a/mpd_random_playlist/Makefile b/mpd_random_playlist/Makefile new file mode 100644 index 0000000..1258625 --- /dev/null +++ b/mpd_random_playlist/Makefile @@ -0,0 +1,2 @@ +all: + gcc -Wall `pkg-config --libs libmpd` -o mpd_random_playlist *.c diff --git a/mpd_random_playlist/mpd_random_playlist.c b/mpd_random_playlist/mpd_random_playlist.c new file mode 100644 index 0000000..d130c68 --- /dev/null +++ b/mpd_random_playlist/mpd_random_playlist.c @@ -0,0 +1,148 @@ +/* + * mpd_random_playlist.c 19.01.2009 + * + * Copyright 2009 Max Christian Pohle + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#define PROGRAM_NAME "mpd_random_playlist" + +#include +#include +#include +#include +#include + +void help(char *program); +void error_callback(MpdObj *mi,int errorid, char *msg, void *userdata); + +int main(int argc, char **argv) +{ + int iport = 6600; + int icount = 50; + int silent = 1; + + char *hostname = getenv("MPD_HOST"); + char *port = getenv("MPD_PORT"); + char *password = getenv("MPD_PASSWORD"); + char *count = argv[1]; + + + if(!hostname) { hostname = "localhost"; } + if(!password) { password = ""; } + if(count) { icount = atoi(count); } + if(port) { iport = atoi(port); } + + if(!silent) + printf("==== " PROGRAM_NAME "====\n\n"); + if(icount <= 0) + { + help(PROGRAM_NAME); + exit(0); + } + + MpdObj *mo = mpd_new(hostname, iport, password); + + if(!mpd_connect(mo)) + { mpd_send_password(mo); } + + mpd_signal_connect_error(mo, (ErrorCallback)error_callback, NULL); + + if(!silent) + printf("STATUS\n"); + if (!mpd_check_connected(mo)) + { + mpd_free(mo); + printf("\tERROR: failed to connect to %s\n", hostname); + return 1; + } + else + { + if(!silent) + printf("\t> connection to host `%s` estamblished.\n", hostname); + } + + MpdData *listItems = mpd_database_get_complete(mo); + + int total_songs = mpd_stats_get_total_songs(mo); + int total_playtime = mpd_stats_get_db_playtime(mo); + if(!silent) + { + printf("\t> found %d songs in database.\n", total_songs); + printf("\t> with a total playtime of %d\n", total_playtime); + printf("\t> random selection of %d files starts now...\n", icount); + } + srand( (unsigned)time( NULL ) ); + int i; + for(i=1;i<=icount; i++) + { + float random_song = ((float) rand() / RAND_MAX) * total_songs; + int songNr = (int) random_song; + + int q; + for(q=0; q<=songNr; q++) + { + listItems = mpd_data_get_next(listItems); + if (mpd_data_is_last(listItems)) + { listItems = mpd_data_get_first(listItems); } + + if(listItems->type != MPD_DATA_TYPE_SONG) + { q--; } + } + + if(!silent) + printf("\t\t> adding %s\n", listItems->song->title); + mpd_playlist_queue_add(mo, listItems->song->file); + } + if(!silent) + printf("\t> random selection successfully done.\n"); + + mpd_playlist_clear(mo); + if(!silent) + printf("\t> cleared playlist.\n"); + mpd_playlist_queue_commit(mo); + if(!silent) + printf("\t> submitted new playlist with random songs.\n"); + mpd_player_play(mo); + if(!silent) + printf("\t> started playback.\n\n"); + + + mpd_free(mo); + exit(0); +} + +void error_callback(MpdObj *mi,int errorid, char *msg, void *userdata) +{ + help(PROGRAM_NAME); + printf("ERROR: %s\n...maybe wrong password?", msg); + exit(1); +} + +void help(char *program) +{ + printf( + "HELP\n" + "\tPROGRAM-CALL\n" + "\t./%s [--help] []\n\n" + "\tENVIROMENT-VARIABLES\n\n" + "\tMPD_HOST\thostname of mpd (optional)\n" + "\tMPD_PORT\tport to connect with mpd (optional)\n" + "\tMPD_PASSWORD\tpassword for the mpd-server (optional, but recommend)\n\n" + "\t...to set these use export =-syntax\n\n" + , program + ); +} -- 2.11.4.GIT