changeset 2306:dd78327f5747

keep track of how long song is played
author Tomasz Mon <desowin@gmail.com>
date Sun, 13 Jan 2008 17:33:47 +0100
parents 738914331374
children 1606353a9fcb
files src/scrobbler/plugin.c src/scrobbler/scrobbler.c src/scrobbler/scrobbler.h
diffstat 3 files changed, 36 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/scrobbler/plugin.c	Sat Jan 12 13:09:10 2008 -0600
+++ b/src/scrobbler/plugin.c	Sun Jan 13 17:33:47 2008 +0100
@@ -53,6 +53,7 @@
 
 static GMutex *hs_mutex, *xs_mutex;
 static GCond *hs_cond, *xs_cond;
+guint track_timeout;
 
 static GeneralPlugin scrobbler_gp =
 {
@@ -91,6 +92,14 @@
 	g_cond_signal(xs_cond);
 }
 
+static void aud_hook_playback_end(gpointer aud_hook_data, gpointer user_data)
+{
+    if (track_timeout) {
+        g_source_remove(track_timeout);
+        track_timeout = 0;
+    }
+}
+
 static void init(void)
 {
 	char *username = NULL, *password = NULL;
@@ -166,6 +175,7 @@
 	}
 
 	aud_hook_associate("playback begin", aud_hook_playback_begin, NULL);
+	aud_hook_associate("playback end", aud_hook_playback_end, NULL);
 
 	pdebug("plugin started", DEBUG);
 }
@@ -205,6 +215,7 @@
 	gerpok_sc_cleaner();
 
 	aud_hook_dissociate("playback begin", aud_hook_playback_begin);
+	aud_hook_dissociate("playback end", aud_hook_playback_end);
 }
 
 static void *xs_thread(void *data __attribute__((unused)))
@@ -253,6 +264,8 @@
 				
 				sc_addentry(m_scrobbler, tuple, aud_tuple_get_int(tuple, FIELD_LENGTH, NULL) / 1000);
 				gerpok_sc_addentry(m_scrobbler, tuple, aud_tuple_get_int(tuple, FIELD_LENGTH, NULL) / 1000);
+                                if (!track_timeout)
+                                    track_timeout = g_timeout_add_seconds(1, sc_timeout, NULL);
 			}
 			else
 				pdebug("tuple does not contain an artist or a title, not submitting.", DEBUG);
--- a/src/scrobbler/scrobbler.c	Sat Jan 12 13:09:10 2008 -0600
+++ b/src/scrobbler/scrobbler.c	Sun Jan 13 17:33:47 2008 +0100
@@ -65,6 +65,7 @@
 		*mb,
 		*album;
 	int utctime, track, len;
+int timeplayed;
 	int numtries;
 	void *next;
 } item_t;
@@ -73,6 +74,14 @@
 static item_t *q_queue_last = NULL;
 static int q_nitems;
 
+/* isn't there better way for that? --desowin */
+gboolean sc_timeout(gpointer data) {
+    if (q_queue_last && audacious_drct_get_playing())
+        q_queue_last->timeplayed+=1;
+
+    return TRUE;
+}
+
 gchar *
 xmms_urldecode_plain(const gchar * encoded_path)
 {
@@ -131,9 +140,15 @@
 
 	item->artist = fmt_escape(aud_tuple_get_string(tuple, FIELD_ARTIST, NULL));
 	item->title = fmt_escape(aud_tuple_get_string(tuple, FIELD_TITLE, NULL));
-	item->utctime = t;
 	item->len = len;
 	item->track = aud_tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL);
+        if (t == -1) { /* now playing song */
+            item->timeplayed = 0;
+            item->utctime = time(NULL);
+        } else { /* item from queue */
+            item->timeplayed = len;
+            item->utctime = t;
+        }
 
 #ifdef NOTYET
 	if(tuple->mb == NULL)
@@ -623,8 +638,8 @@
 		 * don't submit queued tracks which don't yet meet audioscrobbler
 		 * requirements...
 		 */
-		if ((time(NULL) - item->utctime) < (item->len / 2) &&
-		    (time(NULL) - item->utctime) < 240)
+		if ((item->timeplayed < (item->len / 2)) &&
+		    (item->timeplayed < 240))
 			continue;
 
 		if (!item)
@@ -870,10 +885,9 @@
             title = g_strdup(entry[2]);
             track = atoi(entry[3]);
             len = atoi(entry[4]);
-            /* entry[5] should always be "P"... */
             t = atoi(entry[6]);
 
-            {
+            if (!strncmp(entry[5], "L", 1)) {
                 Tuple *tuple = aud_tuple_new();
                 gchar* string_value;
                 string_value = xmms_urldecode_plain(artist);
@@ -946,7 +960,7 @@
                     I_TITLE(item),
                     item->track,
                     I_LEN(item),
-                    "P",
+                    ((item->timeplayed > item->len/2) || (item->timeplayed > 240)) ? "L" : "S",
                     I_TIME(item));
     }
 
@@ -1028,7 +1042,7 @@
 	g_mutex_lock(mutex);
 
 	sc_submit_np(tuple);
-	q_put(tuple, time(NULL), len);
+	q_put(tuple, -1, len);
 
 	/*
 	 * This will help make sure the queue will be saved on a nasty
--- a/src/scrobbler/scrobbler.h	Sat Jan 12 13:09:10 2008 -0600
+++ b/src/scrobbler/scrobbler.h	Sun Jan 13 17:33:47 2008 +0100
@@ -5,6 +5,8 @@
 
 #define SC_CURL_TIMEOUT 5
 
+gboolean sc_timeout(gpointer data);
+
 int sc_idle(GMutex *);
 void sc_init(char *, char *);
 void sc_addentry(GMutex *, Tuple *, int);