diff src/streambrowser/shoutcast.c @ 2735:6d6a3eb67510

some work on the streambrowser
author Calin Crisan ccrisan@gmail.com
date Tue, 01 Jul 2008 02:05:25 +0300
parents 28498c0bde64
children d608b16a710b
line wrap: on
line diff
--- a/src/streambrowser/shoutcast.c	Mon Jun 23 12:15:36 2008 +0300
+++ b/src/streambrowser/shoutcast.c	Tue Jul 01 02:05:25 2008 +0300
@@ -3,42 +3,41 @@
 #include <glib.h>
 #include <libxml/parser.h>
 #include <libxml/tree.h>
+#include <audacious/plugin.h>
 
 #include "streambrowser.h"
 #include "shoutcast.h"
 
 
-static streaminfo_t*		shoutcast_streaminfo_fetch(gchar *station_name, gchar *station_id);
-static category_t*		shoutcast_category_fetch(gchar *category_name);
-
-
-static streaminfo_t *shoutcast_streaminfo_fetch(gchar *station_name, gchar *station_id)
+gboolean shoutcast_category_fetch(category_t *category)
 {
 	gchar url[DEF_STRING_LEN];
-	g_snprintf(url, DEF_STRING_LEN, SHOUTCAST_STREAMINFO_URL, station_id);
-
-	streaminfo_t *streaminfo = streaminfo_new(station_name, url);
+	g_snprintf(url, DEF_STRING_LEN, SHOUTCAST_CATEGORY_URL, category->name);
 
-	// todo: read the .pls file fetched from the above url
-	
-	return streaminfo;
-}
+	/* generate a valid, temporary filename */
+	char *temp_filename = tempnam(NULL, "aud_sb");
+	if (temp_filename == NULL) {
+		failure("shoutcast: failed to create a temporary file\n");
+		return FALSE;
+	}
 
-static category_t *shoutcast_category_fetch(gchar *category_name)
-{
-	category_t *category = category_new(category_name);
+	char temp_pathname[DEF_STRING_LEN];
+	sprintf(temp_pathname, "file://%s", temp_filename);
+	free(temp_filename);
 
-	gchar url[DEF_STRING_LEN];
-	g_snprintf(url, DEF_STRING_LEN, SHOUTCAST_CATEGORY_URL, category_name);
+	debug("shoutcast: fetching category file '%s'\n", url);
+	if (!fetch_remote_to_local_file(url, temp_pathname))  {
+		failure("shoutcast: category file '%s' could not be downloaded to '%s'\n", url, temp_pathname);
+		return FALSE;
+	}
+	debug("shoutcast: category file '%s' successfuly downloaded to '%s'\n", url, temp_pathname);
 
-	xmlDoc *doc = xmlReadFile(url, NULL, 0);
+	xmlDoc *doc = xmlReadFile(temp_pathname, NULL, 0);
 	if (doc == NULL) {
-		error("    shoutcast: failed to read \"%s\" category file\n", category_name);
-		return NULL;
+		failure("shoutcast: failed to read '%s' category file\n", category->name);
+		return FALSE;
 	}
 	
-	debug("    shoutcast: category file fetched\n");
-
 	xmlNode *root_node = xmlDocGetRootElement(doc);
 	xmlNode *node;
 	
@@ -46,38 +45,61 @@
 
 	for (node = root_node; node != NULL; node = node->next) {
 		if (node->type == XML_ELEMENT_NODE && !strcmp((char *) node->name, "station")) {
-			gchar *station_name = (gchar*) xmlGetProp(node, (xmlChar *) "name");
-			gchar *station_id = (gchar*) xmlGetProp(node, (xmlChar *) "id");
+			gchar *streaminfo_name = (gchar*) xmlGetProp(node, (xmlChar *) "name");
+			gchar *streaminfo_id = (gchar*) xmlGetProp(node, (xmlChar *) "id");
+			gchar streaminfo_playlist_url[DEF_STRING_LEN];
+			gchar *streaminfo_current_track = (gchar*) xmlGetProp(node, (xmlChar *) "ct");
+			
+			g_snprintf(streaminfo_playlist_url, DEF_STRING_LEN, SHOUTCAST_STREAMINFO_URL, streaminfo_id);
 
-			debug("        shoutcast: fetching stream info for name = \"%s\" and id = %s\n", station_name, station_id);
+			debug("shoutcast: fetching stream info for '%s/%d' from '%s'\n", streaminfo_name, streaminfo_id, url);
 
-			streaminfo_t *streaminfo = shoutcast_streaminfo_fetch(station_name, station_id);
+			streaminfo_t *streaminfo = streaminfo_new(streaminfo_name, streaminfo_playlist_url, streaminfo_current_track);
 			streaminfo_add(category, streaminfo);
-	
-			// todo: debug - print info about streaminfon urls		
-			debug("        shoutcast: stream info added for name = \"%s\" and id = %s\n", station_name, station_id);
+			
+			xmlFree(streaminfo_name);
+			xmlFree(streaminfo_id);
+			xmlFree(streaminfo_current_track);
+
+			debug("shoutcast: stream info added\n");
 		}
 	}
 	
-	return category;
+	remove(temp_filename);
+	// todo: free the mallocs()
+	
+	return TRUE;
 }
 
 
 streamdir_t* shoutcast_streamdir_fetch()
 {
-	streamdir_t *streamdir = streamdir_new("Shoutcast");
+	streamdir_t *streamdir = streamdir_new(SHOUTCAST_NAME);
 	
-	debug("shoutcast: fetching streaming directory file\n");
-
-	// todo: replace dummy filename with SHOUTCAST_DIRECTORY_URL
-	xmlDoc *doc = xmlReadFile("shoutcast.xml", NULL, 0);
-	if (doc == NULL) {
-		error("shoutcast: failed to read stream directory file\n");
+	/* generate a valid, temporary filename */
+	char *temp_filename = tempnam(NULL, "aud_sb");
+	if (temp_filename == NULL) {
+		failure("shoutcast: failed to create a temporary file\n");
 		return NULL;
 	}
 	
-	debug("shoutcast: streaming directory file fetched\n");
+	char temp_pathname[DEF_STRING_LEN];
+	sprintf(temp_pathname, "file://%s", temp_filename);
+	free(temp_filename);
+	
+	debug("shoutcast: fetching streaming directory file '%s'\n", SHOUTCAST_STREAMDIR_URL);
+	if (!fetch_remote_to_local_file(SHOUTCAST_STREAMDIR_URL, temp_pathname)) {
+		failure("shoutcast: stream directory file '%s' could not be downloaded to '%s'\n", SHOUTCAST_STREAMDIR_URL, temp_pathname);
+		return NULL;
+	}
+	debug("shoutcast: stream directory file '%s' successfuly downloaded to '%s'\n", SHOUTCAST_STREAMDIR_URL, temp_pathname);
 
+	xmlDoc *doc = xmlReadFile(temp_pathname, NULL, 0);
+	if (doc == NULL) {
+		failure("shoutcast: failed to read stream directory file\n");
+		return NULL;
+	}
+	
 	xmlNode *root_node = xmlDocGetRootElement(doc);
 	xmlNode *node;
 	
@@ -87,15 +109,22 @@
 		if (node->type == XML_ELEMENT_NODE) {
 			gchar *category_name = (gchar*) xmlGetProp(node, (xmlChar *) "name");
 			
-			debug("    shoutcast: fetching category \"%s\"\n", category_name);
+			debug("shoutcast: fetching category '%s'\n", category_name);
 
-			category_t *category = shoutcast_category_fetch(category_name);
+			category_t *category = category_new(category_name);
 			category_add(streamdir, category);
 			
-			debug("    shoutcast: added category \"%s\"\n", category_name);
+			xmlFree(category_name);
+			
+			debug("shoutcast: category added\n", category_name);
 		}
 	}
+
+	// todo: free the mallocs()
 	
-	exit(0);
+	remove(temp_filename);
+	debug("shoutcast: streaming directory successfuly loaded\n");
+
+	return streamdir;
 }