~ubuntu-branches/ubuntu/wily/kid3/wily-proposed

« back to all changes in this revision

Viewing changes to src/core/import/amazonimporter.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2013-05-11 21:00:00 UTC
  • mfrom: (1.1.15) (2.1.17 sid)
  • Revision ID: package-import@ubuntu.com-20130511210000-4k2q62ldezhgtxjt
Tags: 2.3-2
Upload to unstable 

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 * \author Urs Fleisch
7
7
 * \date 13 Dec 2009
8
8
 *
9
 
 * Copyright (C) 2009-2011  Urs Fleisch
 
9
 * Copyright (C) 2009-2013  Urs Fleisch
10
10
 *
11
11
 * This file is part of Kid3.
12
12
 *
42
42
  TrackDataModel* trackDataModel)
43
43
  : ServerImporter(netMgr, trackDataModel)
44
44
{
45
 
  setObjectName("AmazonImporter");
 
45
  setObjectName(QLatin1String("AmazonImporter"));
46
46
}
47
47
 
48
48
/**
94
94
    <span class="ptBrand">by <a href="/Amon-Amarth/e/B000APIBHO/ref=sr_ntt_srch_lnk_1?qid=1328870421&sr=1-1">Amon Amarth</a> and <a href="/Amon-Amarth/e/B002E4DJ7Q/ref=sr_ntt_srch_lnk_1?qid=1328870421&sr=1-1">Amon Amarth</a></span>
95
95
   */
96
96
  QString str = QString::fromLatin1(searchStr);
97
 
  QRegExp catIdTitleArtistRe(
 
97
  QRegExp catIdTitleArtistRe(QLatin1String(
98
98
    "<a class=\"title\" href=\"[^\"]+/(dp|ASIN|images|product|-)/([A-Z0-9]+)[^\"]+\">"
99
 
    "([^<]+)<.*>\\s*by\\s*(?:<[^>]+>)?([^<]+)<");
100
 
  QStringList lines = str.remove('\r').split(QRegExp("\\n{2,}"));
 
99
    "([^<]+)<.*>\\s*by\\s*(?:<[^>]+>)?([^<]+)<"));
 
100
  QStringList lines = str.remove(QLatin1Char('\r')).split(QRegExp(QLatin1String("\\n{2,}")));
101
101
  m_albumListModel->clear();
102
102
  for (QStringList::const_iterator it = lines.begin(); it != lines.end(); ++it) {
103
103
    QString line(*it);
104
 
    line.remove('\n');
 
104
    line.remove(QLatin1Char('\n'));
105
105
    if (catIdTitleArtistRe.indexIn(line) != -1) {
106
106
      m_albumListModel->appendRow(new AlbumListItem(
107
 
        removeHtml(catIdTitleArtistRe.cap(4)) + " - " +
 
107
        removeHtml(catIdTitleArtistRe.cap(4)) + QLatin1String(" - ") +
108
108
        removeHtml(catIdTitleArtistRe.cap(3)),
109
109
        catIdTitleArtistRe.cap(1),
110
110
        catIdTitleArtistRe.cap(2)));
152
152
   */
153
153
  QString str = QString::fromLatin1(albumStr);
154
154
  FrameCollection framesHdr;
 
155
  const bool standardTags = getStandardTags();
155
156
  // search for 'id="btAsinTitle"', text after '>' until ' [' or '<' => album
156
157
  int end = 0;
157
 
  int start = str.indexOf("id=\"btAsinTitle\"");
158
 
  if (start >= 0) {
159
 
    start = str.indexOf(">", start);
 
158
  int start = str.indexOf(QLatin1String("id=\"btAsinTitle\""));
 
159
  if (start >= 0 && standardTags) {
 
160
    start = str.indexOf(QLatin1Char('>'), start);
160
161
    if (start >= 0) {
161
 
      end = str.indexOf("<", start);
 
162
      end = str.indexOf(QLatin1Char('<'), start);
162
163
      if (end > start) {
163
 
        int bracketPos = str.indexOf(" [", start);
 
164
        int bracketPos = str.indexOf(QLatin1String(" ["), start);
164
165
        if (bracketPos >= 0 && bracketPos < end) {
165
166
          end = bracketPos;
166
167
        }
168
169
          replaceHtmlEntities(str.mid(start + 1, end - start - 1)));
169
170
 
170
171
        // next '<a href=', text after '>' until '<' => artist
171
 
        start = str.indexOf("<a href=", end);
 
172
        start = str.indexOf(QLatin1String("<a href="), end);
172
173
        if (start >= 0) {
173
 
          start = str.indexOf(">", start);
 
174
          start = str.indexOf(QLatin1Char('>'), start);
174
175
          if (start >= 0) {
175
 
            end = str.indexOf("<", start);
 
176
            end = str.indexOf(QLatin1Char('<'), start);
176
177
            if (end > start) {
177
178
              framesHdr.setArtist(
178
179
                replaceHtmlEntities(str.mid(start + 1, end - start - 1)));
186
187
  // search for >Product Details<, >Original Release Date:<, >Label:<
187
188
  const bool additionalTags = getAdditionalTags();
188
189
  QString albumArtist;
189
 
  start = str.indexOf(">Product Details<");
 
190
  start = str.indexOf(QLatin1String(">Product Details<"));
190
191
  if (start >= 0) {
191
 
    int detailStart = str.indexOf(">Original Release Date:<", start);
 
192
    int detailStart = str.indexOf(QLatin1String(">Original Release Date:<"), start);
192
193
    if (detailStart < 0) {
193
 
      detailStart  = str.indexOf(">Audio CD<", start);
 
194
      detailStart  = str.indexOf(QLatin1String(">Audio CD<"), start);
194
195
    }
195
 
    if (detailStart >= 0) {
196
 
      int detailEnd = str.indexOf("\n", detailStart + 10);
 
196
    if (detailStart >= 0 && standardTags) {
 
197
      int detailEnd = str.indexOf(QLatin1Char('\n'), detailStart + 10);
197
198
      if (detailEnd > detailStart + 10) {
198
 
        QRegExp yearRe("(\\d{4})");
 
199
        QRegExp yearRe(QLatin1String("(\\d{4})"));
199
200
        if (yearRe.indexIn(
200
201
              str.mid(detailStart + 10, detailEnd - detailStart - 11)) >= 0) {
201
202
          framesHdr.setYear(yearRe.cap(1).toInt());
203
204
      }
204
205
    }
205
206
    if (additionalTags) {
206
 
      detailStart = str.indexOf(">Label:<", start);
 
207
      detailStart = str.indexOf(QLatin1String(">Label:<"), start);
207
208
      if (detailStart > 0) {
208
 
        int detailEnd = str.indexOf("\n", detailStart + 8);
 
209
        int detailEnd = str.indexOf(QLatin1Char('\n'), detailStart + 8);
209
210
        if (detailEnd > detailStart + 8) {
210
 
          QRegExp labelRe(">\\s*([^<]+)<");
 
211
          QRegExp labelRe(QLatin1String(">\\s*([^<]+)<"));
211
212
          if (labelRe.indexIn(
212
213
                str.mid(detailStart + 8, detailEnd - detailStart - 9)) >= 0) {
213
214
            framesHdr.setValue(Frame::FT_Publisher, removeHtml(labelRe.cap(1)));
214
215
          }
215
216
        }
216
217
      }
217
 
      detailStart = str.indexOf(">Performer:<", start);
 
218
      detailStart = str.indexOf(QLatin1String(">Performer:<"), start);
218
219
      if (detailStart > 0) {
219
 
        int detailEnd = str.indexOf("</li>", detailStart + 12);
 
220
        int detailEnd = str.indexOf(QLatin1String("</li>"), detailStart + 12);
220
221
        if (detailEnd > detailStart + 12) {
221
222
          framesHdr.setValue(
222
223
            Frame::FT_Performer,
223
224
            removeHtml(str.mid(detailStart + 11, detailEnd - detailStart - 11)));
224
225
        }
225
226
      }
226
 
      detailStart = str.indexOf(">Orchestra:<", start);
 
227
      detailStart = str.indexOf(QLatin1String(">Orchestra:<"), start);
227
228
      if (detailStart > 0) {
228
 
        int detailEnd = str.indexOf("</li>", detailStart + 12);
 
229
        int detailEnd = str.indexOf(QLatin1String("</li>"), detailStart + 12);
229
230
        if (detailEnd > detailStart + 12) {
230
231
          albumArtist =
231
232
            removeHtml(str.mid(detailStart + 11, detailEnd - detailStart - 11));
232
233
        }
233
234
      }
234
 
      detailStart = str.indexOf(">Conductor:<", start);
 
235
      detailStart = str.indexOf(QLatin1String(">Conductor:<"), start);
235
236
      if (detailStart > 0) {
236
 
        int detailEnd = str.indexOf("</li>", detailStart + 12);
 
237
        int detailEnd = str.indexOf(QLatin1String("</li>"), detailStart + 12);
237
238
        if (detailEnd > detailStart + 12) {
238
239
          framesHdr.setValue(
239
240
            Frame::FT_Conductor,
240
241
            removeHtml(str.mid(detailStart + 11, detailEnd - detailStart - 11)));
241
242
        }
242
243
      }
243
 
      detailStart = str.indexOf(">Composer:<", start);
 
244
      detailStart = str.indexOf(QLatin1String(">Composer:<"), start);
244
245
      if (detailStart > 0) {
245
 
        int detailEnd = str.indexOf("</li>", detailStart + 11);
 
246
        int detailEnd = str.indexOf(QLatin1String("</li>"), detailStart + 11);
246
247
        if (detailEnd > detailStart + 11) {
247
248
          framesHdr.setValue(
248
249
            Frame::FT_Composer,
253
254
  }
254
255
 
255
256
  ImportTrackDataVector trackDataVector(m_trackDataModel->getTrackData());
 
257
  trackDataVector.setCoverArtUrl(QString());
256
258
  if (getCoverArt()) {
257
259
    // <input type="hidden" id="ASIN" name="ASIN" value="B0025AY48W" />
258
 
    start = str.indexOf("id=\"ASIN\"");
 
260
    start = str.indexOf(QLatin1String("id=\"ASIN\""));
259
261
    if (start > 0) {
260
 
      start = str.indexOf("value=\"", start);
 
262
      start = str.indexOf(QLatin1String("value=\""), start);
261
263
      if (start > 0) {
262
 
        end = str.indexOf("\"", start + 7);
 
264
        end = str.indexOf(QLatin1Char('"'), start + 7);
263
265
        if (end > start) {
264
266
          trackDataVector.setCoverArtUrl(
265
 
            QString("http://www.amazon.com/dp/") +
 
267
            QLatin1String("http://www.amazon.com/dp/") +
266
268
            str.mid(start + 7, end - start - 7));
267
269
        }
268
270
      }
270
272
  }
271
273
 
272
274
  bool hasTitleCol = false;
273
 
  bool hasArtist = str.indexOf("<td>Song Title</td><td>Artist</td>") != -1;
 
275
  bool hasArtist = str.indexOf(QLatin1String("<td>Song Title</td><td>Artist</td>")) != -1;
274
276
  // search 'class="titleCol"', next '<a href=', text after '>' until '<'
275
277
  // => title
276
278
  // if not found: alternatively look for 'class="listRow'
277
 
  start = str.indexOf("class=\"titleCol\"");
 
279
  start = str.indexOf(QLatin1String("class=\"titleCol\""));
278
280
  if (start >= 0) {
279
281
    hasTitleCol = true;
280
282
  } else {
281
 
    start = str.indexOf("class=\"listRow");
 
283
    start = str.indexOf(QLatin1String("class=\"listRow"));
282
284
  }
283
285
  if (start >= 0) {
284
 
    QRegExp durationRe("(\\d+):(\\d+)");
285
 
    QRegExp nrTitleRe("\\s*\\d+\\.\\s+(.*\\S)");
 
286
    QRegExp durationRe(QLatin1String("(\\d+):(\\d+)"));
 
287
    QRegExp nrTitleRe(QLatin1String("\\s*\\d+\\.\\s+(.*\\S)"));
286
288
    FrameCollection frames(framesHdr);
287
289
    ImportTrackDataVector::iterator it = trackDataVector.begin();
288
290
    bool atTrackDataListEnd = (it == trackDataVector.end());
292
294
      QString artist;
293
295
      int duration = 0;
294
296
      if (hasTitleCol) {
295
 
        end = str.indexOf("\n", start);
 
297
        end = str.indexOf(QLatin1Char('\n'), start);
296
298
        if (end > start) {
297
299
          QString line = str.mid(start, end - start);
298
 
          int titleStart = line.indexOf("<a href=");
 
300
          int titleStart = line.indexOf(QLatin1String("<a href="));
299
301
          if (titleStart >= 0) {
300
 
            titleStart = line.indexOf(">", titleStart);
 
302
            titleStart = line.indexOf(QLatin1Char('>'), titleStart);
301
303
            if (titleStart >= 0) {
302
 
              int titleEnd = line.indexOf("<", titleStart);
 
304
              int titleEnd = line.indexOf(QLatin1Char('<'), titleStart);
303
305
              if (titleEnd > titleStart) {
304
306
                title = line.mid(titleStart + 1, titleEnd - titleStart - 1);
305
307
                // if there was an Artist title,
306
308
                // search for artist in a second titleCol
307
309
                if (hasArtist) {
308
310
                  int artistStart =
309
 
                    line.indexOf("class=\"titleCol\"", titleEnd);
 
311
                    line.indexOf(QLatin1String("class=\"titleCol\""), titleEnd);
310
312
                  if (artistStart >= 0) {
311
 
                    artistStart = line.indexOf("<a href=", artistStart);
 
313
                    artistStart = line.indexOf(QLatin1String("<a href="), artistStart);
312
314
                    if (artistStart >= 0) {
313
 
                      artistStart = line.indexOf(">", artistStart);
 
315
                      artistStart = line.indexOf(QLatin1Char('>'), artistStart);
314
316
                      if (artistStart >= 0) {
315
 
                        int artistEnd = line.indexOf("<", artistStart);
 
317
                        int artistEnd = line.indexOf(QLatin1Char('<'), artistStart);
316
318
                        if (artistEnd > artistStart) {
317
319
                          artist = line.mid(
318
320
                            artistStart + 1, artistEnd - artistStart - 1);
327
329
                // search for next 'class="', if it is 'class="runtimeCol"',
328
330
                // text after '>' until '<' => duration
329
331
                int runtimeStart =
330
 
                  line.indexOf("class=\"runtimeCol\"", titleEnd);
 
332
                  line.indexOf(QLatin1String("class=\"runtimeCol\""), titleEnd);
331
333
                if (runtimeStart >= 0) {
332
 
                  runtimeStart = line.indexOf(">", runtimeStart + 18);
 
334
                  runtimeStart = line.indexOf(QLatin1Char('>'), runtimeStart + 18);
333
335
                  if (runtimeStart >= 0) {
334
 
                    int runtimeEnd = line.indexOf("<", runtimeStart);
 
336
                    int runtimeEnd = line.indexOf(QLatin1Char('<'), runtimeStart);
335
337
                    if (runtimeEnd > runtimeStart) {
336
338
                      if (durationRe.indexIn(
337
339
                            line.mid(runtimeStart + 1,
342
344
                    }
343
345
                  }
344
346
                }
345
 
                start = str.indexOf("class=\"titleCol\"", end);
 
347
                start = str.indexOf(QLatin1String("class=\"titleCol\""), end);
346
348
              } else {
347
349
                start = -1;
348
350
              }
351
353
        }
352
354
      } else {
353
355
        // 'class="listRow' found
354
 
        start = str.indexOf("<td>", start);
 
356
        start = str.indexOf(QLatin1String("<td>"), start);
355
357
        if (start >= 0) {
356
 
          end = str.indexOf("</td>", start);
 
358
          end = str.indexOf(QLatin1String("</td>"), start);
357
359
          if (end > start &&
358
360
              nrTitleRe.indexIn(str.mid(start + 4, end - start - 4)) >= 0) {
359
361
            title = nrTitleRe.cap(1);
360
 
            start = str.indexOf("class=\"listRow", end);
 
362
            start = str.indexOf(QLatin1String("class=\"listRow"), end);
361
363
          } else {
362
364
            start = -1;
363
365
          }
364
366
        }
365
367
      }
366
368
      if (!title.isEmpty()) {
367
 
        frames.setTitle(replaceHtmlEntities(title));
368
 
        if (!artist.isEmpty()) {
369
 
          frames.setArtist(replaceHtmlEntities(artist));
 
369
        if (standardTags) {
 
370
          frames.setTitle(replaceHtmlEntities(title));
 
371
          if (!artist.isEmpty()) {
 
372
            frames.setArtist(replaceHtmlEntities(artist));
 
373
          }
 
374
          frames.setTrack(trackNr);
370
375
        }
371
376
        if (!albumArtist.isEmpty() && additionalTags) {
372
377
          frames.setValue(Frame::FT_AlbumArtist, albumArtist);
373
378
        }
374
 
        frames.setTrack(trackNr);
375
379
        if (atTrackDataListEnd) {
376
380
          ImportTrackData trackData;
377
381
          trackData.setFrameCollection(frames);
439
443
   * http://www.amazon.com/gp/search/ref=sr_adv_m_pop/?search-alias=popular&field-artist=amon+amarth&field-title=the+avenger
440
444
   */
441
445
  sendRequest(cfg->m_server,
442
 
              QString("/gp/search/ref=sr_adv_m_pop/"
 
446
              QLatin1String("/gp/search/ref=sr_adv_m_pop/"
443
447
                      "?search-alias=popular&field-artist=") +
444
 
              encodeUrlQuery(artist) + "&field-title=" + encodeUrlQuery(album));
 
448
              encodeUrlQuery(artist) + QLatin1String("&field-title=") + encodeUrlQuery(album));
445
449
}
446
450
 
447
451
/**
459
463
   * Query looks like this:
460
464
   * http://www.amazon.com/dp/B001VROVHO
461
465
   */
462
 
  sendRequest(cfg->m_server, QString("/") + cat + '/' + id);
 
466
  sendRequest(cfg->m_server, QLatin1Char('/') + cat + QLatin1Char('/') + id);
463
467
}