~blue-shell/blue-shell/flickr-runner

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
/******************************************************************************
 *  Copyright (C) 2012 by Shaun Reich <shaun.reich@blue-systems.com           *
 *                                                                            *
 *  This library is free software; you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as published  *
 *  by the Free Software Foundation; either version 2 of the License or (at   *
 *  your option) any later version.                                           *
 *                                                                            *
 *  This library 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         *
 *  Library General Public License for more details.                          *
 *                                                                            *
 *  You should have received a copy of the GNU Lesser General Public License  *
 *  along with this library; see the file COPYING.LIB.                        *
 *  If not, see <http://www.gnu.org/licenses/>.                               *
 *****************************************************************************/

#include "flickjob.h"
#include <QtCore/qurl.h>

#include <KDebug>
#include <KToolInvocation>

#include <QNetworkAccessManager>
#include <QNetworkReply>

FlickJob::FlickJob(const QString& term, bool singleRunnerMode)
  : QObject()
  , m_manager(0)
{
    m_manager = new QNetworkAccessManager(this);

    int resultCount = singleRunnerMode ? 30 : 10;
    QUrl url("http://api.flickr.com/services/rest/");
    url.addQueryItem("method", "flickr.photos.search");
    url.addQueryItem("api_key", "37e8cfe947339a756f5045b9b0d445cd");
    url.addQueryItem("tags", term);
    url.addQueryItem("format", "json");
    url.addQueryItem("nojsoncallback","1");
    url.addQueryItem("per_page", QString::number(resultCount));
    url.addQueryItem("page", QString::number(1));
    url.addQueryItem("sort","relevance");

    QNetworkRequest request = QNetworkRequest(url);
    m_manager->get(request);

    connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(jobCompleted(QNetworkReply*)));
}

void FlickJob::jobCompleted(QNetworkReply* reply)
{
    m_data = reply->readAll();

    emit finished();
}

QByteArray FlickJob::data()
{
    return m_data;
}


#include "flickjob.moc"