1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-06-22 21:03:13 +00:00

Feat: configuration for smart playlists in files per playlist under /var/lib/mympd/smartpls

This commit is contained in:
jcorporation 2018-09-25 23:49:14 +01:00
parent e244dbc29b
commit 0a09d06a4b
8 changed files with 61 additions and 49 deletions

View File

@ -56,3 +56,4 @@ install(FILES dist/htdocs/css/mympd.min.css DESTINATION share/${PROJECT_NAME}/ht
install(DIRECTORY htdocs/assets DESTINATION share/${PROJECT_NAME}/htdocs)
install(DIRECTORY DESTINATION ../var/lib/${PROJECT_NAME}/pics)
install(DIRECTORY DESTINATION ../var/lib/${PROJECT_NAME}/tmp)
install(DIRECTORY dist/smartpls DESTINATION ../var/lib/${PROJECT_NAME})

View File

@ -38,15 +38,3 @@ taglist = Artist,Album,AlbumArtist,Title,Track,Genre,Date,Composer,Performer
#Enable smart playlists
smartpls = true
#Newest songs = newest song minus one week
smartpls_newest = 604800
#Add max songs to myMPDsmart-newestSongs
smartpls_maxnewest = 200
#Add max songs to myMPDsmart-bestRated
smartpls_maxlike = 200
#Add max songs to myMPDsmart-mostPlayed
smartpls_maxplaycount = 200

1
dist/smartpls/myMPDsmart-bestRated vendored Normal file
View File

@ -0,0 +1 @@
{"type": "sticker", "sticker": "like", "maxentries": 200}

1
dist/smartpls/myMPDsmart-mostPlayed vendored Normal file
View File

@ -0,0 +1 @@
{"type": "sticker", "sticker": "playCount", "maxentries": 200}

1
dist/smartpls/myMPDsmart-newestSongs vendored Normal file
View File

@ -0,0 +1 @@
{"type": "newest", "timerange": 604800 , "maxentries": 200}

View File

@ -1883,14 +1883,53 @@ void mympd_disconnect() {
}
int mympd_smartpls_update_all() {
DIR *dir;
struct dirent *ent;
char *smartpltype;
char filename[400];
int je;
char *p_charbuf1;
int int_buf1, int_buf2;
if (!config.smartpls)
return 0;
if (mympd_smartpls_update("like", "myMPDsmart-bestRated") != 0)
return 1;
if (mympd_smartpls_update("playCount", "myMPDsmart-mostPlayed") != 0)
return 1;
if (mympd_smartpls_update_newest("myMPDsmart-newestSongs") != 0)
if ((dir = opendir ("/var/lib/mympd/smartpls")) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (strncmp (ent->d_name, ".", 1) == 0)
continue;
snprintf(filename, 400, "/var/lib/mympd/smartpls/%s", ent->d_name);
char *content = json_fread(filename);
je = json_scanf(content, strlen(content), "{type: %Q }", &smartpltype);
if (je != 1)
continue;
if (strcmp(smartpltype, "sticker") == 0) {
je = json_scanf(content, strlen(content), "{sticker: %Q, maxentries: %d}", &p_charbuf1, &int_buf1);
if (je == 2) {
mympd_smartpls_update(p_charbuf1, ent->d_name, int_buf1);
free(p_charbuf1);
}
else
printf("Can't parse file %s\n", filename);
}
else if (strcmp(smartpltype, "newest") == 0) {
je = json_scanf(content, strlen(content), "{timerange: %d, maxentries: %d}", &int_buf1, &int_buf2);
if (je == 2) {
mympd_smartpls_update_newest(ent->d_name, int_buf1, int_buf2);
}
else
printf("Can't parse file %s\n", filename);
}
else if (strcmp(smartpltype, "search") == 0) {
//to be implemented
}
free(smartpltype);
}
closedir (dir);
} else {
printf("Can't open dir /var/lib/mympd/smartpls\n");
return 1;
}
return 0;
}
@ -1907,7 +1946,10 @@ int mympd_smartpls_clear(char *playlist) {
if (strcmp(playlist, plpath) == 0)
exists = true;
mpd_playlist_free(pl);
if (exists)
break;
}
mpd_response_finish(mpd.conn);
if (exists) {
if (!mpd_run_rm(mpd.conn, playlist)) {
@ -1918,7 +1960,7 @@ int mympd_smartpls_clear(char *playlist) {
return 0;
}
int mympd_smartpls_update(char *sticker, char *playlist) {
int mympd_smartpls_update(char *sticker, char *playlist, int maxentries) {
struct mpd_pair *pair;
char *uri = NULL;
char *name;
@ -1970,10 +2012,8 @@ int mympd_smartpls_update(char *sticker, char *playlist) {
name = strtok(uri, "::");
p_value = strtok(NULL, "::");
value = strtol(p_value, &crap, 10);
if (strcmp(sticker, "playCount") == 0) {
if (value <= value_max)
continue;
}
if (value < value_max)
continue;
if (!mpd_run_playlist_add(mpd.conn, playlist, name)) {
LOG_ERROR_AND_RECOVER("mpd_run_playlist_add");
fclose(fp);
@ -1981,14 +2021,8 @@ int mympd_smartpls_update(char *sticker, char *playlist) {
return 1;
}
i++;
if (strcmp(sticker, "playCount") == 0) {
if (i >= config.smartpls_maxplaycount)
break;
}
else if (strcmp(sticker, "like") == 0) {
if (i >= config.smartpls_maxlike)
break;
}
if (i >= maxentries)
break;
}
fclose(fp);
free(uri);
@ -1997,7 +2031,7 @@ int mympd_smartpls_update(char *sticker, char *playlist) {
return 0;
}
int mympd_smartpls_update_newest(char *playlist) {
int mympd_smartpls_update_newest(char *playlist, int timerange, int maxentries) {
struct mpd_song *song;
char *uri;
char *p_value;
@ -2030,7 +2064,7 @@ int mympd_smartpls_update_newest(char *playlist) {
mympd_smartpls_clear(playlist);
value_max -= config.smartpls_newest; //one week
value_max -= timerange;
fp = fopen("/var/lib/mympd/tmp/playlist.tmp", "r");
if (fp == NULL) {
@ -2050,7 +2084,7 @@ int mympd_smartpls_update_newest(char *playlist) {
return 1;
}
i++;
if (i >= config.smartpls_maxnewest)
if (i >= maxentries)
break;
}
fclose(fp);

View File

@ -163,10 +163,6 @@ typedef struct {
bool mixramp;
const char* taglist;
bool smartpls;
long smartpls_newest;
long smartpls_maxnewest;
long smartpls_maxlike;
long smartpls_maxplaycount;
} t_config;
t_config config;
@ -204,8 +200,8 @@ void mympd_get_sticker(const char *uri, t_sticker *sticker);
void mympd_jukebox();
int mympd_smartpls_update_all();
int mympd_smartpls_clear(char *playlist);
int mympd_smartpls_update(char *sticker, char *playlist);
int mympd_smartpls_update_newest(char *playlist);
int mympd_smartpls_update(char *sticker, char *playlist, int maxentries);
int mympd_smartpls_update_newest(char *playlist, int timerange, int maxentries);
int mympd_get_updatedb_state(char *buffer);
int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, int *last_song_id, unsigned *queue_version, unsigned *queue_length);
int mympd_put_outputs(char *buffer);

View File

@ -167,14 +167,6 @@ static int inihandler(void* user, const char* section, const char* name, const c
p_config->smartpls = true;
else
p_config->smartpls = false;
else if (MATCH("smartpls_newest"))
p_config->smartpls_newest = strtol(value, &crap, 10);
else if (MATCH("smartpls_maxnewest"))
p_config->smartpls_maxnewest = strtol(value, &crap, 10);
else if (MATCH("smartpls_maxlike"))
p_config->smartpls_maxlike = strtol(value, &crap, 10);
else if (MATCH("smartpls_maxplaycount"))
p_config->smartpls_maxplaycount = strtol(value, &crap, 10);
else if (MATCH("mixramp"))
if (strcmp(value, "true") == 0)
p_config->mixramp = true;
@ -212,8 +204,6 @@ int main(int argc, char **argv) {
config.mixramp = true;
config.taglist = "Artist,Album,AlbumArtist,Title,Track,Genre,Date,Composer,Performer";
config.smartpls = true;
config.smartpls_newest = 604800;
config.smartpls_maxnewest = 200;
mpd.timeout = 3000;
mpd.last_update_sticker_song_id = -1;