mirror of
https://github.com/SuperBFG7/ympd
synced 2024-12-26 02:50:26 +00:00
Feat: improve last_played function
This commit is contained in:
parent
ba187b17ce
commit
2cc733af35
@ -982,6 +982,8 @@ function filterCols(x) {
|
|||||||
tags.push('Pos');
|
tags.push('Pos');
|
||||||
else if (x == 'colsBrowseFilesystem')
|
else if (x == 'colsBrowseFilesystem')
|
||||||
tags.push('Type');
|
tags.push('Type');
|
||||||
|
if (x == 'colsLastPlayed')
|
||||||
|
tags.push('LastPlayed');
|
||||||
|
|
||||||
var cols = [];
|
var cols = [];
|
||||||
for (var i = 0; i < settings[x].length; i++) {
|
for (var i = 0; i < settings[x].length; i++) {
|
||||||
@ -1189,6 +1191,8 @@ function setCols(table, className) {
|
|||||||
tags.push('Pos');
|
tags.push('Pos');
|
||||||
if (table == 'BrowseFilesystem')
|
if (table == 'BrowseFilesystem')
|
||||||
tags.push('Type');
|
tags.push('Type');
|
||||||
|
if (table == 'LastPlayed')
|
||||||
|
tags.push('LastPlayed');
|
||||||
|
|
||||||
tags.sort();
|
tags.sort();
|
||||||
|
|
||||||
@ -1526,6 +1530,7 @@ function parseLastPlayed(obj) {
|
|||||||
var minutes = Math.floor(obj.data[i].Duration / 60);
|
var minutes = Math.floor(obj.data[i].Duration / 60);
|
||||||
var seconds = obj.data[i].Duration - minutes * 60;
|
var seconds = obj.data[i].Duration - minutes * 60;
|
||||||
obj.data[i].Duration = minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
|
obj.data[i].Duration = minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
|
||||||
|
obj.data[i].LastPlayed = new Date(obj.data[i].LastPlayed * 1000).toUTCString();
|
||||||
var row = document.createElement('tr');
|
var row = document.createElement('tr');
|
||||||
row.setAttribute('data-songpos', (obj.data[i].Pos + 1));
|
row.setAttribute('data-songpos', (obj.data[i].Pos + 1));
|
||||||
row.setAttribute('data-uri', obj.data[i].uri);
|
row.setAttribute('data-uri', obj.data[i].uri);
|
||||||
@ -1582,9 +1587,6 @@ function parseFilesystem(obj) {
|
|||||||
var tr = tbody.getElementsByTagName('tr');
|
var tr = tbody.getElementsByTagName('tr');
|
||||||
for (var i = 0; i < nrItems; i++) {
|
for (var i = 0; i < nrItems; i++) {
|
||||||
var uri = encodeURI(obj.data[i].uri);
|
var uri = encodeURI(obj.data[i].uri);
|
||||||
//if (tr[i])
|
|
||||||
// if (tr[i].getAttribute('data-uri') == uri)
|
|
||||||
// continue;
|
|
||||||
var row = document.createElement('tr');
|
var row = document.createElement('tr');
|
||||||
row.setAttribute('data-type', obj.data[i].Type);
|
row.setAttribute('data-type', obj.data[i].Type);
|
||||||
row.setAttribute('data-uri', uri);
|
row.setAttribute('data-uri', uri);
|
||||||
@ -1686,9 +1688,6 @@ function parsePlaylists(obj) {
|
|||||||
if (app.current.view == 'All') {
|
if (app.current.view == 'All') {
|
||||||
for (var i = 0; i < nrItems; i++) {
|
for (var i = 0; i < nrItems; i++) {
|
||||||
var uri = encodeURI(obj.data[i].uri);
|
var uri = encodeURI(obj.data[i].uri);
|
||||||
if (tr[i])
|
|
||||||
if (tr[i].getAttribute('data-uri') == uri)
|
|
||||||
continue;
|
|
||||||
var d = new Date(obj.data[i].last_modified * 1000);
|
var d = new Date(obj.data[i].last_modified * 1000);
|
||||||
var row = document.createElement('tr');
|
var row = document.createElement('tr');
|
||||||
row.setAttribute('data-uri', uri);
|
row.setAttribute('data-uri', uri);
|
||||||
@ -1707,9 +1706,6 @@ function parsePlaylists(obj) {
|
|||||||
else if (app.current.view == 'Detail') {
|
else if (app.current.view == 'Detail') {
|
||||||
for (var i = 0; i < nrItems; i++) {
|
for (var i = 0; i < nrItems; i++) {
|
||||||
var uri = encodeURI(obj.data[i].uri);
|
var uri = encodeURI(obj.data[i].uri);
|
||||||
//if (tr[i])
|
|
||||||
// if (tr[i].getAttribute('data-uri') == uri && tr[i].getAttribute('id') == 'playlistTrackId' + songpos)
|
|
||||||
// continue;
|
|
||||||
var row = document.createElement('tr');
|
var row = document.createElement('tr');
|
||||||
if (obj.smartpls == false)
|
if (obj.smartpls == false)
|
||||||
row.setAttribute('draggable','true');
|
row.setAttribute('draggable','true');
|
||||||
@ -1772,9 +1768,6 @@ function parseListDBtags(obj) {
|
|||||||
var cards = cardContainer.getElementsByClassName('card');
|
var cards = cardContainer.getElementsByClassName('card');
|
||||||
for (var i = 0; i < nrItems; i++) {
|
for (var i = 0; i < nrItems; i++) {
|
||||||
var id = genId(obj.data[i].value);
|
var id = genId(obj.data[i].value);
|
||||||
// if (cards[i])
|
|
||||||
// if (cards[i].getAttribute('id') == id)
|
|
||||||
// continue;
|
|
||||||
var card = document.createElement('div');
|
var card = document.createElement('div');
|
||||||
card.classList.add('card', 'ml-4', 'mr-4', 'mb-4', 'w-100');
|
card.classList.add('card', 'ml-4', 'mr-4', 'mb-4', 'w-100');
|
||||||
card.setAttribute('id', 'card' + id);
|
card.setAttribute('id', 'card' + id);
|
||||||
@ -1822,9 +1815,6 @@ function parseListDBtags(obj) {
|
|||||||
var tr = tbody.getElementsByTagName('tr');
|
var tr = tbody.getElementsByTagName('tr');
|
||||||
for (var i = 0; i < nrItems; i++) {
|
for (var i = 0; i < nrItems; i++) {
|
||||||
var uri = encodeURI(obj.data[i].value);
|
var uri = encodeURI(obj.data[i].value);
|
||||||
if (tr[i])
|
|
||||||
if (tr[i].getAttribute('data-uri') == uri)
|
|
||||||
continue;
|
|
||||||
var row = document.createElement('tr');
|
var row = document.createElement('tr');
|
||||||
row.setAttribute('data-uri', uri);
|
row.setAttribute('data-uri', uri);
|
||||||
row.innerHTML='<td data-col="Type"><span class="material-icons">album</span></td>' +
|
row.innerHTML='<td data-col="Type"><span class="material-icons">album</span></td>' +
|
||||||
|
11
src/list.c
11
src/list.c
@ -140,6 +140,17 @@ int list_push(struct list *l, const char *data, int value) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int list_insert(struct list *l, const char *data, int value) {
|
||||||
|
struct node *n = malloc(sizeof(struct node));
|
||||||
|
n->value = value;
|
||||||
|
n->data = strdup(data);
|
||||||
|
n->next = l->list;
|
||||||
|
|
||||||
|
l->list = n;
|
||||||
|
l->length++;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
struct node *list_node_extract(struct list *l, unsigned idx) {
|
struct node *list_node_extract(struct list *l, unsigned idx) {
|
||||||
if (l->list == NULL) { return NULL; }
|
if (l->list == NULL) { return NULL; }
|
||||||
struct node *current = l->list, **previous = &l->list;
|
struct node *current = l->list, **previous = &l->list;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
struct node {
|
struct node {
|
||||||
char *data;
|
char *data;
|
||||||
int value;
|
long value;
|
||||||
struct node *next;
|
struct node *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -13,6 +13,7 @@ struct list {
|
|||||||
|
|
||||||
int list_init(struct list *l);
|
int list_init(struct list *l);
|
||||||
int list_push(struct list *l, const char *data, int value);
|
int list_push(struct list *l, const char *data, int value);
|
||||||
|
int list_insert(struct list *l, const char *data, int value);
|
||||||
int list_shift(struct list *l, unsigned idx);
|
int list_shift(struct list *l, unsigned idx);
|
||||||
struct node *list_node_extract(struct list *l, unsigned idx);
|
struct node *list_node_extract(struct list *l, unsigned idx);
|
||||||
int list_replace(struct list *l, int pos, const char *data, int value);
|
int list_replace(struct list *l, int pos, const char *data, int value);
|
||||||
|
@ -1014,22 +1014,39 @@ void mympd_like_song_uri(const char *uri, int value) {
|
|||||||
|
|
||||||
void mympd_last_played_list(int song_id) {
|
void mympd_last_played_list(int song_id) {
|
||||||
struct mpd_song *song;
|
struct mpd_song *song;
|
||||||
|
char tmpfile[400];
|
||||||
|
char cfgfile[400];
|
||||||
|
snprintf(cfgfile, 400, "%s/state/last_played", config.varlibdir);
|
||||||
|
snprintf(tmpfile, 400, "%s/tmp/last_played", config.varlibdir);
|
||||||
|
|
||||||
if (song_id > -1) {
|
if (song_id > -1) {
|
||||||
song = mpd_run_get_queue_song_id(mpd.conn, song_id);
|
song = mpd_run_get_queue_song_id(mpd.conn, song_id);
|
||||||
if (song) {
|
if (song) {
|
||||||
list_push(&last_played, mpd_song_get_uri(song), 1);
|
list_insert(&last_played, mpd_song_get_uri(song), time(NULL));
|
||||||
mpd.last_last_played_id = song_id;
|
mpd.last_last_played_id = song_id;
|
||||||
mpd_song_free(song);
|
mpd_song_free(song);
|
||||||
if (last_played.length > config.last_played_count) {
|
if (last_played.length > config.last_played_count) {
|
||||||
list_shift(&last_played, 0);
|
list_shift(&last_played, last_played.length -1);
|
||||||
}
|
}
|
||||||
|
FILE *fp = fopen(tmpfile, "w");
|
||||||
|
if (fp == NULL) {
|
||||||
|
printf("Error opening %s\n", tmpfile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct node *current = last_played.list;
|
||||||
|
while (current != NULL) {
|
||||||
|
fprintf(fp, "%ld::%s\n", current->value, current->data);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
rename(tmpfile, cfgfile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mympd_last_played_song_id(int song_id) {
|
void mympd_last_played_song_id(int song_id) {
|
||||||
struct mpd_song *song;
|
struct mpd_song *song;
|
||||||
|
|
||||||
if (song_id > -1) {
|
if (song_id > -1) {
|
||||||
song = mpd_run_get_queue_song_id(mpd.conn, song_id);
|
song = mpd_run_get_queue_song_id(mpd.conn, song_id);
|
||||||
if (song) {
|
if (song) {
|
||||||
@ -1665,7 +1682,7 @@ int mympd_put_last_played_songs(char *buffer, unsigned int offset) {
|
|||||||
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) {
|
||||||
if (entities_returned++)
|
if (entities_returned++)
|
||||||
len += json_printf(&out, ",");
|
len += json_printf(&out, ",");
|
||||||
len += json_printf(&out, "{Pos: %d, ", entity_count);
|
len += json_printf(&out, "{Pos: %d, LastPlayed: %ld, ", entity_count, current->value);
|
||||||
if (!mpd_send_list_all_meta(mpd.conn, current->data))
|
if (!mpd_send_list_all_meta(mpd.conn, current->data))
|
||||||
RETURN_ERROR_AND_RECOVER("mpd_send_list_all_meta");
|
RETURN_ERROR_AND_RECOVER("mpd_send_list_all_meta");
|
||||||
if ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
if ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
|
||||||
|
26
src/mympd.c
26
src/mympd.c
@ -329,6 +329,31 @@ void read_statefiles() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int read_last_played() {
|
||||||
|
char cfgfile[400];
|
||||||
|
char *line;
|
||||||
|
char *data;
|
||||||
|
size_t n = 0;
|
||||||
|
ssize_t read;
|
||||||
|
long value;
|
||||||
|
|
||||||
|
snprintf(cfgfile, 400, "%s/state/last_played", config.varlibdir);
|
||||||
|
FILE *fp = fopen(cfgfile, "r");
|
||||||
|
if (fp == NULL) {
|
||||||
|
printf("Error opening %s\n", cfgfile);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
while ((read = getline(&line, &n, fp)) > 0) {
|
||||||
|
value = strtol(line, &data, 10);
|
||||||
|
if (strlen(data) > 2)
|
||||||
|
data = data + 2;
|
||||||
|
strtok(data, "\n");
|
||||||
|
list_push(&last_played, data, value);
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
return last_played.length;;
|
||||||
|
}
|
||||||
|
|
||||||
bool testdir(char *name, char *dirname) {
|
bool testdir(char *name, char *dirname) {
|
||||||
DIR* dir = opendir(dirname);
|
DIR* dir = opendir(dirname);
|
||||||
if (dir) {
|
if (dir) {
|
||||||
@ -499,6 +524,7 @@ int main(int argc, char **argv) {
|
|||||||
list_init(&mpd_tags);
|
list_init(&mpd_tags);
|
||||||
list_init(&mympd_tags);
|
list_init(&mympd_tags);
|
||||||
list_init(&last_played);
|
list_init(&last_played);
|
||||||
|
printf("Reading last played songs: %d\n", read_last_played());
|
||||||
|
|
||||||
if (config.ssl == true)
|
if (config.ssl == true)
|
||||||
mg_set_protocol_http_websocket(nc_http);
|
mg_set_protocol_http_websocket(nc_http);
|
||||||
|
Loading…
Reference in New Issue
Block a user