comparison src/streambrowser/shoutcast.c @ 2848:fbb32674bfd2

click on a stream updates; more comments; more proper stream icons
author Calin Crisan ccrisan@gmail.com
date Mon, 28 Jul 2008 23:43:31 +0300
parents cc6f02424609
children c27da2c06805
comparison
equal deleted inserted replaced
2845:d0cbf303869b 2848:fbb32674bfd2
25 25
26 #include "streambrowser.h" 26 #include "streambrowser.h"
27 #include "shoutcast.h" 27 #include "shoutcast.h"
28 28
29 29
30 gboolean shoutcast_category_fetch(category_t *category) 30 gboolean shoutcast_streaminfo_fetch(category_t *category, streaminfo_t *streaminfo)
31 { 31 {
32 gchar url[DEF_STRING_LEN]; 32 gchar url[DEF_STRING_LEN];
33 g_snprintf(url, DEF_STRING_LEN, SHOUTCAST_CATEGORY_URL, category->name); 33 g_snprintf(url, DEF_STRING_LEN, SHOUTCAST_CATEGORY_URL, category->name);
34 34
35 /* generate a valid, temporary filename */ 35 /* generate a valid, temporary filename */
54 if (doc == NULL) { 54 if (doc == NULL) {
55 failure("shoutcast: failed to read '%s' category file\n", category->name); 55 failure("shoutcast: failed to read '%s' category file\n", category->name);
56 free(temp_filename); 56 free(temp_filename);
57 return FALSE; 57 return FALSE;
58 } 58 }
59
60 /* free/remove any existing streaminfos in this category */
61 while (streaminfo_get_count(category) > 0)
62 streaminfo_remove(category, streaminfo_get_by_index(category, 0));
63 59
64 xmlNode *root_node = xmlDocGetRootElement(doc); 60 xmlNode *root_node = xmlDocGetRootElement(doc);
65 xmlNode *node; 61 xmlNode *node;
66 62
67 root_node = root_node->children; 63 root_node = root_node->children;
72 gchar *streaminfo_id = (gchar*) xmlGetProp(node, (xmlChar *) "id"); 68 gchar *streaminfo_id = (gchar*) xmlGetProp(node, (xmlChar *) "id");
73 gchar streaminfo_playlist_url[DEF_STRING_LEN]; 69 gchar streaminfo_playlist_url[DEF_STRING_LEN];
74 gchar *streaminfo_current_track = (gchar*) xmlGetProp(node, (xmlChar *) "ct"); 70 gchar *streaminfo_current_track = (gchar*) xmlGetProp(node, (xmlChar *) "ct");
75 71
76 g_snprintf(streaminfo_playlist_url, DEF_STRING_LEN, SHOUTCAST_STREAMINFO_URL, streaminfo_id); 72 g_snprintf(streaminfo_playlist_url, DEF_STRING_LEN, SHOUTCAST_STREAMINFO_URL, streaminfo_id);
77 73
78 debug("shoutcast: fetching stream info for '%s/%d' from '%s'\n", streaminfo_name, streaminfo_id, url); 74 if (strncmp(streaminfo_playlist_url, streaminfo->playlist_url, DEF_STRING_LEN) == 0) {
75 debug("shoutcast: updating stream info for '%s/%d' from '%s'\n", streaminfo_name, streaminfo_id, url);
76
77 strcpy(streaminfo->name, streaminfo_name);
78 strcpy(streaminfo->playlist_url, streaminfo_playlist_url);
79 strcpy(streaminfo->current_track, streaminfo_current_track);
80
81 xmlFree(streaminfo_name);
82 xmlFree(streaminfo_id);
83 xmlFree(streaminfo_current_track);
84
85 debug("shoutcast: stream info added\n");
86
87 break;
88 }
89 }
90 }
91
92 xmlFreeDoc(doc);
93
94 if (remove(temp_filename) != 0) {
95 failure("shoutcast: cannot remove the temporary file: %s\n", strerror(errno));
96 }
97 free(temp_filename);
98
99 return TRUE;
100 }
101
102 gboolean shoutcast_category_fetch(category_t *category)
103 {
104 gchar url[DEF_STRING_LEN];
105 g_snprintf(url, DEF_STRING_LEN, SHOUTCAST_CATEGORY_URL, category->name);
106
107 /* generate a valid, temporary filename */
108 char *temp_filename = tempnam(NULL, "aud_sb");
109 if (temp_filename == NULL) {
110 failure("shoutcast: failed to create a temporary file\n");
111 return FALSE;
112 }
113
114 char temp_pathname[DEF_STRING_LEN];
115 sprintf(temp_pathname, "file://%s", temp_filename);
116
117 debug("shoutcast: fetching category file '%s'\n", url);
118 if (!fetch_remote_to_local_file(url, temp_pathname)) {
119 failure("shoutcast: category file '%s' could not be downloaded to '%s'\n", url, temp_pathname);
120 free(temp_filename);
121 return FALSE;
122 }
123 debug("shoutcast: category file '%s' successfuly downloaded to '%s'\n", url, temp_pathname);
124
125 xmlDoc *doc = xmlReadFile(temp_pathname, NULL, 0);
126 if (doc == NULL) {
127 failure("shoutcast: failed to read '%s' category file\n", category->name);
128 free(temp_filename);
129 return FALSE;
130 }
131
132 /* free/remove any existing streaminfos in this category */
133 while (streaminfo_get_count(category) > 0)
134 streaminfo_remove(category, streaminfo_get_by_index(category, 0));
135
136 xmlNode *root_node = xmlDocGetRootElement(doc);
137 xmlNode *node;
138
139 root_node = root_node->children;
140
141 for (node = root_node; node != NULL; node = node->next) {
142 if (node->type == XML_ELEMENT_NODE && !strcmp((char *) node->name, "station")) {
143 gchar *streaminfo_name = (gchar*) xmlGetProp(node, (xmlChar *) "name");
144 gchar *streaminfo_id = (gchar*) xmlGetProp(node, (xmlChar *) "id");
145 gchar streaminfo_playlist_url[DEF_STRING_LEN];
146 gchar *streaminfo_current_track = (gchar*) xmlGetProp(node, (xmlChar *) "ct");
147
148 g_snprintf(streaminfo_playlist_url, DEF_STRING_LEN, SHOUTCAST_STREAMINFO_URL, streaminfo_id);
149
150 debug("shoutcast: adding stream info for '%s/%d' from '%s'\n", streaminfo_name, streaminfo_id, url);
79 151
80 streaminfo_t *streaminfo = streaminfo_new(streaminfo_name, streaminfo_playlist_url, "", streaminfo_current_track); 152 streaminfo_t *streaminfo = streaminfo_new(streaminfo_name, streaminfo_playlist_url, "", streaminfo_current_track);
81 streaminfo_add(category, streaminfo); 153 streaminfo_add(category, streaminfo);
82 154
83 xmlFree(streaminfo_name); 155 xmlFree(streaminfo_name);