New hack: mpd_random_playlist.c
[mpd-hacks.git] / mpd_random_playlist / mpd_random_playlist.c
blobd130c68ccff44c61433e149af614bd637a2ec660
1 /*
2 * mpd_random_playlist.c 19.01.2009
4 * Copyright 2009 Max Christian Pohle <webmaster@coderonline.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
21 #define PROGRAM_NAME "mpd_random_playlist"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <libmpd/libmpd.h>
26 #include <string.h>
27 #include <time.h>
29 void help(char *program);
30 void error_callback(MpdObj *mi,int errorid, char *msg, void *userdata);
32 int main(int argc, char **argv)
34 int iport = 6600;
35 int icount = 50;
36 int silent = 1;
38 char *hostname = getenv("MPD_HOST");
39 char *port = getenv("MPD_PORT");
40 char *password = getenv("MPD_PASSWORD");
41 char *count = argv[1];
44 if(!hostname) { hostname = "localhost"; }
45 if(!password) { password = ""; }
46 if(count) { icount = atoi(count); }
47 if(port) { iport = atoi(port); }
49 if(!silent)
50 printf("==== " PROGRAM_NAME "====\n\n");
51 if(icount <= 0)
53 help(PROGRAM_NAME);
54 exit(0);
57 MpdObj *mo = mpd_new(hostname, iport, password);
59 if(!mpd_connect(mo))
60 { mpd_send_password(mo); }
62 mpd_signal_connect_error(mo, (ErrorCallback)error_callback, NULL);
64 if(!silent)
65 printf("STATUS\n");
66 if (!mpd_check_connected(mo))
68 mpd_free(mo);
69 printf("\tERROR: failed to connect to %s\n", hostname);
70 return 1;
72 else
74 if(!silent)
75 printf("\t> connection to host `%s` estamblished.\n", hostname);
78 MpdData *listItems = mpd_database_get_complete(mo);
80 int total_songs = mpd_stats_get_total_songs(mo);
81 int total_playtime = mpd_stats_get_db_playtime(mo);
82 if(!silent)
84 printf("\t> found %d songs in database.\n", total_songs);
85 printf("\t> with a total playtime of %d\n", total_playtime);
86 printf("\t> random selection of %d files starts now...\n", icount);
88 srand( (unsigned)time( NULL ) );
89 int i;
90 for(i=1;i<=icount; i++)
92 float random_song = ((float) rand() / RAND_MAX) * total_songs;
93 int songNr = (int) random_song;
95 int q;
96 for(q=0; q<=songNr; q++)
98 listItems = mpd_data_get_next(listItems);
99 if (mpd_data_is_last(listItems))
100 { listItems = mpd_data_get_first(listItems); }
102 if(listItems->type != MPD_DATA_TYPE_SONG)
103 { q--; }
106 if(!silent)
107 printf("\t\t> adding %s\n", listItems->song->title);
108 mpd_playlist_queue_add(mo, listItems->song->file);
110 if(!silent)
111 printf("\t> random selection successfully done.\n");
113 mpd_playlist_clear(mo);
114 if(!silent)
115 printf("\t> cleared playlist.\n");
116 mpd_playlist_queue_commit(mo);
117 if(!silent)
118 printf("\t> submitted new playlist with random songs.\n");
119 mpd_player_play(mo);
120 if(!silent)
121 printf("\t> started playback.\n\n");
124 mpd_free(mo);
125 exit(0);
128 void error_callback(MpdObj *mi,int errorid, char *msg, void *userdata)
130 help(PROGRAM_NAME);
131 printf("ERROR: %s\n...maybe wrong password?", msg);
132 exit(1);
135 void help(char *program)
137 printf(
138 "HELP\n"
139 "\tPROGRAM-CALL\n"
140 "\t./%s [--help] [<NUMBER_OF_RANDOM_FILES>]\n\n"
141 "\tENVIROMENT-VARIABLES\n\n"
142 "\tMPD_HOST\thostname of mpd (optional)\n"
143 "\tMPD_PORT\tport to connect with mpd (optional)\n"
144 "\tMPD_PASSWORD\tpassword for the mpd-server (optional, but recommend)\n\n"
145 "\t...to set these use export <VARIABLE>=<VALUE>-syntax\n\n"
146 , program