244
/* BEGIN_COMMON_COPYRIGHT_HEADER
247
* Copyright: 2012 Labo A.L
249
* Aaron Lewis <the.warl0ck.1989@gmail.com>
251
* This program or library is free software; you can redistribute it
252
* and/or modify it under the terms of the GNU Lesser General Public
253
* License as published by the Free Software Foundation; either
254
* version 2.1 of the License, or (at your option) any later version.
256
* This library is distributed in the hope that it will be useful,
257
* but WITHOUT ANY WARRANTY; without even the implied warranty of
258
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
259
* Lesser General Public License for more details.
261
* You should have received a copy of the GNU Lesser General
262
* Public License along with this library; if not, write to the
263
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
264
* Boston, MA 02110-1301 USA
266
* END_COMMON_COPYRIGHT_HEADER */
267
#include "provider.h"
269
Provider::Provider(QStandardItemModel *model, QObject *parent) :
272
m_nam (new QNetworkAccessManager (this)),
278
Provider::~Provider()
283
void Provider::addItem (const QString &icon, const QString &url,
284
const QString &name, const QString &subtitle)
286
m_model->appendRow(new ProviderItem (url, name, icon, subtitle));
290
TedProvider::TedProvider(QStandardItemModel *model, QObject *parent):
291
Provider(model, parent)
293
m_icon = ":/images/ted.jpg";
294
connect (m_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
297
TedProvider::~TedProvider()
299
if ( m_reply != NULL )
303
void TedProvider::start()
305
if ( m_reply != NULL )
307
m_reply = m_nam->get(QNetworkRequest(QUrl::fromEncoded("http://www.ted.com/talks/rss")));
310
void TedProvider::finished(QNetworkReply *reply)
312
if ( reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200 )
314
qDebug() << "TedProvider: ERROR loading contents";
318
QDomDocument doc; doc.setContent(reply->readAll());
319
QDomNode channel = doc.documentElement().firstChild();
320
QString videoTitle , videoUrl;
322
if ( ! channel.isNull() )
324
QDomNode item = channel.firstChild();
325
while ( ! item.isNull() )
327
videoTitle.clear(); videoUrl.clear();
329
/* Scan for details for each item */
330
QDomElement detail = item.firstChild().toElement();
331
while ( ! detail.isNull() )
333
if ( detail.tagName() == "title" )
335
videoTitle = detail.text().remove( QRegExp("^TED:\\s+") );
338
if ( detail.tagName() == "media:content" )
340
videoUrl = detail.attribute("url");
343
/* No other tags needed , break the loop */
344
if ( ! videoTitle.isEmpty() && ! videoUrl.isEmpty() )
346
addItem(m_icon, videoUrl, videoTitle);
350
detail = detail.nextSibling().toElement();
353
item = item.nextSibling();
361
SciAmericanProvider::SciAmericanProvider (QStandardItemModel *model, QObject *parent):
362
Provider(model, parent)
364
m_icon = ":/images/sci.jpg";
366
page.settings()->setAttribute(QWebSettings::JavascriptEnabled , false);
367
page.settings()->setAttribute(QWebSettings::AutoLoadImages , false);
368
page.settings()->setAttribute(QWebSettings::JavaEnabled , false);
370
connect (&page, SIGNAL(loadFinished(bool)), this, SLOT(fini(bool)));
373
SciAmericanProvider::~SciAmericanProvider()
378
#define SCIAMERICAN_BASEURL "http://www.scientificamerican.com/podcast/podcasts.cfm?id=60-second-science"
379
void SciAmericanProvider::start()
381
page.mainFrame()->load(QString(SCIAMERICAN_BASEURL));
384
void SciAmericanProvider::fini(bool ok)
388
qDebug() << "SciAmericanProvider: ERROR loading contents";
392
QWebPage *page = qobject_cast<QWebPage*> (sender());
394
const QString & url = page->mainFrame()->url().toString();
396
if ( url == SCIAMERICAN_BASEURL )
398
QString videoTitle , videoUrl;
400
QWebElementCollection lis = page->mainFrame()->documentElement().findAll("li");
401
foreach (const QWebElement & li , lis) if ( li.classes().contains("message_box") )
404
QWebElementCollection aLinks = li.findAll("a");
405
foreach (const QWebElement & a , aLinks)
407
videoTitle = a.toPlainText();
408
videoUrl = a.attribute("href");
410
if ( ! videoTitle.isEmpty() && videoUrl.contains("episode.cfm") )
412
QStandardItem *item = new QStandardItem (QIcon(":/images/loading.gif"), videoTitle);
413
m_model->appendRow(item);
415
mapping[videoUrl] = item;
417
QWebPage *page = new QWebPage;
418
page->settings()->setAttribute(QWebSettings::JavascriptEnabled , false);
419
page->settings()->setAttribute(QWebSettings::AutoLoadImages , false);
420
page->settings()->setAttribute(QWebSettings::JavaEnabled , false);
422
connect (page, SIGNAL(loadFinished(bool)), this, SLOT(fini(bool)));
423
page->mainFrame()->load(videoUrl);
435
if ( url.indexOf("http://www.scientificamerican.com/podcast/episode.cfm?id=") == 0 )
437
QString mp3Url , transcript;
439
const QWebElementCollection & divs = page->mainFrame()->documentElement().findAll("div");
440
foreach (const QWebElement & div , divs) if ( div.attribute("id") == "articleContent" )
443
const QWebElementCollection & paragraphs = div.findAll("p");
444
foreach (const QWebElement & p , paragraphs)
446
const QString & xml = p.toInnerXml();
447
// if ( xml.contains("<strong>Read More") )
457
transcript.append("<br/>");
461
const QWebElementCollection & aLinks = page->mainFrame()->documentElement().findAll("a");
462
foreach (const QWebElement & a , aLinks)
464
if ( a.attribute("href").indexOf("http://www.scientificamerican.com/podcast/podcast.mp3") == 0 )
466
mp3Url = a.attribute("href");
471
if ( transcript.isEmpty() )
473
transcript = tr("No transcript ? Possibly bug, please report to "
474
"<b>Aaron Lewis (the.warl0ck.1989@gmail.com)</b>");
477
QStandardItem *item = mapping.value(url);
478
item->setIcon(QIcon(":/images/sci.jpg"));
479
item->setData(mp3Url, Qt::UserRole);
480
item->setData(transcript, Qt::UserRole + OFFSET_TRANSCRIPT);