summaryrefslogtreecommitdiff
path: root/plugins/lastfm/lastfm.c
blob: 4b6bc21e55f3ea5958ba2efa6bb80791b5f73d7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009  Alexey Yakovenko

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include "../../deadbeef.h"

static DB_misc_t plugin;
static DB_functions_t *deadbeef;
static char lastfm_login[50];
static char lastfm_pass[50];

#define SCROBBLER_URL "http://ws.audioscrobbler.com/2.0"
#define LASTFM_API_KEY "6b33c8ae4d598a9aff8fe63e334e6e86"
#define LASTFM_API_SECRET "a9f5e17e358377d96e96477d870b2b18"

DB_plugin_t *
lastfm_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}

static char *lastfm_srv_res;
static char lastfm_srv_size;
static char lastfm_curl_err[CURL_ERROR_SIZE];

static size_t
lastfm_curl_res (void *ptr, size_t size, size_t nmemb, void *stream)
{
    int len = size * nmemb;
    lastfm_srv_res = realloc (lastfm_srv_res, lastfm_srv_size + len + 1);
    memcpy (lastfm_srv_res + lastfm_srv_size, ptr, len);
    lastfm_srv_size += len;

    char s[size*nmemb+1];
    memcpy (s, ptr, size*nmemb);
    s[size*nmemb] = 0;
    printf ("%s\n", s);

    return len;
}

int
lastfm_auth (void) {
    // auth
    char msg[4096];
    char sigstr[4096];
    uint8_t sig[16];
    snprintf (sigstr, sizeof (sigstr), "api_key%smethodauth.getToken%s", SCROBBLER_URL, LASTFM_API_KEY, LASTFM_API_SECRET);
    deadbeef->md5 (sig, sigstr, strlen (sigstr));
    deadbeef->md5_to_str (sigstr, sig);
    snprintf (msg, sizeof (msg), "%s/?method=auth.getToken&api_key=%s&api_sig=%s", SCROBBLER_URL, LASTFM_API_KEY, sigstr);
    printf ("sending request: %s\n", msg);
    // init curl
    CURL *curl;
    curl = curl_easy_init ();
    if (!curl) {
        fprintf (stderr, "lastfm: failed to init curl\n");
        return 0;
    }
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
    curl_easy_setopt(curl, CURLOPT_URL, msg);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, lastfm_curl_res);
    memset(lastfm_curl_err, 0, sizeof(lastfm_curl_err));
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, lastfm_curl_err);
    curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    int status = curl_easy_perform(curl);
    curl_easy_cleanup (curl);

    if (!status) {
        // parse output
        if (strstr (lastfm_srv_res, "<lfm status=\"ok\">")) {
            char *token = strstr (lastfm_srv_res, "<token>");
            if (token) {
                token += 7;
                char *end = strstr (token, "</token>");
                if (end) {
                    *end = 0;
                    printf ("token: %s\n", token);
                }
                else {
                    printf ("no </token>\n");
                }
            }
            else {
                printf ("no <token>\n");
            }
        }
        else {
            printf ("not ok\n");
        }
    }

    if (lastfm_srv_res) {
        free (lastfm_srv_res);
        lastfm_srv_res = NULL;
    }
}

static int
lastfm_frameupdate (int ev, uintptr_t data) {
//    printf ("lastfm tick\n");
    return 0;
}

static int
lastfm_songchanged (int ev, uintptr_t data) {
    printf ("song changed\n");
    return 0;
}

static int
lastfm_start (void) {
    // subscribe to frameupdate event
    deadbeef->ev_subscribe (DB_PLUGIN (&plugin), DB_EV_FRAMEUPDATE, lastfm_frameupdate, 0);
    deadbeef->ev_subscribe (DB_PLUGIN (&plugin), DB_EV_SONGCHANGED, lastfm_songchanged, 0);
    // load login/pass
    char config[1024];
    snprintf (config, 1024, "%s/.config/deadbeef/lastfm", getenv ("HOME"));
    FILE *fp = fopen (config, "rt");
    if (!fp) {
        fprintf (stderr, "lastfm: failed open %s\n", config);
        return -1;
    }
    if (!fgets (lastfm_login, 50, fp)) {
        fprintf (stderr, "lastfm: failed to read login from %s\n", config);
        fclose (fp);
        return -1;
    }
    if (!fgets (lastfm_pass, 50, fp)) {
        fprintf (stderr, "lastfm: failed to read pass from %s\n", config);
        fclose (fp);
        return -1;
    }
    fclose (fp);
    // remove trailing garbage
    int l;
    char *p;
    l = strlen (lastfm_login);
    p = lastfm_login+l-1;
    while (p >= lastfm_login && *p < 0x20) {
        p--;
    }
    *p = 0;
    l = strlen (lastfm_pass);
    p = lastfm_pass+l-1;
    while (p >= lastfm_login && *p < 0x20) {
        p--;
    }
    *p = 0;
    lastfm_auth ();

    return 0;
}

static int
lastfm_stop (void) {
    deadbeef->ev_unsubscribe (DB_PLUGIN (&plugin), DB_EV_FRAMEUPDATE, lastfm_frameupdate, 0);
    deadbeef->ev_unsubscribe (DB_PLUGIN (&plugin), DB_EV_SONGCHANGED, lastfm_songchanged, 0);
    return 0;
}

// define plugin interface
static DB_misc_t plugin = {
    .plugin.type = DB_PLUGIN_MISC,
    .plugin.name = "last.fm scrobbler",
    .plugin.descr = "sends played songs information to your last.fm account",
    .plugin.author = "Alexey Yakovenko",
    .plugin.email = "waker@users.sourceforge.net",
    .plugin.website = "http://deadbeef.sf.net",
    .plugin.start = lastfm_start,
    .plugin.stop = lastfm_stop
};