summaryrefslogtreecommitdiff
path: root/src/scrobbler2/scrobbler_xml_parsing.cc
blob: 6f2a6d31ad632734d9fcf5fc670faaf46caf96dc (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//plugin includes
#include "scrobbler.h"

//static (private) variables
static xmlDocPtr doc = nullptr;
static xmlXPathContextPtr context = nullptr;

static gboolean prepare_data () {
    received_data[received_data_size] = '\0';
    AUDDBG("Data received from last.fm:\n%s\n%%%%End of data%%%%\n", received_data);

    doc = xmlParseMemory(received_data, received_data_size+1);
    received_data_size = 0;
    if (doc == nullptr) {
        AUDDBG("Document not parsed successfully.\n");
        return false;
    }

    context = xmlXPathNewContext(doc);
    if (context == nullptr) {
        AUDDBG("Error in xmlXPathNewContext\n");
        xmlFreeDoc(doc);
        doc = nullptr;
        return false;
    }
    return true;
}

static void clean_data() {
    xmlXPathFreeContext(context);
    xmlFreeDoc(doc);
    context = nullptr;
    doc = nullptr;
}


//returns:
// nullptr if an error occurs or the attribute was not found
// the attribute's value if it was found
static String get_attribute_value (const char *node_expression, const char *attribute) {
    if (doc == nullptr || context == nullptr) {
        AUDDBG("Response from last.fm not parsed successfully. Did you call prepare_data?\n");
        return String();
    }

    xmlXPathObjectPtr statusObj = xmlXPathEvalExpression((xmlChar *) node_expression, context);
    if (statusObj == nullptr) {
        AUDDBG ("Error in xmlXPathEvalExpression.\n");
        return String();
    }
    if (xmlXPathNodeSetIsEmpty(statusObj->nodesetval)) {
        AUDDBG("No result.\n");
        xmlXPathFreeObject(statusObj);
        return String();
    }

    xmlChar *prop = xmlGetProp(statusObj->nodesetval->nodeTab[0], (xmlChar *) attribute);

    String result;
    if (prop && prop[0])
        result = String((const char *)prop);

    xmlXPathFreeObject(statusObj);
    xmlFree(prop);

    AUDDBG("RESULT FOR THIS FUNCTION: %s.\n", (const char *)result);
    return result;
}

//returns:
// nullptr if an error occurs or the node was not found
static String get_node_string (const char *node_expression) {
    if (doc == nullptr || context == nullptr) {
        AUDDBG("Response from last.fm not parsed successfully. Did you call prepare_data?\n");
        return String();
    }

    xmlXPathObjectPtr statusObj = xmlXPathEvalExpression((xmlChar *) node_expression, context);
    if (statusObj == nullptr) {
        AUDDBG ("Error in xmlXPathEvalExpression.\n");
        return String();
    }
    if (xmlXPathNodeSetIsEmpty(statusObj->nodesetval)) {
        AUDDBG("No result.\n");
        xmlXPathFreeObject(statusObj);
        return String();
    }

    xmlChar *string = xmlNodeListGetString(doc, statusObj->nodesetval->nodeTab[0]->children, 1);

    String result;
    if (string && string[0])
        result = String((const char *) string);

    xmlXPathFreeObject(statusObj);
    xmlFree(string);

    AUDDBG("RESULT FOR THIS FUNCTION: %s.\n", (const char *)result);
    return result;
}


//returns:
// nullptr if an error occurs
// "true" if the the command succeeded
// "false" if an error occurred. error_code and error_detail should be checked in this case
static String check_status (String &error_code, String &error_detail) {

    String status = get_attribute_value("/lfm[@status]", "status");
    if (!status) {
        AUDDBG("last.fm not answering according to the API.\n");
        return String();
    }

    AUDDBG ("status is %s.\n", (const char *)status);
    if (strcmp(status, "ok")) {

        error_code = get_attribute_value("/lfm/error[@code]", "code");
        if (!(*error_code)) {
            AUDDBG("Weird API answer. Last.fm says status is %s but there is no error code?\n",
             (const char *)status);
            status = String();
        } else {
            error_detail = get_node_string("/lfm/error");
        }
    }

    AUDDBG("check_status results: return: %s. error_code: %s. error_detail: %s.\n",
     (const char *)status, (const char *)error_code, (const char *)error_detail);
    return status;
}

/*
 * Returns:
 *  * TRUE if the scrobble was successful
 *    * with ignored = TRUE if it was ignored
 *      * ignored_code_out must be checked
 *    * with ignored = FALSE if it was scrobbled OK
 *  * FALSE if the scrobble was unsuccessful
 *    * error_code_out and error_detail_out must be checked:
 *      * They are nullptr if an API communication error occur
 */
gboolean read_scrobble_result(String &error_code, String &error_detail,
 gboolean *ignored, String &ignored_code) {

    *ignored = false;

    gboolean result = true;

    if (!prepare_data()) {
        AUDDBG("Could not read received data from last.fm. What's up?\n");
        return false;
    }

    String status = check_status(error_code, error_detail);

    if (!status) {
        AUDDBG("Status was nullptr. Invalid API answer.\n");
        clean_data();
        return false;
    }

    if (!strcmp(status, "failed")) {
        AUDDBG("Error code: %s. Detail: %s.\n", (const char *)error_code,
         (const char *)error_detail);
        result = false;

    } else {
        //TODO: We are assuming that only one track is scrobbled per request! This will have to be
        //re-done to support multiple tracks being scrobbled in batch
        String ignored_scrobble = get_attribute_value("/lfm/scrobbles[@ignored]", "ignored");

        if (ignored_scrobble && strcmp(ignored_scrobble, "0")) {
          //The track was ignored
          //TODO: this assumes ignored_scrobble == 1!!!
          *ignored = true;
          ignored_code = get_attribute_value("/lfm/scrobbles/scrobble/ignoredMessage[@code]", "code");
        }

        AUDDBG("ignored? %i, ignored_code: %s\n", *ignored, (const char *)ignored_code);
    }

    clean_data();
    return result;
}

//returns
//FALSE if there was an error with the connection
gboolean read_authentication_test_result (String &error_code, String &error_detail) {

    gboolean result = true;

    if (!prepare_data()) {
        AUDDBG("Could not read received data from last.fm. What's up?\n");
        return false;
    }

    String status = check_status(error_code, error_detail);

    if (!status) {
        AUDDBG("Status was nullptr. Invalid API answer.\n");
        clean_data();
        return false;
    }

    if (!strcmp(status, "failed")) {
        result = false;

    } else {
        username = get_node_string("/lfm/user/name");
        if (!username) {
          AUDERR("last.fm not answering according to the API.\n");
          result = false;
        }
    }

    clean_data();
    return result;
}



gboolean read_token (String &error_code, String &error_detail) {

    gboolean result = true;

    if (!prepare_data()) {
        AUDDBG("Could not read received data from last.fm. What's up?\n");
        return false;
    }

    String status = check_status(error_code, error_detail);

    if (!status) {
        AUDDBG("Status was nullptr. Invalid API answer.\n");
        clean_data();
        return false;
    }

    if (!strcmp(status, "failed")) {
        AUDDBG("Error code: %s. Detail: %s.\n", (const char *)error_code,
         (const char *)error_detail);
        result = false;
    }
    else {
        request_token = get_node_string("/lfm/token");

        if (!request_token || !request_token[0]) {
            AUDDBG("Could not read the received token. Something's wrong with the API?\n");
            result = false;
        }
        else {
            AUDDBG("This is the token: %s.\nNice? Nice.\n", (const char *)request_token);
        }
    }

    clean_data();
    return result;
}



gboolean read_session_key(String &error_code, String &error_detail) {

    gboolean result = true;

    if (!prepare_data()) {
        AUDDBG("Could not read received data from last.fm. What's up?\n");
        return false;
    }

    String status = check_status(error_code, error_detail);

    if (!status) {
        AUDDBG("Status was nullptr or empty. Invalid API answer.\n");
        clean_data();
        return false;
    }

    if (!strcmp(status, "failed")) {
        AUDDBG("Error code: %s. Detail: %s.\n", (const char *)error_code,
         (const char *)error_detail);
        result = false;

    } else {
        session_key = get_node_string("/lfm/session/key");

        if (!session_key || !session_key[0]) {
            AUDDBG("Could not read the received session key. Something's wrong with the API?\n");
            result = false;
        } else {
            AUDDBG("This is the session key: %s.\n", (const char *)session_key);
        }
    }

    clean_data();
    return result;
}