comparison src/scrobbler/plugin.c @ 350:b4f1ad07fcd9 trunk

[svn] - add support for hatena (experimental)
author nenolod
date Sat, 09 Dec 2006 03:37:53 -0800
parents 76047737ea49
children 1bd205b6e83d
comparison
equal deleted inserted replaced
349:8879027752ef 350:b4f1ad07fcd9
21 #include <wchar.h> 21 #include <wchar.h>
22 #include <sys/time.h> 22 #include <sys/time.h>
23 23
24 #include "scrobbler.h" 24 #include "scrobbler.h"
25 #include "gerpok.h" 25 #include "gerpok.h"
26 #include "hatena.h"
26 #include "gtkstuff.h" 27 #include "gtkstuff.h"
27 #include "config.h" 28 #include "config.h"
28 #include "fmt.h" 29 #include "fmt.h"
29 #include "configure.h" 30 #include "configure.h"
30 31
37 38
38 static void init(void); 39 static void init(void);
39 static void cleanup(void); 40 static void cleanup(void);
40 static void *xs_thread(void *); 41 static void *xs_thread(void *);
41 static void *hs_thread(void *); 42 static void *hs_thread(void *);
42 static int sc_going, ge_going; 43 static int sc_going, ge_going, ha_going;
43 static GtkWidget *cfgdlg; 44 static GtkWidget *cfgdlg;
44 45
45 static GThread *pt_scrobbler; 46 static GThread *pt_scrobbler;
46 static GMutex *m_scrobbler; 47 static GMutex *m_scrobbler;
47 static GThread *pt_handshake; 48 static GThread *pt_handshake;
60 61
61 static void init(void) 62 static void init(void)
62 { 63 {
63 char *username = NULL, *password = NULL; 64 char *username = NULL, *password = NULL;
64 char *ge_username = NULL, *ge_password = NULL; 65 char *ge_username = NULL, *ge_password = NULL;
66 char *ha_username = NULL, *ha_password = NULL;
65 ConfigDb *cfgfile; 67 ConfigDb *cfgfile;
66 sc_going = 1; 68 sc_going = 1;
67 ge_going = 1; 69 ge_going = 1;
68 GError **moo = NULL; 70 GError **moo = NULL;
69 cfgdlg = create_cfgdlg(); 71 cfgdlg = create_cfgdlg();
77 &password); 79 &password);
78 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "ge_username", 80 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "ge_username",
79 &ge_username); 81 &ge_username);
80 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "ge_password", 82 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "ge_password",
81 &ge_password); 83 &ge_password);
84 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "ha_username",
85 &ha_username);
86 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "ha_password",
87 &ha_password);
82 bmp_cfg_db_close(cfgfile); 88 bmp_cfg_db_close(cfgfile);
83 } 89 }
84 90
85 if ((!username || !password) || (!*username || !*password)) 91 if ((!username || !password) || (!*username || !*password))
86 { 92 {
104 gerpok_sc_init(ge_username, ge_password); 110 gerpok_sc_init(ge_username, ge_password);
105 111
106 g_free(ge_username); 112 g_free(ge_username);
107 g_free(ge_password); 113 g_free(ge_password);
108 114
115 if ((!ha_username || !ha_password) || (!*ha_username || !*ha_password))
116 {
117 pdebug("username/password not found - not starting Hatena support",
118 DEBUG);
119 ha_going = 0;
120 }
121 else
122 hatena_sc_init(ha_username, ha_password);
123
124 g_free(ha_username);
125 g_free(ha_password);
126
109 m_scrobbler = g_mutex_new(); 127 m_scrobbler = g_mutex_new();
110 if ((pt_scrobbler = g_thread_create(xs_thread, m_scrobbler, TRUE, moo)) == NULL) 128 if ((pt_scrobbler = g_thread_create(xs_thread, m_scrobbler, TRUE, moo)) == NULL)
111 { 129 {
112 pdebug(fmt_vastr("Error creating scrobbler thread: %s", moo), DEBUG); 130 pdebug(fmt_vastr("Error creating scrobbler thread: %s", moo), DEBUG);
113 sc_going = 0; 131 sc_going = 0;
114 ge_going = 0; 132 ge_going = 0;
133 ha_going = 0;
115 return; 134 return;
116 } 135 }
117 136
118 if ((pt_handshake = g_thread_create(hs_thread, m_scrobbler, TRUE, NULL)) == NULL) 137 if ((pt_handshake = g_thread_create(hs_thread, m_scrobbler, TRUE, NULL)) == NULL)
119 { 138 {
120 pdebug(fmt_vastr("Error creating handshake thread: %s", moo), DEBUG); 139 pdebug(fmt_vastr("Error creating handshake thread: %s", moo), DEBUG);
121 sc_going = 0; 140 sc_going = 0;
122 ge_going = 0; 141 ge_going = 0;
142 ha_going = 0;
123 return; 143 return;
124 } 144 }
125 145
126 pdebug("plugin started", DEBUG); 146 pdebug("plugin started", DEBUG);
127 } 147 }
146 166
147 g_thread_join(pt_handshake); 167 g_thread_join(pt_handshake);
148 168
149 sc_cleaner(); 169 sc_cleaner();
150 gerpok_sc_cleaner(); 170 gerpok_sc_cleaner();
171 hatena_sc_cleaner();
151 } 172 }
152 173
153 static char ishttp(const char *a) 174 static char ishttp(const char *a)
154 { 175 {
155 char *tmp, *bp; 176 char *tmp, *bp;
413 { 434 {
414 errorbox_show(gerpok_sc_fetch_error()); 435 errorbox_show(gerpok_sc_fetch_error());
415 gerpok_sc_clear_error(); 436 gerpok_sc_clear_error();
416 } 437 }
417 438
439 if(hatena_sc_catch_error())
440 {
441 errorbox_show(hatena_sc_fetch_error());
442 hatena_sc_clear_error();
443 }
444
418 /* Check for ability to submit */ 445 /* Check for ability to submit */
419 dosubmit = get_song_status(); 446 dosubmit = get_song_status();
420 447
421 if(dosubmit.dosubmit) { 448 if(dosubmit.dosubmit) {
422 TitleInput *tuple; 449 TitleInput *tuple;
435 tuple->performer, tuple->track_name), DEBUG); 462 tuple->performer, tuple->track_name), DEBUG);
436 sc_addentry(m_scrobbler, tuple, 463 sc_addentry(m_scrobbler, tuple,
437 dosubmit.len/1000); 464 dosubmit.len/1000);
438 gerpok_sc_addentry(m_scrobbler, tuple, 465 gerpok_sc_addentry(m_scrobbler, tuple,
439 dosubmit.len/1000); 466 dosubmit.len/1000);
467 hatena_sc_addentry(m_scrobbler, tuple,
468 dosubmit.len/1000);
440 } 469 }
441 else 470 else
442 pdebug("tuple does not contain an artist or a title, not submitting.", DEBUG); 471 pdebug("tuple does not contain an artist or a title, not submitting.", DEBUG);
443 } 472 }
444 g_mutex_lock(m_scrobbler); 473 g_mutex_lock(m_scrobbler);
445 run = (sc_going != 0 || ge_going != 0); 474 run = (sc_going != 0 || ge_going != 0 || ha_going != 0);
446 g_mutex_unlock(m_scrobbler); 475 g_mutex_unlock(m_scrobbler);
447 g_usleep(100000); 476 g_usleep(100000);
448 } 477 }
449 pdebug("scrobbler thread: exiting", DEBUG); 478 pdebug("scrobbler thread: exiting", DEBUG);
450 g_thread_exit(NULL); 479 g_thread_exit(NULL);
472 g_mutex_lock(m_scrobbler); 501 g_mutex_lock(m_scrobbler);
473 ge_going = 0; 502 ge_going = 0;
474 g_mutex_lock(m_scrobbler); 503 g_mutex_lock(m_scrobbler);
475 } 504 }
476 505
506 if(hatena_sc_idle(m_scrobbler))
507 {
508 pdebug("Giving up due to fatal error", DEBUG);
509 g_mutex_lock(m_scrobbler);
510 ha_going = 0;
511 g_mutex_lock(m_scrobbler);
512 }
513
477 g_mutex_lock(m_scrobbler); 514 g_mutex_lock(m_scrobbler);
478 run = (sc_going != 0 || ge_going != 0); 515 run = (sc_going != 0 || ge_going != 0 || ha_going != 0);
479 g_mutex_unlock(m_scrobbler); 516 g_mutex_unlock(m_scrobbler);
480 g_usleep(1000000); 517 g_usleep(1000000);
481 } 518 }
482 pdebug("handshake thread: exiting", DEBUG); 519 pdebug("handshake thread: exiting", DEBUG);
483 g_thread_exit(NULL); 520 g_thread_exit(NULL);