Skip to content

Commit

Permalink
main: add support for web-service api
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
  • Loading branch information
felipec committed Jun 26, 2010
1 parent 47068c5 commit 9367f2b
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions main.c
Expand Up @@ -24,11 +24,22 @@ struct service {
const char *url;
sr_session_t *session;
char *cache;

/* web-service */
char *api_url;
char *api_key;
char *api_secret;
};

static struct service services[] = {
{ .id = "lastfm", .url = SR_LASTFM_URL },
{ .id = "librefm", .url = SR_LIBREFM_URL },
{ .id = "lastfm", .url = SR_LASTFM_URL,
.api_url = SR_LASTFM_API_URL,
.api_key = "a550e8cdf80179f749786109ae94a644",
.api_secret = "92cb9a26e36b18031e5dad8db4edfddb", },
{ .id = "librefm", .url = SR_LIBREFM_URL,
.api_url = SR_LIBREFM_API_URL,
.api_key = "a550e8cdf80179f749786109ae94a644",
.api_secret = "92cb9a26e36b18031e5dad8db4edfddb", },
};

static void
Expand Down Expand Up @@ -155,20 +166,33 @@ static void scrobble_cb(sr_session_t *s)
sr_session_store_list(s, service->cache);
}

static void session_key_cb(sr_session_t *s, const char *session_key)
{
struct service *service = s->user_data;
g_key_file_set_string(keyfile, service->id, "session-key", session_key);
g_file_set_contents(conf_file,
g_key_file_to_data(keyfile, NULL, NULL),
-1, NULL);
}

static gboolean
authenticate_session(struct service *s)
{
gchar *username, *password;
gchar *session_key;
gboolean ok;

username = g_key_file_get_string(keyfile, s->id, "username", NULL);
password = g_key_file_get_string(keyfile, s->id, "password", NULL);
session_key = g_key_file_get_string(keyfile, s->id, "session-key", NULL);

ok = username && password;
if (!ok)
goto leave;

sr_session_set_cred(s->session, username, password);
if (session_key)
sr_session_set_session_key(s->session, session_key);
if (connected)
sr_session_handshake(s->session);

Expand All @@ -187,8 +211,12 @@ get_session(struct service *service)
s->user_data = service;
s->error_cb = error_cb;
s->scrobble_cb = scrobble_cb;
s->session_key_cb = session_key_cb;
service->cache = g_build_filename(cache_dir, service->id, NULL);
sr_session_load_list(s, service->cache);
if (service->api_key)
sr_session_set_api(s, service->api_url,
service->api_key, service->api_secret);
service->session = s;
}

Expand All @@ -202,13 +230,10 @@ authenticate(void)

ok = g_key_file_load_from_file(keyfile, conf_file, G_KEY_FILE_NONE, NULL);
if (!ok)
goto leave;
return;

for (i = 0; i < G_N_ELEMENTS(services); i++)
authenticate_session(&services[i]);

leave:
g_key_file_free(keyfile);
}

static void
Expand Down Expand Up @@ -345,6 +370,8 @@ int main(void)

g_main_loop_unref(main_loop);

g_key_file_free(keyfile);

for (i = 0; i < G_N_ELEMENTS(services); i++) {
struct service *s = &services[i];
sr_session_pause(s->session);
Expand Down

0 comments on commit 9367f2b

Please sign in to comment.