comparison 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
comparison
equal deleted inserted replaced
2727:c68fadbad6d8 2735:6d6a3eb67510
1 1
2 #include <string.h> 2 #include <string.h>
3 #include <glib.h> 3 #include <glib.h>
4 #include <libxml/parser.h> 4 #include <libxml/parser.h>
5 #include <libxml/tree.h> 5 #include <libxml/tree.h>
6 #include <audacious/plugin.h>
6 7
7 #include "streambrowser.h" 8 #include "streambrowser.h"
8 #include "shoutcast.h" 9 #include "shoutcast.h"
9 10
10 11
11 static streaminfo_t* shoutcast_streaminfo_fetch(gchar *station_name, gchar *station_id); 12 gboolean shoutcast_category_fetch(category_t *category)
12 static category_t* shoutcast_category_fetch(gchar *category_name);
13
14
15 static streaminfo_t *shoutcast_streaminfo_fetch(gchar *station_name, gchar *station_id)
16 { 13 {
17 gchar url[DEF_STRING_LEN]; 14 gchar url[DEF_STRING_LEN];
18 g_snprintf(url, DEF_STRING_LEN, SHOUTCAST_STREAMINFO_URL, station_id); 15 g_snprintf(url, DEF_STRING_LEN, SHOUTCAST_CATEGORY_URL, category->name);
19 16
20 streaminfo_t *streaminfo = streaminfo_new(station_name, url); 17 /* generate a valid, temporary filename */
18 char *temp_filename = tempnam(NULL, "aud_sb");
19 if (temp_filename == NULL) {
20 failure("shoutcast: failed to create a temporary file\n");
21 return FALSE;
22 }
21 23
22 // todo: read the .pls file fetched from the above url 24 char temp_pathname[DEF_STRING_LEN];
23 25 sprintf(temp_pathname, "file://%s", temp_filename);
24 return streaminfo; 26 free(temp_filename);
25 }
26 27
27 static category_t *shoutcast_category_fetch(gchar *category_name) 28 debug("shoutcast: fetching category file '%s'\n", url);
28 { 29 if (!fetch_remote_to_local_file(url, temp_pathname)) {
29 category_t *category = category_new(category_name); 30 failure("shoutcast: category file '%s' could not be downloaded to '%s'\n", url, temp_pathname);
31 return FALSE;
32 }
33 debug("shoutcast: category file '%s' successfuly downloaded to '%s'\n", url, temp_pathname);
30 34
31 gchar url[DEF_STRING_LEN]; 35 xmlDoc *doc = xmlReadFile(temp_pathname, NULL, 0);
32 g_snprintf(url, DEF_STRING_LEN, SHOUTCAST_CATEGORY_URL, category_name);
33
34 xmlDoc *doc = xmlReadFile(url, NULL, 0);
35 if (doc == NULL) { 36 if (doc == NULL) {
36 error(" shoutcast: failed to read \"%s\" category file\n", category_name); 37 failure("shoutcast: failed to read '%s' category file\n", category->name);
37 return NULL; 38 return FALSE;
38 } 39 }
39 40
40 debug(" shoutcast: category file fetched\n");
41
42 xmlNode *root_node = xmlDocGetRootElement(doc); 41 xmlNode *root_node = xmlDocGetRootElement(doc);
43 xmlNode *node; 42 xmlNode *node;
44 43
45 root_node = root_node->children; 44 root_node = root_node->children;
46 45
47 for (node = root_node; node != NULL; node = node->next) { 46 for (node = root_node; node != NULL; node = node->next) {
48 if (node->type == XML_ELEMENT_NODE && !strcmp((char *) node->name, "station")) { 47 if (node->type == XML_ELEMENT_NODE && !strcmp((char *) node->name, "station")) {
49 gchar *station_name = (gchar*) xmlGetProp(node, (xmlChar *) "name"); 48 gchar *streaminfo_name = (gchar*) xmlGetProp(node, (xmlChar *) "name");
50 gchar *station_id = (gchar*) xmlGetProp(node, (xmlChar *) "id"); 49 gchar *streaminfo_id = (gchar*) xmlGetProp(node, (xmlChar *) "id");
50 gchar streaminfo_playlist_url[DEF_STRING_LEN];
51 gchar *streaminfo_current_track = (gchar*) xmlGetProp(node, (xmlChar *) "ct");
52
53 g_snprintf(streaminfo_playlist_url, DEF_STRING_LEN, SHOUTCAST_STREAMINFO_URL, streaminfo_id);
51 54
52 debug(" shoutcast: fetching stream info for name = \"%s\" and id = %s\n", station_name, station_id); 55 debug("shoutcast: fetching stream info for '%s/%d' from '%s'\n", streaminfo_name, streaminfo_id, url);
53 56
54 streaminfo_t *streaminfo = shoutcast_streaminfo_fetch(station_name, station_id); 57 streaminfo_t *streaminfo = streaminfo_new(streaminfo_name, streaminfo_playlist_url, streaminfo_current_track);
55 streaminfo_add(category, streaminfo); 58 streaminfo_add(category, streaminfo);
56 59
57 // todo: debug - print info about streaminfon urls 60 xmlFree(streaminfo_name);
58 debug(" shoutcast: stream info added for name = \"%s\" and id = %s\n", station_name, station_id); 61 xmlFree(streaminfo_id);
62 xmlFree(streaminfo_current_track);
63
64 debug("shoutcast: stream info added\n");
59 } 65 }
60 } 66 }
61 67
62 return category; 68 remove(temp_filename);
69 // todo: free the mallocs()
70
71 return TRUE;
63 } 72 }
64 73
65 74
66 streamdir_t* shoutcast_streamdir_fetch() 75 streamdir_t* shoutcast_streamdir_fetch()
67 { 76 {
68 streamdir_t *streamdir = streamdir_new("Shoutcast"); 77 streamdir_t *streamdir = streamdir_new(SHOUTCAST_NAME);
69 78
70 debug("shoutcast: fetching streaming directory file\n"); 79 /* generate a valid, temporary filename */
71 80 char *temp_filename = tempnam(NULL, "aud_sb");
72 // todo: replace dummy filename with SHOUTCAST_DIRECTORY_URL 81 if (temp_filename == NULL) {
73 xmlDoc *doc = xmlReadFile("shoutcast.xml", NULL, 0); 82 failure("shoutcast: failed to create a temporary file\n");
74 if (doc == NULL) {
75 error("shoutcast: failed to read stream directory file\n");
76 return NULL; 83 return NULL;
77 } 84 }
78 85
79 debug("shoutcast: streaming directory file fetched\n"); 86 char temp_pathname[DEF_STRING_LEN];
87 sprintf(temp_pathname, "file://%s", temp_filename);
88 free(temp_filename);
89
90 debug("shoutcast: fetching streaming directory file '%s'\n", SHOUTCAST_STREAMDIR_URL);
91 if (!fetch_remote_to_local_file(SHOUTCAST_STREAMDIR_URL, temp_pathname)) {
92 failure("shoutcast: stream directory file '%s' could not be downloaded to '%s'\n", SHOUTCAST_STREAMDIR_URL, temp_pathname);
93 return NULL;
94 }
95 debug("shoutcast: stream directory file '%s' successfuly downloaded to '%s'\n", SHOUTCAST_STREAMDIR_URL, temp_pathname);
80 96
97 xmlDoc *doc = xmlReadFile(temp_pathname, NULL, 0);
98 if (doc == NULL) {
99 failure("shoutcast: failed to read stream directory file\n");
100 return NULL;
101 }
102
81 xmlNode *root_node = xmlDocGetRootElement(doc); 103 xmlNode *root_node = xmlDocGetRootElement(doc);
82 xmlNode *node; 104 xmlNode *node;
83 105
84 root_node = root_node->children; 106 root_node = root_node->children;
85 107
86 for (node = root_node; node != NULL; node = node->next) { 108 for (node = root_node; node != NULL; node = node->next) {
87 if (node->type == XML_ELEMENT_NODE) { 109 if (node->type == XML_ELEMENT_NODE) {
88 gchar *category_name = (gchar*) xmlGetProp(node, (xmlChar *) "name"); 110 gchar *category_name = (gchar*) xmlGetProp(node, (xmlChar *) "name");
89 111
90 debug(" shoutcast: fetching category \"%s\"\n", category_name); 112 debug("shoutcast: fetching category '%s'\n", category_name);
91 113
92 category_t *category = shoutcast_category_fetch(category_name); 114 category_t *category = category_new(category_name);
93 category_add(streamdir, category); 115 category_add(streamdir, category);
94 116
95 debug(" shoutcast: added category \"%s\"\n", category_name); 117 xmlFree(category_name);
118
119 debug("shoutcast: category added\n", category_name);
96 } 120 }
97 } 121 }
122
123 // todo: free the mallocs()
98 124
99 exit(0); 125 remove(temp_filename);
126 debug("shoutcast: streaming directory successfuly loaded\n");
127
128 return streamdir;
100 } 129 }
101 130