Project

General

Profile

scrobbler-http-revised.diff

John Lindgren, June 20, 2012 02:02

View differences:

src/scrobbler/plugin.c
40 40

  
41 41
guint track_timeout = 0;
42 42

  
43
Tuple *submit_tuple = NULL;
44

  
45 43
static gboolean ishttp(const char *a)
46 44
{
47 45
    g_return_val_if_fail(a != NULL, FALSE);
......
53 51
    gint playlist = aud_playlist_get_playing();
54 52
    gint pos = aud_playlist_get_position(playlist);
55 53

  
56
    if (aud_playlist_entry_get_length (playlist, pos, FALSE) < 30)
57
    {
58
        AUDDBG(" *** not submitting due to entry->length < 30");
54
    char *filename = aud_playlist_entry_get_filename(playlist, pos);
55
    bool_t is_http_source = ishttp(filename);
56
    str_unref(filename);
57

  
58
    Tuple *tuple = aud_playlist_entry_get_tuple(playlist, pos, FALSE);
59
    if (!tuple)
59 60
        return;
60
    }
61 61

  
62
    gchar * filename = aud_playlist_entry_get_filename (playlist, pos);
63
    if (ishttp (filename))
62
    int len = tuple_get_int(tuple, FIELD_LENGTH, NULL) / 1000;
63

  
64
    /* Make up a length when we submit streaming to now playing.
65
     * We will change it before we actually scrobble the track. */
66
    if (len < 1 && is_http_source)
67
        len = 240;
68

  
69
    if (len < 30)
64 70
    {
65
        AUDDBG(" *** not submitting due to HTTP source");
66
        str_unref (filename);
71
        AUDDBG("Length less than 30 seconds; not submitting\n");
72
        tuple_unref(tuple);
67 73
        return;
68 74
    }
69
    str_unref (filename);
70 75

  
71 76
    sc_idle(m_scrobbler);
72 77

  
73
    if (submit_tuple)
74
        tuple_unref (submit_tuple);
75
    submit_tuple = aud_playlist_entry_get_tuple (playlist, pos, FALSE);
76
    if (! submit_tuple)
77
        return;
78

  
79
    sc_addentry(m_scrobbler, submit_tuple, tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) / 1000);
78
    sc_addentry(m_scrobbler, tuple, len, is_http_source);
79
    tuple_unref(tuple);
80 80

  
81 81
    if (!track_timeout)
82 82
        track_timeout = g_timeout_add_seconds(1, sc_timeout, NULL);
......
84 84

  
85 85
static void aud_hook_playback_end(gpointer aud_hook_data, gpointer user_data)
86 86
{
87
    sc_playback_end();
87 88
    sc_idle(m_scrobbler);
88 89

  
89 90
    if (track_timeout)
......
91 92
        g_source_remove(track_timeout);
92 93
        track_timeout = 0;
93 94
    }
94

  
95
    if (submit_tuple != NULL)
96
    {
97
        tuple_unref (submit_tuple);
98
        submit_tuple = NULL;
99
    }
100 95
}
101 96

  
102 97
void start(void) {
src/scrobbler/scrobbler.c
13 13
#include <audacious/drct.h>
14 14
#include <audacious/debug.h>
15 15
#include <audacious/misc.h>
16
#include <audacious/playlist.h>
16 17
#include <audacious/plugin.h>
17 18

  
18 19
#define SCROBBLER_CLI_ID "aud"
......
48 49
        sc_curl_errbuf[CURL_ERROR_SIZE],
49 50
        *sc_major_error;
50 51

  
52
static int sc_submit_np(Tuple *tuple, int len);
51 53
static void dump_queue();
52 54

  
53 55
/**** Queue stuff ****/
......
63 65
    int utctime, track, len;
64 66
    int timeplayed;
65 67
    int numtries;
68
    bool_t is_http_source;
66 69
    void *next;
67 70
} item_t;
68 71

  
......
137 140
    return newitem;
138 141
}
139 142

  
140
static item_t *create_item(Tuple *tuple, int len)
143
static item_t *create_item(Tuple *tuple, int len, bool_t is_http_source)
141 144
{
142 145
    item_t *item;
143 146
    gchar *album, *artist, *title;
......
172 175
    else
173 176
        item->album = fmt_escape("");
174 177

  
178
    item->is_http_source = is_http_source;
179

  
175 180
    item->next = NULL;
176 181

  
177 182
    return item;
......
181 186
{
182 187
    item_t *item;
183 188

  
184
    if ((item = create_item (tuple, len)) == NULL)
189
    if (!(item = create_item(tuple, len, FALSE)))
185 190
        return NULL;
186 191

  
187 192
    item->timeplayed = len;
......
190 195
    return q_additem(item);
191 196
}
192 197

  
193
static item_t *set_np(Tuple *tuple, int len)
198
static item_t *set_np(Tuple *tuple, int len, bool_t is_http_source)
194 199
{
195 200
    if (np_item != NULL)
196 201
        q_item_free (np_item);
197 202

  
198
    if ((np_item = create_item (tuple, len)) != NULL)
203
    if ((np_item = create_item(tuple, len, is_http_source)))
199 204
        AUDDBG ("Tracking now-playing track: %s - %s\n", np_item->artist,
200 205
         np_item->title);
201 206

  
......
266 271
/* isn't there better way for that? --desowin */
267 272
gboolean sc_timeout(gpointer data)
268 273
{
269
    if (np_item)
274
    if (!np_item)
275
        return TRUE;
276

  
277
    if (aud_drct_get_playing() && !aud_drct_get_paused())
278
        np_item->timeplayed++;
279

  
280
    if (np_item->is_http_source)
270 281
    {
271
        if (aud_drct_get_playing() && !aud_drct_get_paused())
272
            np_item->timeplayed+=1;
282
        /* This might be a stream, so check if the track info has changed. */
283
        int playlist = aud_playlist_get_playing();
284
        int pos = aud_playlist_get_position(playlist);
285
        Tuple *tuple = aud_playlist_entry_get_tuple(playlist, pos, FALSE);
286

  
287
        if (!tuple)
288
            return TRUE;
289

  
290
        int len = tuple_get_int(tuple, FIELD_LENGTH, NULL) / 1000;
291

  
292
        /* Make up a length when we submit streaming to now playing.
293
         * We will change it before we actually scrobble the track. */
294
        if (len < 1)
295
            len = 240;
296

  
297
        item_t *current_item = create_item(tuple, len, TRUE);
298

  
299
        if (!current_item)
300
        {
301
            tuple_unref(tuple);
302
            return TRUE;
303
        }
304

  
305
        if (strcmp(np_item->artist, current_item->artist)
306
         || strcmp(np_item->title, current_item->title)
307
         || strcmp (np_item->album, current_item->album)
308
         || np_item->track != current_item->track)
309
        {
310
            /* Now set the real length of the track. */
311
            np_item->len = np_item->timeplayed;
312

  
313
            AUDDBG ("Submitting\n");
314

  
315
            q_additem(np_item);
316
            np_item = current_item;
317
            dump_queue();
318

  
319
            sc_submit_np(tuple, len);
320
        }
321
        else
322
        {
323
            q_item_free(current_item);
273 324

  
325
            if (np_item->timeplayed > np_item->len)
326
            {
327
                /* Submit the track again to make sure it stays on now playing.
328
                 * This doubles the total length we have submitted. */
329
                sc_submit_np(tuple, np_item->len);
330
                np_item->len *= 2;
331
            }
332
        }
333

  
334
        tuple_unref(tuple);
335
    }
336
    else
337
    {
274 338
        /*
275 339
         * Check our now-playing track to see if it should go into the queue
276 340
         */
......
288 352
    return TRUE;
289 353
}
290 354

  
355
void sc_playback_end(void)
356
{
357
    if (np_item && np_item->is_http_source)
358
    {
359
        /* Now set the real length of the track. */
360
        np_item->len = np_item->timeplayed;
361

  
362
        AUDDBG ("Submitting\n");
363

  
364
        q_additem(np_item);
365
        np_item = NULL;
366
        dump_queue();
367
    }
368
}
369

  
291 370
/* Error functions */
292 371

  
293 372
static void sc_throw_error(char *errortxt)
......
778 857
    return i;
779 858
}
780 859

  
781
static int sc_submit_np(Tuple *tuple)
860
static int sc_submit_np(Tuple *tuple, int len)
782 861
{
783 862
    CURL *curl;
784 863
    /* struct HttpPost *post = NULL , *last = NULL; */
......
810 889
        field_artist,
811 890
        field_title,
812 891
        field_album,
813
        tuple_get_int(tuple, FIELD_LENGTH, NULL) / 1000,
892
        len,
814 893
        tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL));
815 894
        curl_free(field_artist);
816 895
        curl_free(field_title);
......
1126 1205
    AUDDBG("scrobbler starting up\n");
1127 1206
}
1128 1207

  
1129
void sc_addentry(GMutex *mutex, Tuple *tuple, int len)
1208
void sc_addentry(GMutex *mutex, Tuple *tuple, int len, bool_t is_http_source)
1130 1209
{
1131 1210
    g_mutex_lock(mutex);
1132 1211

  
1133
    sc_submit_np(tuple);
1134
    set_np(tuple, len);
1212
    sc_submit_np(tuple, len);
1213
    set_np(tuple, len, is_http_source);
1135 1214

  
1136 1215
    /*
1137 1216
     * This will help make sure the queue will be saved on a nasty
src/scrobbler/scrobbler.h
14 14

  
15 15
int sc_idle(GMutex *);
16 16
void sc_init(char *, char *, char *);
17
void sc_addentry(GMutex *, Tuple *, int);
17
void sc_addentry(GMutex *mutex, Tuple *tuple, int len, bool_t is_http_source);
18 18
void sc_cleaner(void);
19 19
int sc_catch_error(void);
20 20
char *sc_fetch_error(void);
21 21
void sc_clear_error(void);
22
void sc_playback_end(void);
23

  
22 24
#endif