File: puid.c

package info (click to toggle)
libtunepimp 0.5.3-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,668 kB
  • ctags: 3,470
  • sloc: ansic: 15,652; cpp: 12,752; sh: 11,929; perl: 1,179; python: 720; makefile: 310; pascal: 33
file content (376 lines) | stat: -rw-r--r-- 10,735 bytes parent folder | download | duplicates (2)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*----------------------------------------------------------------------------

   libtunepimp -- The MusicBrainz tagging library.  
                  Let a thousand taggers bloom!
   
   Copyright (C) Robert Kaye 2003
   
   This file is part of libtunepimp.

   libtunepimp 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.

   libtunepimp 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 libtunepimp; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   $Id: puid.c 7959 2006-06-29 00:48:30Z luks $

----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <musicbrainz/browser.h>
#include <tunepimp-0.5/tp_c.h>
#include "../config.h"

char *EscapeFormValue(const char *form_value)
{
    int i, form_value_length, extra_length;
    char *escaped_value, *ptr;

    form_value_length = strlen(form_value);
    for (i = 0, extra_length = 0; i < form_value_length; ++i)
    {
        switch(form_value[i])
        {
            case '"':
                extra_length += 5;
                break;
            case '&':
                extra_length += 4;
                break;
            case '<':
            case '>':
                extra_length += 3;
                break;
        }
    }

    if (extra_length == 0)
    {
        // This is necessary since the caller must free the memory.
        return strdup(form_value);
    }

    escaped_value = (char *)malloc(form_value_length + extra_length + 1);
    for (i = 0, ptr = escaped_value; i < form_value_length; ++i)
    {
        switch(form_value[i])
        {
            case '"':
                strcpy(ptr, "&quot;");
                ptr += 6;
                break;
            case '&':
                strcpy(ptr, "&amp;");
                ptr += 5;
                break;
            case '<':
                strcpy(ptr, "&lt;");
                ptr += 4;
                break;
            case '>':
                strcpy(ptr, "&gt;");
                ptr += 4;
                break;
            default:
                *(ptr++) = form_value[i];
        }
    }
    *ptr = 0;

    return escaped_value;
}

int CreateLookupPage(const char *outFile, track_t *track, metadata_t *data, const char *server)
{
    FILE *out;
    char *temp, srcFile[1024], puid[255];

    tr_GetFileName(track, srcFile, 1024);
    tr_GetPUID(track, puid, 255);

    out = fopen(outFile, "wt");
    if (!out)
        return 0;

    fprintf(out,
            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
            "<HTML><HEAD><TITLE>MusicBrainz Lookup</TITLE></HEAD>\n"
            "<BODY onLoad=\"document.forms[0].submit()\">\n"
            "<center>\n"
            "<h3>MusicBrainz Loookup</h3><p>\n"
            "Looking up file at MusicBrainz.\n"
            "<FORM METHOD=\"POST\"\n"
            "      ACTION=\"http://%s/taglookup.html\"\n"
            "      ENCTYPE=\"application/x-www-form-urlencoded\" "
            "      class=\"formstyle\">\n", server);

    temp = EscapeFormValue(data->artist);
    fprintf(out, 
            "  <INPUT TYPE=\"hidden\" NAME=\"artist\" VALUE=\"%s\">\n",
            temp);
    free(temp);

    temp = EscapeFormValue(data->album);
    fprintf(out, 
            "  <INPUT TYPE=\"hidden\" NAME=\"album\" VALUE=\"%s\">\n",
            temp);
    free(temp);

    temp = EscapeFormValue(data->track);
    fprintf(out, 
            "  <INPUT TYPE=\"hidden\" NAME=\"track\" VALUE=\"%s\">\n",
            temp);
    free(temp);

    temp = EscapeFormValue(data->trackId);
    fprintf(out, 
            "  <INPUT TYPE=\"hidden\" NAME=\"trackid\" VALUE=\"%s\">\n",
            temp);
    free(temp);

    temp = EscapeFormValue(srcFile);
    fprintf(out, 
            "  <INPUT TYPE=\"hidden\" NAME=\"filename\" VALUE=\"%s\">\n",
            temp);
    free(temp);

    temp = EscapeFormValue(puid);
    fprintf(out, 
            "  <INPUT TYPE=\"hidden\" NAME=\"puid\" VALUE=\"%s\">\n",
            temp);
    free(temp);

    fprintf(out, 
            "  <INPUT TYPE=\"hidden\" NAME=\"duration\" VALUE=\"%lu\">\n"
            "  <INPUT TYPE=\"hidden\" NAME=\"tracknum\" VALUE=\"%d\">\n"
            "</form>\n"
            "</center>\n"
            "</body>\n"
            "</html>\n", data->duration, data->trackNum);

    fclose(out);


    return 1;
}

void LookupMetadata(track_t track, const char *fileName)
{
    metadata_t *data;
    char       *server;
    char       *outFile = "/tmp/lookup.html";
    char       *url = "file:///tmp/lookup.html";

    if (getenv("MB_SERVER"))
        server = getenv("MB_SERVER");
    else
        server = "musicbrainz.org";

    data = md_New();
    tr_GetLocalMetadata(track, data);
    CreateLookupPage(outFile, track, data, server);
    md_Delete(data);

    LaunchBrowser(url, "mozilla");
}

void PrintMetadata(track_t *track)
{
    metadata_t *data;
    char        puid[255];
   
    data = md_New();
    tr_GetPUID(track, puid, 255);
    tr_GetLocalMetadata(track, data);
    printf("  Artist: '%s'\n", data->artist);
    printf("   Album: '%s'\n", data->album);
    printf("   Track: '%s'\n", data->track);
    printf("TrackNum: '%d'\n", data->trackNum);
    printf("Duration: '%lu'\n", data->duration);
    printf("     PUID: %s\n", puid);
    md_Delete(data);
}

int main(int argc, char *argv[])
{
    int            index = 1, isLookup = 0, printID3 = 0;
    tunepimp_t     pimp;
    track_t        track;
    int            done, fileId, i;
    TPCallbackEnum type;
    TPFileStatus   status;
    char           puid[255], clientId[128];
    int            analyzed = 0;
#ifdef WIN32
    WSADATA        sGawdIHateMicrosoft;
#endif

    if (argc < 2)
    {
        int        num;
        char       ext[10][32];

        fprintf(stderr,"usage: puid [-i|-l] <musicDNS clientId> <audio file>\n");
        fprintf(stderr,"Options:\n");
        fprintf(stderr,"  -i   Print out track metadata along with puid id\n");
        fprintf(stderr,"  -l   Lookup file at MusicBrainz\n\n");
        fprintf(stderr,"To obtain a MusicDNS client id, go to http://musicdns.org\n\n");
        fprintf(stderr, "BROWSER environment variable to specify your browser of choice.\n");
        fprintf(stderr, "Check http://www.catb.org/~esr/BROWSER/index.html for details.\n\n");

        pimp = tp_NewWithArgs("puid", VERSION, TP_THREAD_NONE, NULL);

        printf("Supported file extensions: ");
        num = tp_GetNumSupportedExtensions(pimp);
        tp_GetSupportedExtensions(pimp, ext);
        for(i = 0; i < num; i++)
            printf("%s ", ext[i]);
        printf("\n");

        tp_Delete(pimp);

        return -1;
    }

#ifdef WIN32
    WSAStartup(0x0002, &sGawdIHateMicrosoft);
#endif

    for(; index < argc; index++)
    {
        if (strcmp(argv[index], "-l") == 0)
           isLookup = 1;
        else
        if (strcmp(argv[index], "-i") == 0)
           printID3 = 1;
        else
           break;
    }

    if (index >= argc)
    {
        fprintf(stderr, "error: no clientid specified.\n");
        exit(-1);
    }
    strcpy(clientId, argv[index++]);
    if (strlen(clientId) == 0)
    {
        fprintf(stderr, "Invalid MusicDNS client id specified.\n");
        return (-1);
    }
    if (index >= argc)
    {
        fprintf(stderr, "error: no lookup file specified.\n");
        exit(-1);
    }
    if (access(argv[index], 0))
    {
        fprintf(stderr, "Cannot open file %s\n", argv[index]);
        return (-1);
    }

    pimp = tp_NewWithArgs("puid", VERSION, TP_THREAD_ANALYZER | TP_THREAD_READ, NULL);
    tp_SetMusicDNSClientId(pimp, clientId);
    tp_AddFile(pimp, argv[index], 0);

    for(done = 0;!done;)
    {
        while(tp_GetNotification(pimp, &type, &fileId, &status) && !done)
        {
            if (type != tpFileChanged)
                continue;

            track = tp_GetTrack(pimp, 0);
            tr_Lock(track);
            switch(tr_GetStatus(track))
            {
                case ePUIDLookup:
                {
                    if (printID3)
                        PrintMetadata(track);
                    else
                    if (isLookup)
                        LookupMetadata(track, argv[index]);
                    else
                    {
                        tr_GetPUID(track, puid, 255);
                        printf("%s\n", puid);
                    }

                    done = 1;
                    break;
                }
                case eUnrecognized:
                    if (!analyzed)
                    {
                        puid[0] = 0;
                        tr_GetPUID(track, puid, 255);
                        if (puid[0] == 0)
                        {
                            tr_SetStatus(track, ePending);
                            tp_Wake(pimp, track);
                            analyzed = 1;
                        }
                        break;
                    }
                    printf("No PUID available for this track.\n");
                    done = 1;
                    break;
                    
                case eRecognized:
                    puid[0] = 0;
                    tr_GetPUID(track, puid, 255);
                    if (puid[0])
                        fprintf(stderr, "PUID id read from file: %s\n", puid);

                    tp_IdentifyAgain(pimp, 0);
                    break;
                case ePending:
                    break;
                case eError:
                {
                    char err[255];
                    tr_GetError(track, err, 255);
                    fprintf(stderr, "Error: %s\n", err);
                    done = 1;
                    break;
                }
                default:
                    fprintf(stderr, "Warning: Unsupported case: %d\n", tr_GetStatus(track));
                    done = 1;
                    break;
            }
            tr_Unlock(track);
            tp_ReleaseTrack(pimp, track);

#ifdef WIN32
            Sleep(50);
#else
            usleep(50000);
#endif
        }
    }

    tp_Delete(pimp);

#ifdef WIN32
    WSACleanup();
#endif

    return 0;
}