summaryrefslogtreecommitdiffstats
path: root/src/fetch/yahoofetcher.cpp
blob: 474fc7ce916df4e665bce484d19e19c1ba79e698 (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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/***************************************************************************
    copyright            : (C) 2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "yahoofetcher.h"
#include "messagehandler.h"
#include "../translators/xslthandler.h"
#include "../translators/tellicoimporter.h"
#include "../imagefactory.h"
#include "../tellico_kernel.h"
#include "../tellico_utils.h"
#include "../collection.h"
#include "../entry.h"
#include "../tellico_debug.h"

#include <tdelocale.h>
#include <kstandarddirs.h>
#include <tdeconfig.h>
#include <tdeio/job.h>

#include <tqdom.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqfile.h>

namespace {
  static const int YAHOO_MAX_RETURNS_TOTAL = 20;
  static const char* YAHOO_BASE_URL = "http://search.yahooapis.com/AudioSearchService/V1/albumSearch";
  static const char* YAHOO_APP_ID = "tellico-robby";
}

using Tellico::Fetch::YahooFetcher;

YahooFetcher::YahooFetcher(TQObject* parent_, const char* name_)
    : Fetcher(parent_, name_), m_xsltHandler(0),
      m_limit(YAHOO_MAX_RETURNS_TOTAL), m_job(0), m_started(false) {
}

YahooFetcher::~YahooFetcher() {
  delete m_xsltHandler;
  m_xsltHandler = 0;
}

TQString YahooFetcher::defaultName() {
  return i18n("Yahoo! Audio Search");
}

TQString YahooFetcher::source() const {
  return m_name.isEmpty() ? defaultName() : m_name;
}

bool YahooFetcher::canFetch(int type) const {
  return type == Data::Collection::Album;
}

void YahooFetcher::readConfigHook(const TDEConfigGroup& config_) {
  Q_UNUSED(config_);
}

void YahooFetcher::search(FetchKey key_, const TQString& value_) {
  m_key = key_;
  m_value = value_;
  m_started = true;
  m_start = 1;
  m_total = -1;
  doSearch();
}

void YahooFetcher::continueSearch() {
  m_started = true;
  doSearch();
}

void YahooFetcher::doSearch() {
//  myDebug() << "YahooFetcher::search() - value = " << value_ << endl;

  KURL u(TQString::fromLatin1(YAHOO_BASE_URL));
  u.addQueryItem(TQString::fromLatin1("appid"),   TQString::fromLatin1(YAHOO_APP_ID));
  u.addQueryItem(TQString::fromLatin1("type"),    TQString::fromLatin1("all"));
  u.addQueryItem(TQString::fromLatin1("output"),  TQString::fromLatin1("xml"));
  u.addQueryItem(TQString::fromLatin1("start"),   TQString::number(m_start));
  u.addQueryItem(TQString::fromLatin1("results"), TQString::number(YAHOO_MAX_RETURNS_TOTAL));

  if(!canFetch(Kernel::self()->collectionType())) {
    message(i18n("%1 does not allow searching for this collection type.").arg(source()), MessageHandler::Warning);
    stop();
    return;
  }

  switch(m_key) {
    case Title:
      u.addQueryItem(TQString::fromLatin1("album"), m_value);
      break;

    case Person:
      u.addQueryItem(TQString::fromLatin1("artist"), m_value);
      break;

    // raw is used for the entry updates
    case Raw:
//      u.removeQueryItem(TQString::fromLatin1("type"));
//      u.addQueryItem(TQString::fromLatin1("type"), TQString::fromLatin1("phrase"));
      u.setQuery(u.query() + '&' + m_value);
      break;

    default:
      kdWarning() << "YahooFetcher::search() - key not recognized: " << m_key << endl;
      stop();
      return;
  }
//  myDebug() << "YahooFetcher::search() - url: " << u.url() << endl;

  m_job = TDEIO::get(u, false, false);
  connect(m_job, TQ_SIGNAL(data(TDEIO::Job*, const TQByteArray&)),
          TQ_SLOT(slotData(TDEIO::Job*, const TQByteArray&)));
  connect(m_job, TQ_SIGNAL(result(TDEIO::Job*)),
          TQ_SLOT(slotComplete(TDEIO::Job*)));
}

void YahooFetcher::stop() {
  if(!m_started) {
    return;
  }
  if(m_job) {
    m_job->kill();
    m_job = 0;
  }
  m_data.truncate(0);
  m_started = false;
  emit signalDone(this);
}

void YahooFetcher::slotData(TDEIO::Job*, const TQByteArray& data_) {
  TQDataStream stream(m_data, IO_WriteOnly | IO_Append);
  stream.writeRawBytes(data_.data(), data_.size());
}

void YahooFetcher::slotComplete(TDEIO::Job* job_) {
//  myDebug() << "YahooFetcher::slotComplete()" << endl;
  // since the fetch is done, don't worry about holding the job pointer
  m_job = 0;

  if(job_->error()) {
    job_->showErrorDialog(Kernel::self()->widget());
    stop();
    return;
  }

  if(m_data.isEmpty()) {
    myDebug() << "YahooFetcher::slotComplete() - no data" << endl;
    stop();
    return;
  }

#if 0
  kdWarning() << "Remove debug from yahoofetcher.cpp" << endl;
  TQFile f(TQString::fromLatin1("/tmp/test.xml"));
  if(f.open(IO_WriteOnly)) {
    TQTextStream t(&f);
    t.setEncoding(TQTextStream::UnicodeUTF8);
    t << TQCString(m_data, m_data.size()+1);
  }
  f.close();
#endif

  if(!m_xsltHandler) {
    initXSLTHandler();
    if(!m_xsltHandler) { // probably an error somewhere in the stylesheet loading
      stop();
      return;
    }
  }

  if(m_total == -1) {
    TQDomDocument dom;
    if(!dom.setContent(m_data, false)) {
      kdWarning() << "YahooFetcher::slotComplete() - server did not return valid XML." << endl;
      return;
    }
    // total is top level element, with attribute totalResultsAvailable
    TQDomElement e = dom.documentElement();
    if(!e.isNull()) {
      m_total = e.attribute(TQString::fromLatin1("totalResultsAvailable")).toInt();
    }
  }

  // assume yahoo is always utf-8
  TQString str = m_xsltHandler->applyStylesheet(TQString::fromUtf8(m_data, m_data.size()));
  Import::TellicoImporter imp(str);
  Data::CollPtr coll = imp.collection();
  if(!coll) {
    myDebug() << "YahooFetcher::slotComplete() - no collection pointer" << endl;
    stop();
    return;
  }

  int count = 0;
  Data::EntryVec entries = coll->entries();
  for(Data::EntryVec::Iterator entry = entries.begin(); count < m_limit && entry != entries.end(); ++entry, ++count) {
    if(!m_started) {
      // might get aborted
      break;
    }
    TQString desc = entry->field(TQString::fromLatin1("artist"))
                 + TQChar('/')
                 + entry->field(TQString::fromLatin1("label"))
                 + TQChar('/')
                 + entry->field(TQString::fromLatin1("year"));

    SearchResult* r = new SearchResult(this, entry->title(), desc, entry->field(TQString::fromLatin1("isbn")));
    m_entries.insert(r->uid, Data::EntryPtr(entry));
    emit signalResultFound(r);
  }
  m_start = m_entries.count() + 1;
  m_hasMoreResults = m_start <= m_total;
  stop(); // required
}

Tellico::Data::EntryPtr YahooFetcher::fetchEntry(uint uid_) {
  Data::EntryPtr entry = m_entries[uid_];
  if(!entry) {
    kdWarning() << "YahooFetcher::fetchEntry() - no entry in dict" << endl;
    return 0;
  }

  KURL imageURL = entry->field(TQString::fromLatin1("image"));
  if(!imageURL.isEmpty()) {
    TQString id = ImageFactory::addImage(imageURL, true);
    if(id.isEmpty()) {
    // rich text causes layout issues
//      emit signalStatus(i18n("<qt>The cover image for <i>%1</i> could not be loaded.</qt>").arg(
//                              entry->field(TQString::fromLatin1("title"))));
      message(i18n("The cover image could not be loaded."), MessageHandler::Warning);
    } else {
      entry->setField(TQString::fromLatin1("cover"), id);
    }
  }

  getTracks(entry);

  // don't want to show image urls in the fetch dialog
  entry->setField(TQString::fromLatin1("image"),  TQString());
  // no need for album id now ?
  entry->setField(TQString::fromLatin1("yahoo"),  TQString());
  return entry;
}

void YahooFetcher::initXSLTHandler() {
  TQString xsltfile = locate("appdata", TQString::fromLatin1("yahoo2tellico.xsl"));
  if(xsltfile.isEmpty()) {
    kdWarning() << "YahooFetcher::initXSLTHandler() - can not locate yahoo2tellico.xsl." << endl;
    return;
  }

  KURL u;
  u.setPath(xsltfile);

  delete m_xsltHandler;
  m_xsltHandler = new XSLTHandler(u);
  if(!m_xsltHandler->isValid()) {
    kdWarning() << "YahooFetcher::initXSLTHandler() - error in yahoo2tellico.xsl." << endl;
    delete m_xsltHandler;
    m_xsltHandler = 0;
    return;
  }
}

void YahooFetcher::getTracks(Data::EntryPtr entry_) {
  // get album id
  if(!entry_ || entry_->field(TQString::fromLatin1("yahoo")).isEmpty()) {
    return;
  }

  const TQString albumid = entry_->field(TQString::fromLatin1("yahoo"));

  KURL u(TQString::fromLatin1(YAHOO_BASE_URL));
  u.setFileName(TQString::fromLatin1("songSearch"));
  u.addQueryItem(TQString::fromLatin1("appid"),   TQString::fromLatin1(YAHOO_APP_ID));
  u.addQueryItem(TQString::fromLatin1("type"),    TQString::fromLatin1("all"));
  u.addQueryItem(TQString::fromLatin1("output"),  TQString::fromLatin1("xml"));
  // go ahesad and ask for all results, since there might well be more than 10 songs on the CD
  u.addQueryItem(TQString::fromLatin1("results"), TQString::number(50));
  u.addQueryItem(TQString::fromLatin1("albumid"), albumid);

//  myDebug() << "YahooFetcher::getTracks() - url: " << u.url() << endl;
  TQDomDocument dom = FileHandler::readXMLFile(u, false /*no namespace*/, true /*quiet*/);
  if(dom.isNull()) {
    myDebug() << "YahooFetcher::getTracks() - null dom returned" << endl;
    return;
  }

#if 0
  kdWarning() << "Remove debug from yahoofetcher.cpp" << endl;
  TQFile f(TQString::fromLatin1("/tmp/test.xml"));
  if(f.open(IO_WriteOnly)) {
    TQTextStream t(&f);
    t.setEncoding(TQTextStream::UnicodeUTF8);
    t << dom.toString();
  }
  f.close();
#endif

  const TQString track = TQString::fromLatin1("track");

  TQDomNodeList nodes = dom.documentElement().childNodes();
  for(uint i = 0; i < nodes.count(); ++i) {
    TQDomElement e = nodes.item(i).toElement();
    if(e.isNull()) {
      continue;
    }
    TQString t = e.namedItem(TQString::fromLatin1("Title")).toElement().text();
    TQString n = e.namedItem(TQString::fromLatin1("Track")).toElement().text();
    bool ok;
    int trackNum = Tellico::toUInt(n, &ok);
    // trackNum might be 0
    if(t.isEmpty() || !ok || trackNum < 1) {
      continue;
    }
    TQString a = e.namedItem(TQString::fromLatin1("Artist")).toElement().text();
    TQString l = e.namedItem(TQString::fromLatin1("Length")).toElement().text();

    int len = Tellico::toUInt(l, &ok);
    TQString value = t + "::" + a;
    if(ok && len > 0) {
      value += + "::" + Tellico::minutes(len);
    }
    entry_->setField(track, insertValue(entry_->field(track), value, trackNum));
  }
}

// not zero-based
TQString YahooFetcher::insertValue(const TQString& str_, const TQString& value_, uint pos_) {
  TQStringList list = Data::Field::split(str_, true);
  for(uint i = list.count(); i < pos_; ++i) {
    list += TQString();
  }
  bool write = true;
  if(!list[pos_-1].isNull()) {
    // for some reason, some songs are repeated from yahoo, with 0 length, don't overwrite that
    if(value_.contains(TQString::fromLatin1("::")) < 2) { // means no length value
      write = false;
    }
  }
  if(!value_.isEmpty() && write) {
    list[pos_-1] = value_;
  }
  return list.join(TQString::fromLatin1("; "));
}

void YahooFetcher::updateEntry(Data::EntryPtr entry_) {
//  myDebug() << "YahooFetcher::updateEntry()" << endl;
  // limit to top 5 results
  m_limit = 5;

  TQString value;
  TQString title = entry_->field(TQString::fromLatin1("title"));
  if(!title.isEmpty()) {
    value += TQString::fromLatin1("album=") + title;
  }
  TQString artist = entry_->field(TQString::fromLatin1("artist"));
  if(!artist.isEmpty()) {
    if(!value.isEmpty()) {
      value += '&';
    }
    value += TQString::fromLatin1("artist=") + artist;
  }
  if(!value.isEmpty()) {
    search(Fetch::Raw, value);
    return;
  }

  myDebug() << "YahooFetcher::updateEntry() - insufficient info to search" << endl;
  emit signalDone(this); // always need to emit this if not continuing with the search
}

Tellico::Fetch::ConfigWidget* YahooFetcher::configWidget(TQWidget* parent_) const {
  return new YahooFetcher::ConfigWidget(parent_, this);
}

YahooFetcher::ConfigWidget::ConfigWidget(TQWidget* parent_, const YahooFetcher*/*=0*/)
    : Fetch::ConfigWidget(parent_) {
  TQVBoxLayout* l = new TQVBoxLayout(optionsWidget());
  l->addWidget(new TQLabel(i18n("This source has no options."), optionsWidget()));
  l->addStretch();
}

TQString YahooFetcher::ConfigWidget::preferredName() const {
  return YahooFetcher::defaultName();
}

#include "yahoofetcher.moc"