changeset 2302:f074702a0df3

revise scrobblerqueue.txt a bit
author Tomasz Mon <desowin@gmail.com>
date Sat, 12 Jan 2008 18:12:08 +0100
parents 227de9a5467d
children ccca9906abd0
files src/scrobbler/scrobbler.c
diffstat 1 files changed, 118 insertions(+), 140 deletions(-) [+]
line wrap: on
line diff
--- a/src/scrobbler/scrobbler.c	Sat Jan 12 16:35:26 2008 +0100
+++ b/src/scrobbler/scrobbler.c	Sat Jan 12 18:12:08 2008 +0100
@@ -63,10 +63,8 @@
 	char *artist,
 		*title,
 		*mb,
-		*album,
-		utctime[16],
-		track[16],
-		len[16];
+		*album;
+	int utctime, track, len;
 	int numtries;
 	void *next;
 } item_t;
@@ -124,7 +122,7 @@
 	free(item);
 }
 
-static item_t *q_put(Tuple *tuple, int len)
+static item_t *q_put(Tuple *tuple, int t, int len)
 {
 	item_t *item;
 	const gchar *album;
@@ -133,10 +131,9 @@
 
 	item->artist = fmt_escape(aud_tuple_get_string(tuple, FIELD_ARTIST, NULL));
 	item->title = fmt_escape(aud_tuple_get_string(tuple, FIELD_TITLE, NULL));
-	g_snprintf(item->utctime, sizeof(item->utctime), "%ld", time(NULL));
-	g_snprintf(item->len, sizeof(item->len), "%d", len);
-	g_snprintf(item->track, sizeof(item->track), "%d",
-		aud_tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL));
+	item->utctime = t;
+	item->len = len;
+	item->track = aud_tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL);
 
 #ifdef NOTYET
 	if(tuple->mb == NULL)
@@ -147,10 +144,11 @@
 		item->mb = fmt_escape((char*)tuple->mb);
 #endif
 
-	if((album = aud_tuple_get_string(tuple, FIELD_ALBUM, NULL)) != NULL)
+	album = aud_tuple_get_string(tuple, FIELD_ALBUM, NULL);
+	if (album)
+		item->album = fmt_escape((char*) album);
+	else
 		item->album = fmt_escape("");
-	else
-		item->album = fmt_escape((char*) album);
 
 	q_nitems++;
 
@@ -625,8 +623,8 @@
 		 * don't submit queued tracks which don't yet meet audioscrobbler
 		 * requirements...
 		 */
-		if ((time(NULL) - atoi(item->utctime)) < (atoi(item->len) / 2) &&
-		    (time(NULL) - atoi(item->utctime)) < 240)
+		if ((time(NULL) - item->utctime) < (item->len / 2) &&
+		    (time(NULL) - item->utctime) < 240)
 			continue;
 
 		if (!item)
@@ -634,12 +632,18 @@
 
                 g_string_append(submission,sc_itemtag('a',i,I_ARTIST(item)));
                 g_string_append(submission,sc_itemtag('t',i,I_TITLE(item)));
-                g_string_append(submission,sc_itemtag('l',i,I_LEN(item)));
-                g_string_append(submission,sc_itemtag('i',i,I_TIME(item)));
+                gchar *tmp = g_strdup_printf("%d",I_LEN(item));
+                g_string_append(submission,sc_itemtag('l',i,tmp));
+                g_free(tmp);
+                tmp = g_strdup_printf("%d",I_TIME(item));
+                g_string_append(submission,sc_itemtag('i',i,tmp));
+                g_free(tmp);
                 g_string_append(submission,sc_itemtag('m',i,I_MB(item)));
                 g_string_append(submission,sc_itemtag('b',i,I_ALBUM(item)));
                 g_string_append(submission,sc_itemtag('o',i,"P"));
-                g_string_append(submission,sc_itemtag('n',i,item->track));
+                tmp = g_strdup_printf("%d",item->track);
+                g_string_append(submission,sc_itemtag('n',i,tmp));
+                g_free(tmp);
                 g_string_append(submission,sc_itemtag('r',i,""));
 
 		pdebug(fmt_vastr("a[%d]=%s t[%d]=%s l[%d]=%s i[%d]=%s m[%d]=%s b[%d]=%s",
@@ -833,146 +837,120 @@
 
 static void read_cache(void)
 {
-	FILE *fd;
-	char buf[PATH_MAX], *cache = NULL, *ptr1, *ptr2;
-	int cachesize, written, i = 0;
-	item_t *item;
-	gchar* config_datadir;
+    FILE *fd;
+    char buf[PATH_MAX];
+    int i=0;
+    item_t *item;
+    gchar* config_datadir;
 
-	cachesize = written = 0;
+    config_datadir = audacious_get_localdir();
+    g_snprintf(buf, sizeof(buf), "%s/scrobblerqueue.txt", config_datadir);
+    g_free(config_datadir);
 
-	config_datadir = audacious_get_localdir();
-	g_snprintf(buf, sizeof(buf), "%s/scrobblerqueue.txt", config_datadir);
-	g_free(config_datadir);
+    if (!(fd = fopen(buf, "r")))
+        return;
+    pdebug(fmt_vastr("Opening %s", buf), DEBUG);
+    fclose(fd);
 
-	if (!(fd = fopen(buf, "r")))
-		return;
-	pdebug(fmt_vastr("Opening %s", buf), DEBUG);
-	while(!feof(fd))
-	{
-		cachesize += CACHE_SIZE;
-		cache = realloc(cache, cachesize + 1);
-		written += fread(cache + written, 1, CACHE_SIZE, fd);
-		cache[written] = '\0';
-	}
-	fclose(fd);
-	ptr1 = cache;
-	while(ptr1 < cache + written - 1)
-	{
-		char *artist, *title, *len, *time, *album, *mb;
+    gchar* cache;
+    gchar** values;
+    gchar** entry;
+    g_file_get_contents(buf, &cache, NULL, NULL);
+    values = g_strsplit(cache, "\n", 0);
+
+    int x;
+    for (x=0; values[x] && strlen(values[x]); x++) {
+        entry = g_strsplit(values[x], "\t", 0);
+        if (entry[0] && entry[1] && entry[2] && entry[3] && entry[4] && entry[6]) {
+            char *artist, *title, *album;
+            int t, len, track;
+
+            artist = g_strdup(entry[0]);
+            album = g_strdup(entry[1]);
+            title = g_strdup(entry[2]);
+            track = atoi(entry[3]);
+            len = atoi(entry[4]);
+            /* entry[5] should always be "P"... */
+            t = atoi(entry[6]);
 
-		pdebug("Pushed:", DEBUG);
-		ptr2 = strchr(ptr1, ' ');
-		artist = calloc(1, ptr2 - ptr1 + 1);
-		strncpy(artist, ptr1, ptr2 - ptr1);
-		ptr1 = ptr2 + 1;
-		ptr2 = strchr(ptr1, ' ');
-		title = calloc(1, ptr2 - ptr1 + 1);
-		strncpy(title, ptr1, ptr2 - ptr1);
-		ptr1 = ptr2 + 1;
-		ptr2 = strchr(ptr1, ' ');
-		len = calloc(1, ptr2 - ptr1 + 1);
-		strncpy(len, ptr1, ptr2 - ptr1);
-		ptr1 = ptr2 + 1;
-		ptr2 = strchr(ptr1, ' ');
-		time = calloc(1, ptr2 - ptr1 + 1);
-		strncpy(time, ptr1, ptr2 - ptr1);
-		ptr1 = ptr2 + 1;
-		ptr2 = strchr(ptr1, ' ');
-		album = calloc(1, ptr2 - ptr1 + 1);
-		strncpy(album, ptr1, ptr2 - ptr1);
-		ptr1 = ptr2 + 1;
-		ptr2 = strchr(ptr1, '\n');
-		if(ptr2 != NULL)
-			*ptr2 = '\0';
-		mb = calloc(1, strlen(ptr1) + 1);
-		strncpy(mb, ptr1, strlen(ptr1));
-		if(ptr2 != NULL)
-			*ptr2 = '\n';
-		/* Why is our save printing out CR/LF? */
-		ptr1 = ptr2 + 1;
+            {
+                Tuple *tuple = aud_tuple_new();
+                gchar* string_value;
+                string_value = xmms_urldecode_plain(artist);
+                aud_tuple_associate_string(tuple, FIELD_ARTIST, NULL, string_value);
+                g_free(string_value);
+                string_value = xmms_urldecode_plain(title);
+                aud_tuple_associate_string(tuple, FIELD_TITLE, NULL, string_value);
+                g_free(string_value);
+                string_value = xmms_urldecode_plain(album);
+                aud_tuple_associate_string(tuple, FIELD_ALBUM, NULL, string_value);
+                g_free(string_value);
+                aud_tuple_associate_int(tuple, FIELD_TRACK_NUMBER, NULL, track);
+                item = q_put(tuple, t, len);
 
-		{
-			Tuple *tuple = aud_tuple_new();
-			gchar* string_value;
+                aud_tuple_free(tuple);
+            }
 
-			string_value = xmms_urldecode_plain(artist);
-			aud_tuple_associate_string(tuple, FIELD_ARTIST, NULL, string_value);
-			g_free(string_value);
-
-			string_value = xmms_urldecode_plain(title);
-			aud_tuple_associate_string(tuple, FIELD_TITLE, NULL, string_value);
-			g_free(string_value);
-
-			string_value = xmms_urldecode_plain(album);
-			aud_tuple_associate_string(tuple, FIELD_ALBUM, NULL, string_value);
-			g_free(string_value);
-
-			item = q_put(tuple, atoi(len));
-
-			aud_tuple_free(tuple);
-		}
-
-		pdebug(fmt_vastr("a[%d]=%s t[%d]=%s l[%d]=%s i[%d]=%s m[%d]=%s b[%d]=%s",
-				i, I_ARTIST(item),
-				i, I_TITLE(item),
-				i, I_LEN(item),
-				i, I_TIME(item),
-				i, I_MB(item),
-				i, I_ALBUM(item)), DEBUG);
-		free(artist);
-		free(title);
-		free(len);
-		free(time);
-		free(album);
-		free(mb);
-
-		i++;
-	}
-	pdebug("Done loading cache.", DEBUG);
-	free(cache);
+            pdebug(fmt_vastr("a[%d]=%s t[%d]=%s l[%d]=%d i[%d]=%d m[%d]=%s b[%d]=%s",
+                             i, I_ARTIST(item),
+                             i, I_TITLE(item),
+                             i, I_LEN(item),
+                             i, I_TIME(item),
+                             i, I_MB(item),
+                             i, I_ALBUM(item)), DEBUG);
+            free(artist);
+            free(title);
+            free(album);
+            g_free(entry);
+            i++;
+        }
+    }
+    g_free(values);
+    g_free(cache);
+    pdebug("Done loading cache.", DEBUG);
 }
 
 static void dump_queue(void)
 {
-	FILE *fd;
-	item_t *item;
-	char *home, buf[PATH_MAX];
-	gchar* config_datadir;
+    FILE *fd;
+    item_t *item;
+    char *home, buf[PATH_MAX];
+    gchar* config_datadir;
 
-	/*pdebug("Entering dump_queue();", DEBUG);*/
+    /*pdebug("Entering dump_queue();", DEBUG);*/
 
-	if (!(home = getenv("HOME")))
-	{
-		pdebug("No HOME directory found. Cannot dump queue.", DEBUG);
-		return;
-	}
+    if (!(home = getenv("HOME")))
+    {
+        pdebug("No HOME directory found. Cannot dump queue.", DEBUG);
+        return;
+    }
 
-	config_datadir = audacious_get_localdir();
-	g_snprintf(buf, sizeof(buf), "%s/scrobblerqueue.txt", config_datadir);
-	g_free(config_datadir);
+    config_datadir = audacious_get_localdir();
+    g_snprintf(buf, sizeof(buf), "%s/scrobblerqueue.txt", config_datadir);
+    g_free(config_datadir);
 
-	if (!(fd = fopen(buf, "w")))
-	{
-		pdebug(fmt_vastr("Failure opening %s", buf), DEBUG);
-		return;
-	}
+    if (!(fd = fopen(buf, "w")))
+    {
+        pdebug(fmt_vastr("Failure opening %s", buf), DEBUG);
+        return;
+    }
 
-	pdebug(fmt_vastr("Opening %s", buf), DEBUG);
+    pdebug(fmt_vastr("Opening %s", buf), DEBUG);
 
-	q_peekall(1);
+    q_peekall(1);
 
-	while ((item = q_peekall(0))) {
-		fprintf(fd, "%s %s %s %s %s %s\n",
-					I_ARTIST(item),
-					I_TITLE(item),
-					I_LEN(item),
-					I_TIME(item),
-					I_ALBUM(item),
-					I_MB(item));
-	}
+    while ((item = q_peekall(0))) {
+        fprintf(fd, "%s\t%s\t%s\t%d\t%d\t%s\t%d\n",
+                    I_ARTIST(item),
+                    I_ALBUM(item),
+                    I_TITLE(item),
+                    item->track,
+                    I_LEN(item),
+                    "P",
+                    I_TIME(item));
+    }
 
-	fclose(fd);
+    fclose(fd);
 }
 
 /* This was made public */
@@ -1050,7 +1028,7 @@
 	g_mutex_lock(mutex);
 
 	sc_submit_np(tuple);
-	q_put(tuple, len);
+	q_put(tuple, time(NULL), len);
 
 	/*
 	 * This will help make sure the queue will be saved on a nasty