~john.vrbanac/ubuntu-app-reviews/minicast

« back to all changes in this revision

Viewing changes to provider.cpp

  • Committer: Aaron Lewis
  • Date: 2012-07-09 08:00:58 UTC
  • Revision ID: the.warl0ck.1989@gmail.com-20120709080058-h0x8cbtuqrza88k4
Fix duplicate file contents

Show diffs side-by-side

added added

removed removed

Lines of Context:
241
241
    }
242
242
 
243
243
}
244
 
/* BEGIN_COMMON_COPYRIGHT_HEADER
245
 
 * (c)LGPL2+
246
 
 *
247
 
 * Copyright: 2012 Labo A.L
248
 
 * Authors:
249
 
 *   Aaron Lewis <the.warl0ck.1989@gmail.com>
250
 
 *
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.
255
 
 *
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.
260
 
 
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
265
 
 *
266
 
 * END_COMMON_COPYRIGHT_HEADER */
267
 
#include "provider.h"
268
 
 
269
 
Provider::Provider(QStandardItemModel *model, QObject *parent) :
270
 
    QObject(parent),
271
 
    m_model (model),
272
 
    m_nam (new QNetworkAccessManager (this)),
273
 
    m_reply (NULL),
274
 
    m_playOptions (None)
275
 
{
276
 
}
277
 
 
278
 
Provider::~Provider()
279
 
{
280
 
 
281
 
}
282
 
 
283
 
void Provider::addItem (const QString &icon, const QString &url,
284
 
                        const QString &name, const QString &subtitle)
285
 
{
286
 
    m_model->appendRow(new ProviderItem (url, name, icon, subtitle));
287
 
}
288
 
 
289
 
//////
290
 
TedProvider::TedProvider(QStandardItemModel *model, QObject *parent):
291
 
    Provider(model, parent)
292
 
{
293
 
    m_icon = ":/images/ted.jpg";
294
 
    connect (m_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
295
 
}
296
 
 
297
 
TedProvider::~TedProvider()
298
 
{
299
 
    if ( m_reply != NULL )
300
 
        m_reply->abort();
301
 
}
302
 
 
303
 
void TedProvider::start()
304
 
{
305
 
    if ( m_reply != NULL )
306
 
        m_reply->abort();
307
 
    m_reply = m_nam->get(QNetworkRequest(QUrl::fromEncoded("http://www.ted.com/talks/rss")));
308
 
}
309
 
 
310
 
void TedProvider::finished(QNetworkReply *reply)
311
 
{
312
 
    if ( reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200 )
313
 
    {
314
 
        qDebug() << "TedProvider: ERROR loading contents";
315
 
        return;
316
 
    }
317
 
 
318
 
    QDomDocument doc; doc.setContent(reply->readAll());
319
 
    QDomNode channel = doc.documentElement().firstChild();
320
 
    QString videoTitle , videoUrl;
321
 
 
322
 
    if ( ! channel.isNull() )
323
 
    {
324
 
        QDomNode item = channel.firstChild();
325
 
        while ( ! item.isNull() )
326
 
        {
327
 
            videoTitle.clear(); videoUrl.clear();
328
 
 
329
 
            /* Scan for details for each item */
330
 
            QDomElement detail = item.firstChild().toElement();
331
 
            while ( ! detail.isNull() )
332
 
            {
333
 
                if ( detail.tagName() == "title" )
334
 
                {
335
 
                    videoTitle = detail.text().remove( QRegExp("^TED:\\s+") );
336
 
                }
337
 
 
338
 
                if ( detail.tagName() == "media:content" )
339
 
                {
340
 
                    videoUrl = detail.attribute("url");
341
 
                }
342
 
 
343
 
                /* No other tags needed , break the loop */
344
 
                if ( ! videoTitle.isEmpty() && ! videoUrl.isEmpty() )
345
 
                {
346
 
                    addItem(m_icon, videoUrl, videoTitle);
347
 
                    break;
348
 
                }
349
 
 
350
 
                detail = detail.nextSibling().toElement();
351
 
            }
352
 
 
353
 
            item = item.nextSibling();
354
 
        }
355
 
    }
356
 
 
357
 
    emit loaded();
358
 
 
359
 
}
360
 
 
361
 
SciAmericanProvider::SciAmericanProvider (QStandardItemModel *model, QObject *parent):
362
 
    Provider(model, parent)
363
 
{
364
 
    m_icon = ":/images/sci.jpg";
365
 
 
366
 
    page.settings()->setAttribute(QWebSettings::JavascriptEnabled , false);
367
 
    page.settings()->setAttribute(QWebSettings::AutoLoadImages , false);
368
 
    page.settings()->setAttribute(QWebSettings::JavaEnabled , false);
369
 
 
370
 
    connect (&page, SIGNAL(loadFinished(bool)), this, SLOT(fini(bool)));
371
 
}
372
 
 
373
 
SciAmericanProvider::~SciAmericanProvider()
374
 
{
375
 
 
376
 
}
377
 
 
378
 
#define SCIAMERICAN_BASEURL "http://www.scientificamerican.com/podcast/podcasts.cfm?id=60-second-science"
379
 
void SciAmericanProvider::start()
380
 
{
381
 
    page.mainFrame()->load(QString(SCIAMERICAN_BASEURL));
382
 
}
383
 
 
384
 
void SciAmericanProvider::fini(bool ok)
385
 
{
386
 
    if ( ! ok )
387
 
    {
388
 
        qDebug() << "SciAmericanProvider: ERROR loading contents";
389
 
        return;
390
 
    }
391
 
 
392
 
    QWebPage *page = qobject_cast<QWebPage*> (sender());
393
 
 
394
 
    const QString & url = page->mainFrame()->url().toString();
395
 
 
396
 
    if ( url == SCIAMERICAN_BASEURL )
397
 
    {
398
 
        QString videoTitle , videoUrl;
399
 
 
400
 
        QWebElementCollection lis = page->mainFrame()->documentElement().findAll("li");
401
 
        foreach (const QWebElement & li , lis) if ( li.classes().contains("message_box") )
402
 
        {
403
 
 
404
 
            QWebElementCollection aLinks = li.findAll("a");
405
 
            foreach (const QWebElement & a , aLinks)
406
 
            {
407
 
                videoTitle = a.toPlainText();
408
 
                videoUrl = a.attribute("href");
409
 
 
410
 
                if ( ! videoTitle.isEmpty() && videoUrl.contains("episode.cfm") )
411
 
                {
412
 
                    QStandardItem *item = new QStandardItem (QIcon(":/images/loading.gif"), videoTitle);
413
 
                    m_model->appendRow(item);
414
 
 
415
 
                    mapping[videoUrl] = item;
416
 
 
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);
421
 
 
422
 
                    connect (page, SIGNAL(loadFinished(bool)), this, SLOT(fini(bool)));
423
 
                    page->mainFrame()->load(videoUrl);
424
 
 
425
 
                    break;
426
 
                }
427
 
            }
428
 
 
429
 
        }
430
 
 
431
 
        emit loaded();
432
 
 
433
 
    }
434
 
 
435
 
    if ( url.indexOf("http://www.scientificamerican.com/podcast/episode.cfm?id=") == 0 )
436
 
    {
437
 
        QString mp3Url , transcript;
438
 
 
439
 
        const QWebElementCollection & divs = page->mainFrame()->documentElement().findAll("div");
440
 
        foreach (const QWebElement & div , divs) if ( div.attribute("id") == "articleContent" )
441
 
        {
442
 
            bool found = false;
443
 
            const QWebElementCollection & paragraphs = div.findAll("p");
444
 
            foreach (const QWebElement & p , paragraphs)
445
 
            {
446
 
                const QString & xml = p.toInnerXml();
447
 
//                if ( xml.contains("<strong>Read More") )
448
 
//                {
449
 
//                    found = true;
450
 
//                    continue;
451
 
//                }
452
 
//
453
 
//                if ( ! found )
454
 
//                    continue;
455
 
 
456
 
                transcript += xml;
457
 
                transcript.append("<br/>");
458
 
            }
459
 
        }
460
 
 
461
 
        const QWebElementCollection & aLinks = page->mainFrame()->documentElement().findAll("a");
462
 
        foreach (const QWebElement & a , aLinks)
463
 
        {
464
 
            if ( a.attribute("href").indexOf("http://www.scientificamerican.com/podcast/podcast.mp3") == 0 )
465
 
            {
466
 
                mp3Url = a.attribute("href");
467
 
                break;
468
 
            }
469
 
        }
470
 
 
471
 
        if ( transcript.isEmpty() )
472
 
        {
473
 
            transcript = tr("No transcript ? Possibly bug, please report to "
474
 
                            "<b>Aaron Lewis (the.warl0ck.1989@gmail.com)</b>");
475
 
        }
476
 
 
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);
481
 
 
482
 
        // delete this
483
 
        page->deleteLater();
484
 
    }
485
 
 
486
 
}