comparison src/lastfm/lastfm.c @ 976:c1bda49a5039 trunk

[svn] - fix indentation
author nenolod
date Sun, 22 Apr 2007 17:23:29 -0700
parents 8bdd0d76a0ca
children 3513bc2fd738
comparison
equal deleted inserted replaced
975:8bdd0d76a0ca 976:c1bda49a5039
24 #include <glib.h> 24 #include <glib.h>
25 25
26 26
27 #define LASTFM_HANDSHAKE_URL "http://ws.audioscrobbler.com/radio/handshake.php?version=1.1.1&platform=linux&username=%s&passwordmd5=%s&debug=0&language=jp" 27 #define LASTFM_HANDSHAKE_URL "http://ws.audioscrobbler.com/radio/handshake.php?version=1.1.1&platform=linux&username=%s&passwordmd5=%s&debug=0&language=jp"
28 #define LASTFM_ADJUST_URL "http://ws.audioscrobbler.com/radio/adjust.php?session=%s&url=%s&debug=0" 28 #define LASTFM_ADJUST_URL "http://ws.audioscrobbler.com/radio/adjust.php?session=%s&url=%s&debug=0"
29 #define LASTFM_CURL_TIMEOUT 5 29 #define LASTFM_CURL_TIMEOUT 5
30 //#define DEBUG 1 30 //#define DEBUG 1
31 31
32 typedef struct { 32 typedef struct
33 VFSFile * proxy_fd; 33 {
34 gchar *lastfm_session_id; 34 VFSFile *proxy_fd;
35 gchar *lastfm_mp3_stream_url; 35 gchar *lastfm_session_id;
36 gchar *lastfm_station_name; 36 gchar *lastfm_mp3_stream_url;
37 int login_count; 37 gchar *lastfm_station_name;
38 int login_count;
38 } LastFM; 39 } LastFM;
39 40
40 LastFM *LastFMGlobalData; 41 LastFM *LastFMGlobalData;
41 /*this keeps the login data in a global place 42 /*this keeps the login data in a global place
42 since we cannot login on every fopen call 43 since we cannot login on every fopen call
43 if anyone has a better solution to this any help is welcome*/ 44 if anyone has a better solution to this any help is welcome */
44 45
45 static size_t lastfm_store_res(void *ptr, size_t size, size_t nmemb, void *udata) 46 static size_t lastfm_store_res(void *ptr, size_t size, size_t nmemb, void *udata)
46 { 47 {
47 GString *data = (GString *) udata; 48 GString *data = (GString *) udata;
48 g_string_append_len(data, ptr, nmemb); 49 g_string_append_len(data, ptr, nmemb);
51 52
52 53
53 static gboolean lastfm_login() 54 static gboolean lastfm_login()
54 { 55 {
55 /*gets the session ID in lastfm_session_id and returns the URL to be played 56 /*gets the session ID in lastfm_session_id and returns the URL to be played
56 read http://gabistapler.de/blog/index.php?/archives/268-Play-last.fm-streams-without-the-player.html for more info 57 read http://gabistapler.de/blog/index.php?/archives/268-Play-last.fm-streams-without-the-player.html for more info
57 */ 58 */
58 // LastFM *LastFMData = g_new0(LastFM,1); 59 // LastFM *LastFMData = g_new0(LastFM,1);
59 gint status, i; 60 gint status, i;
60 gchar buf[4096], **split = NULL; 61 gchar buf[4096], **split = NULL;
61 GString *res = g_string_new(NULL); 62 GString *res = g_string_new(NULL);
62 ConfigDb *cfgfile = NULL; 63 ConfigDb *cfgfile = NULL;
63 char *username = NULL, *password = NULL; 64 char *username = NULL, *password = NULL;
64 CURL*curl; 65 CURL *curl;
65 if ((cfgfile = bmp_cfg_db_open()) != NULL) 66 if ((cfgfile = bmp_cfg_db_open()) != NULL)
66 { 67 {
67 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "username", &username); 68 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "username", &username);
68 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "password", &password); 69 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "password", &password);
69 //puts(username); 70 //puts(username);
70 //puts(password); 71 //puts(password);
71 72
72 g_free(cfgfile); 73 g_free(cfgfile);
73 } 74 }
74 75
75 if (username != NULL && password != NULL){ 76 if (username != NULL && password != NULL)
77 {
76 78
77 snprintf(buf, sizeof(buf), LASTFM_HANDSHAKE_URL, username, password); 79 snprintf(buf, sizeof(buf), LASTFM_HANDSHAKE_URL, username, password);
78 // g_free(password); 80 // g_free(password);
79 // g_free(username); 81 // g_free(username);
80 } 82 }
81 else 83 else
82 return FALSE; 84 return FALSE;
83 85
84 puts("preparing curl"); 86 puts("preparing curl");
85 curl=curl_easy_init(); 87 curl = curl_easy_init();
86 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); 88 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
87 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious"); 89 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious");
88 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res); 90 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res);
89 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 91 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
90 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); 92 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
91 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, LASTFM_CURL_TIMEOUT); 93 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, LASTFM_CURL_TIMEOUT);
92 curl_easy_setopt(curl, CURLOPT_URL, buf); 94 curl_easy_setopt(curl, CURLOPT_URL, buf);
93 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res); 95 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res);
94 status = curl_easy_perform(curl); 96 status = curl_easy_perform(curl);
95 curl_easy_cleanup(curl); 97 curl_easy_cleanup(curl);
96 98
97 puts("curl is done"); 99 puts("curl is done");
98 puts(buf); 100 puts(buf);
99 puts(res->str); 101 puts(res->str);
100 if (status == CURLE_OK) 102 if (status == CURLE_OK)
107 LastFMGlobalData->lastfm_session_id = g_strndup(split[i] + 8, 32); 109 LastFMGlobalData->lastfm_session_id = g_strndup(split[i] + 8, 32);
108 else if (g_str_has_prefix(split[i], "stream_url=")) 110 else if (g_str_has_prefix(split[i], "stream_url="))
109 LastFMGlobalData->lastfm_mp3_stream_url = g_strdup(split[i] + 11); 111 LastFMGlobalData->lastfm_mp3_stream_url = g_strdup(split[i] + 11);
110 } 112 }
111 } 113 }
112 else 114 else
113 return FALSE; 115 return FALSE;
114 116
115 // printf("URL: '%s'\n",LastFMGlobalData->lastfm_mp3_stream_url); 117 // printf("URL: '%s'\n",LastFMGlobalData->lastfm_mp3_stream_url);
116 // printf("session_id: '%s'\n",LastFMGlobalData->lastfm_session_id); 118 // printf("session_id: '%s'\n",LastFMGlobalData->lastfm_session_id);
117 119
118 g_strfreev(split); 120 g_strfreev(split);
119 g_string_erase(res, 0, -1); 121 g_string_erase(res, 0, -1);
120 return (gboolean)TRUE; 122 return (gboolean) TRUE;
121 } 123 }
122 124
123 static gboolean lastfm_adjust(const gchar * url) 125 static gboolean lastfm_adjust(const gchar * url)
124 { 126 {
125 LastFM * LastFMData= g_new0(LastFM,1); 127 LastFM *LastFMData = g_new0(LastFM, 1);
126 128
127 int status, i; 129 int status, i;
128 gchar tmp[4096], **split = NULL; 130 gchar tmp[4096], **split = NULL;
129 gboolean ret = FALSE; 131 gboolean ret = FALSE;
130 GString *res = g_string_new(NULL); 132 GString *res = g_string_new(NULL);
131 CURL*curl; 133 CURL *curl;
132 if(LastFMGlobalData->lastfm_session_id==NULL) 134 if (LastFMGlobalData->lastfm_session_id == NULL)
133 return FALSE; 135 return FALSE;
134 snprintf(tmp, sizeof(tmp), LASTFM_ADJUST_URL,LastFMGlobalData->lastfm_session_id, url); 136 snprintf(tmp, sizeof(tmp), LASTFM_ADJUST_URL, LastFMGlobalData->lastfm_session_id, url);
135 puts("test1"); 137 puts("test1");
136 138
137 139
138 curl=curl_easy_init(); 140 curl = curl_easy_init();
139 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); 141 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
140 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious"); 142 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Audacious");
141 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res); 143 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_store_res);
142 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 144 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
143 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); 145 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
144 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, LASTFM_CURL_TIMEOUT); 146 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, LASTFM_CURL_TIMEOUT);
145 curl_easy_setopt(curl, CURLOPT_URL, tmp); 147 curl_easy_setopt(curl, CURLOPT_URL, tmp);
146 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res); 148 curl_easy_setopt(curl, CURLOPT_WRITEDATA, res);
147 status = curl_easy_perform(curl); 149 status = curl_easy_perform(curl);
148 curl_easy_cleanup(curl); 150 curl_easy_cleanup(curl);
149 151
150 152
151 puts ("Adjust received data:"); 153 puts("Adjust received data:");
152 puts(res->str); 154 puts(res->str);
153 155
154 if (status == CURLE_OK) 156 if (status == CURLE_OK)
155 { 157 {
156 split = g_strsplit(res->str, "\n", 3); 158 split = g_strsplit(res->str, "\n", 3);
158 for (i = 0; split && split[i]; i++) 160 for (i = 0; split && split[i]; i++)
159 { 161 {
160 if (g_str_has_prefix(split[i], "response=OK")) 162 if (g_str_has_prefix(split[i], "response=OK"))
161 ret = TRUE; 163 ret = TRUE;
162 if (g_str_has_prefix(split[i], "stationname=")) 164 if (g_str_has_prefix(split[i], "stationname="))
163 {LastFMGlobalData->lastfm_station_name = g_strdup(split[i] + 12); 165 {
164 puts("StationnName:"); 166 LastFMGlobalData->lastfm_station_name = g_strdup(split[i] + 12);
165 puts( LastFMGlobalData->lastfm_station_name); 167 puts("StationnName:");
166 } 168 puts(LastFMGlobalData->lastfm_station_name);
169 }
167 } 170 }
168 171
169 g_strfreev(split); 172 g_strfreev(split);
170 } 173 }
171 g_string_erase(res, 0, -1); 174 g_string_erase(res, 0, -1);
172 175
173 return ret; 176 return ret;
174 } 177 }
175 178
176 179
177 VFSFile *lastfm_vfs_fopen_impl(const gchar * path, const gchar * mode) 180 VFSFile *lastfm_vfs_fopen_impl(const gchar * path, const gchar * mode)
178 { 181 {
179 VFSFile *file; 182 VFSFile *file;
180 LastFM *handle; 183 LastFM *handle;
181 file = g_new0(VFSFile, 1); 184 file = g_new0(VFSFile, 1);
182 handle = g_new0(LastFM, 1); 185 handle = g_new0(LastFM, 1);
183 186
184 puts("Starting fopen"); 187 puts("Starting fopen");
185 188
186 while ((LastFMGlobalData->lastfm_mp3_stream_url==NULL) 189 while ((LastFMGlobalData->lastfm_mp3_stream_url == NULL) && (LastFMGlobalData->login_count <= 3))
187 &&(LastFMGlobalData->login_count<=3)) 190 {
188 { 191 printf("Login try count: %d\n", LastFMGlobalData->login_count++);
189 printf("Login try count: %d\n",LastFMGlobalData->login_count++); 192 lastfm_login();
190 lastfm_login(); 193 if (LastFMGlobalData->lastfm_mp3_stream_url == NULL)
191 if(LastFMGlobalData->lastfm_mp3_stream_url==NULL) 194 sleep(5);
192 sleep(5); 195
193 196 }
194 } 197 handle->lastfm_mp3_stream_url = g_strdup(LastFMGlobalData->lastfm_mp3_stream_url);
195 handle->lastfm_mp3_stream_url=g_strdup(LastFMGlobalData->lastfm_mp3_stream_url); 198 handle->lastfm_session_id = g_strdup(LastFMGlobalData->lastfm_session_id);
196 handle->lastfm_session_id=g_strdup(LastFMGlobalData->lastfm_session_id); 199 handle->lastfm_station_name = g_strdup(LastFMGlobalData->lastfm_station_name);
197 handle->lastfm_station_name=g_strdup(LastFMGlobalData->lastfm_station_name); 200
198 201 if (lastfm_adjust(path))
199 if (lastfm_adjust(path))
200 202
201 printf("Tuning completed OK\n"); 203 printf("Tuning completed OK\n");
202 else 204 else
203 puts("Cannot tune to given channel"); 205 puts("Cannot tune to given channel");
204 206
205 handle->proxy_fd = vfs_fopen(handle->lastfm_mp3_stream_url, mode); 207 handle->proxy_fd = vfs_fopen(handle->lastfm_mp3_stream_url, mode);
206 file->handle = handle; 208 file->handle = handle;
207 209
208 210
209 puts("Returning from fopen"); 211 puts("Returning from fopen");
210 return file; 212 return file;
211 } 213 }
212 214
213 gint lastfm_vfs_fclose_impl(VFSFile * file) 215 gint lastfm_vfs_fclose_impl(VFSFile * file)
214 { 216 {
215 gint ret = 0; 217 gint ret = 0;
216 218
217 219
218 if (file == NULL) 220 if (file == NULL)
219 return -1; 221 return -1;
220 222
221 LastFM *handle = file->handle; 223 LastFM *handle = file->handle;
222 ret = vfs_fclose(handle->proxy_fd); 224 ret = vfs_fclose(handle->proxy_fd);
223 225
284 off_t lastfm_vfs_fsize_impl(VFSFile * file) 286 off_t lastfm_vfs_fsize_impl(VFSFile * file)
285 { 287 {
286 return -1; 288 return -1;
287 } 289 }
288 290
289 gchar *lastfm_vfs_metadata_impl(VFSFile * file, const gchar *key) 291 gchar *lastfm_vfs_metadata_impl(VFSFile * file, const gchar * key)
290 { 292 {
291 LastFM *handle = file->handle; 293 LastFM *handle = file->handle;
292 294
293 if (!strcasecmp(key, "stream-name")) 295 if (!strcasecmp(key, "stream-name"))
294 return g_strdup(handle->lastfm_station_name); 296 return g_strdup(handle->lastfm_station_name);
295 297
296 return NULL; 298 return NULL;
297 } 299 }
298 300
319 vfs_register_transport(&lastfm_const); 321 vfs_register_transport(&lastfm_const);
320 } 322 }
321 323
322 static void cleanup(void) 324 static void cleanup(void)
323 { 325 {
324 g_free(LastFMGlobalData); 326 g_free(LastFMGlobalData);
325 #if 0 327 #if 0
326 vfs_unregister_transport(&default_const); 328 vfs_unregister_transport(&default_const);
327 vfs_unregister_transport(&file_const); 329 vfs_unregister_transport(&file_const);
328 #endif 330 #endif
329 } 331 }