-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbApi.cpp
157 lines (127 loc) · 4.03 KB
/
fbApi.cpp
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
#include "fbApi.h"
#include <QString>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QObject>
#include <QVariantMap>
#include <stdio.h>
#include <qjson/parser.h>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <KConfig>
#include <KConfigGroup>
#include <QWebPage>
#include <QWebFrame>
#include <QWebElement>
#include <Magick++.h>
FbApi::FbApi(QObject * parent):QObject(parent){
id="";
token="";
net=new QNetworkAccessManager(this);
KConfigGroup imgConfig(&config,"Images");
picList = new QList <QString> (imgConfig.readEntry("images",QList<QString>()));
user=new QString(imgConfig.readEntry("user",QString()));
pass=new QString(imgConfig.readEntry("pass",QString()));
page = new QWebPage(this);
imgWidth=320;
imgHeight=240;
connect(page,SIGNAL(loadFinished(bool)),this,SLOT(doAuthentication(bool)));
}
void FbApi::authenticate(){
page->mainFrame()->load(QUrl(getLoginUrl()));
}
void FbApi::doAuthentication(bool stat){
if(stat){
qDebug() << page->mainFrame()->url();
QUrl url=QUrl(page->mainFrame()->url().toString().replace("#access_token","?access_token"));
if(url.hasQueryItem("access_token")){
qDebug() << "Yei: \n\n";
qDebug() << url.queryItemValue("access_token");
//qDebug() << *pass;
token=url.queryItemValue("access_token");
getPicsId();
}
else if(url.hasQueryItem("login_attempt")){
qDebug()<<"Wrong user/password";
}
else{
page->mainFrame()->findFirstElement("input#email").setAttribute("value",*user);
page->mainFrame()->findFirstElement("input#pass").setAttribute("value",*pass);
page->mainFrame()->evaluateJavaScript("document.getElementById(\"login_form\").submit()");
}
}
}
QUrl FbApi::getLoginUrl(){
QString * url=new QString(fbUrl);
return QUrl(url->replace("YOUR_APP_ID",id,Qt::CaseSensitive));
}
void FbApi::getPicsId(){
QString * url=new QString(getTagPics);
resp=net->get(QNetworkRequest(QUrl(url->replace("ACCESS_TOKEN",token,Qt::CaseSensitive))));
connect(resp,SIGNAL(finished()),this,SLOT(loadPicList()));
}
void FbApi::saveCredentials(QString myUser,QString myPass){
user=new QString(myUser);
pass=new QString(myPass);
KConfigGroup credentials(&config,"Images");
credentials.writeEntry("user",*user);
credentials.writeEntry("pass",*pass);
credentials.config()->sync();
}
void FbApi::loadPicList(){
bool ok;
QVariant result=parser.parse(resp->readAll(), &ok);
resp->close();
//delete resp;
if(ok){
delete picList;
picList = new QList<QString> ();
foreach(QVariant pic,result.toMap()["data"].toList())
(*picList).append(pic.toMap()["src_big"].toString());
qDebug() << (*picList);
KConfigGroup imgConfig(&config,"Images");
imgConfig.writeEntry("images",(*picList));
imgConfig.config()->sync();
emit picsLoaded();
}
//delete picList;
//picList=&result["data"].toList();
}
bool FbApi::nextPicture(){
if((*picList).size()>0){
picResp = net->get(QNetworkRequest(QUrl((*picList).at(qrand()%((*picList).size())))));
connect(picResp,SIGNAL(finished()),this,SLOT(setPicture()));
return true;
}
return false;
}
void FbApi::setPicture(){
QString * path;
if(picResp->error()==QNetworkReply::NoError && picResp->size()>1){
if(img){
path=new QString(QDir::tempPath().append("/framebook.jpg"));
img=false;
}else{
path=new QString(QDir::tempPath().append("/framebook2.jpg"));
img=true;
}
pic=new QFile(*path);
pic->open(QIODevice::WriteOnly);
pic->write(picResp->readAll());
pic->close();
try{
Magick::Image image(qPrintable(*path));
//image.read(qPrintable(*path));
image.scale(Magick::Geometry(imgWidth,imgHeight,0,0));
//image.zoom("320x240");
image.write(qPrintable(path->replace(".jpg","r.jpg")));
//image.write("/home/neto/testjgp.jpg");
emit picLoaded(path);
qDebug() << (*path);
}catch(Magick::Exception err){
qDebug() << "Error loading image, wait for next";
}
}
}