v.0.3.4
This commit is contained in:
parent
12ac44fb70
commit
aca94a5393
98 changed files with 1061 additions and 152 deletions
341
source-linux/common/updatenews.cpp
Normal file
341
source-linux/common/updatenews.cpp
Normal file
|
@ -0,0 +1,341 @@
|
|||
// This file is part of Friendiqa
|
||||
// https://git.friendi.ca/lubuwest/Friendiqa
|
||||
// Copyright (C) 2017 Marco R. <thomasschmidt45@gmx.net>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "updatenews.h"
|
||||
|
||||
#include <QHttpPart>
|
||||
#include <QTextCodec>
|
||||
#include <QUrlQuery>
|
||||
#include <QList>
|
||||
#include <QDataStream>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QQmlEngine>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlRecord>
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlError>
|
||||
#include <QDateTime>
|
||||
#include "xhr.h"
|
||||
|
||||
|
||||
UPDATENEWS *UPDATENEWS::instance()
|
||||
{
|
||||
static UPDATENEWS udn;
|
||||
return &udn;
|
||||
}
|
||||
|
||||
UPDATENEWS::UPDATENEWS(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void UPDATENEWS::setUrl(QString url)
|
||||
{
|
||||
if (url!=m_url) {
|
||||
m_url = url;
|
||||
emit urlChanged(m_url);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void UPDATENEWS::setDatabase()
|
||||
{
|
||||
static QQmlEngine qe;
|
||||
QString db_url=qe.offlineStorageDatabaseFilePath("Friendiqa");
|
||||
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
m_db.setDatabaseName(QUrl("file://"+db_url+".sqlite").toLocalFile());
|
||||
qDebug() << db_url;
|
||||
|
||||
if (!m_db.open())
|
||||
{
|
||||
qDebug() << "Error: connection with database fail " << m_db.lastError();
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "Database: connection ok";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void UPDATENEWS::login()
|
||||
{
|
||||
qDebug() << "login";
|
||||
QSqlQuery query("SELECT * FROM config WHERE isActive=0",m_db);
|
||||
qDebug() <<"size "<< query.size();
|
||||
while (query.next())
|
||||
{
|
||||
username = query.value(1).toString();
|
||||
QByteArray bpassword=query.value(2).toByteArray();
|
||||
QString password=QByteArray::fromBase64(bpassword);
|
||||
m_login=username+":"+password ;
|
||||
//emit this->loginChanged(m_login);
|
||||
m_url=query.value(0).toString();
|
||||
QString isActive=query.value(7).toString();
|
||||
|
||||
qDebug() << " username " << username<< password << m_url << isActive;
|
||||
//UPDATENEWS::connect(&UPDATENEWS::getLogin,SIGNAL(loginChanged(QString)),this,SLOT(XHR::login(QString)));
|
||||
m_api="/api/statuses/friends_timeline";
|
||||
}
|
||||
//return m_login;
|
||||
}
|
||||
|
||||
|
||||
void UPDATENEWS::timeline()
|
||||
{
|
||||
QSqlQuery query("SELECT status_id FROM news WHERE username='"+ username +"' ORDER BY status_id DESC LIMIT 1",m_db);
|
||||
if (query.isActive() && query.isSelect()){query.first();};
|
||||
QString lastid=query.value(0).toString();
|
||||
//query.finish();
|
||||
qDebug() << lastid << query.at();
|
||||
clearParams();
|
||||
setParam("since_id",lastid);
|
||||
setParam("count","50");
|
||||
get();
|
||||
QObject::connect(this,&UPDATENEWS::success,this,&UPDATENEWS::store);
|
||||
}
|
||||
|
||||
void UPDATENEWS::store(QByteArray serverreply,QString apiname)
|
||||
{
|
||||
//qDebug() << serverreply;
|
||||
QJsonDocument news;
|
||||
QJsonParseError jsonerror;
|
||||
news=QJsonDocument::fromJson(serverreply,&jsonerror);
|
||||
qDebug() << news.isArray()<<news.isObject()<<news.isEmpty()<<jsonerror.errorString();
|
||||
if (news.isArray()){
|
||||
qDebug() << " isArray ";
|
||||
//QJsonArray array = news.//news.toArray();
|
||||
for (int i=0; i < news.array().count();i++){
|
||||
QJsonValue newsitem=news[i];
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare("INSERT INTO news (username,messagetype,text,created_at,in_reply_to_status_id,source,status_id,in_reply_to_user_id,geo,favorited,uid,statusnet_html,statusnet_conversation_id,friendica_activities,friendica_activities_self,attachments,friendica_owner) " "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
|
||||
query.bindValue(0,username);
|
||||
query.bindValue(1,"0");
|
||||
query.bindValue(2, newsitem["text"].toString().toUtf8().toBase64());
|
||||
QString sourcedate=newsitem["created_at"].toString();
|
||||
QString formateddate=sourcedate.mid(0,3)+", "+sourcedate.mid(8,3)+sourcedate.mid(4,3)+sourcedate.mid(25,5)+sourcedate.mid(10,15);
|
||||
query.bindValue(3,QDateTime::fromString(formateddate,Qt::RFC2822Date).toMSecsSinceEpoch() );
|
||||
if(newsitem["in_reply_to_status_id"]!=QJsonValue::Null){query.bindValue(4, newsitem["in_reply_to_status_id"].toInt());};
|
||||
query.bindValue(5,newsitem["source"]);
|
||||
query.bindValue(6,newsitem["id"].toInt());
|
||||
if(newsitem["in_reply_to_user_id"]!=QJsonValue::Null){ query.bindValue(7,newsitem["in_reply_to_user_id"].toInt());};
|
||||
query.bindValue(8,newsitem["geo"]);
|
||||
query.bindValue( 9, newsitem["favorited"].toInt());
|
||||
query.bindValue(10, newsitem["user"]["id"].toInt());
|
||||
query.bindValue(11, newsitem["statusnet_html"].toString().toUtf8().toBase64());
|
||||
query.bindValue(12, newsitem["statusnet_conversation_id"].toInt());
|
||||
QJsonArray likeArray;QJsonArray dislikeArray;QJsonArray attendyesArray;QJsonArray attendnoArray;QJsonArray attendmaybeArray;
|
||||
if (newsitem.toObject().contains("friendica_activities")){
|
||||
for (int a=0; a < newsitem["friendica_activities"]["like"].toArray().count();a++){
|
||||
likeArray.append(newsitem["friendica_activities"]["like"][a]["url"].toString());
|
||||
}
|
||||
for (int b=0; b < newsitem["friendica_activities"]["dislike"].toArray().count();b++){
|
||||
dislikeArray.append(newsitem["friendica_activities"]["dislike"][b]["url"].toString());
|
||||
}
|
||||
for (int c=0; c < newsitem["friendica_activities"]["attendyes"].toArray().count();c++){
|
||||
attendyesArray.append(newsitem["friendica_activities"]["attendyes"][c]["url"].toString());
|
||||
}
|
||||
for (int d=0; d < newsitem["friendica_activities"]["attendno"].toArray().count();d++){
|
||||
attendnoArray.append(newsitem["friendica_activities"]["attendno"][d]["url"].toString());
|
||||
}
|
||||
for (int e = 0; e < newsitem["friendica_activities"]["attendmaybe"].toArray().count();e++){
|
||||
attendmaybeArray.append(newsitem["friendica_activities"]["attendmaybe"][e]["url"].toString());
|
||||
}
|
||||
};
|
||||
QJsonArray friendica_activities; friendica_activities={likeArray,dislikeArray,attendyesArray,attendnoArray,attendmaybeArray};
|
||||
QJsonDocument activities; activities.setArray(friendica_activities);
|
||||
query.bindValue(13,activities.toJson(QJsonDocument::Compact).toBase64());
|
||||
query.bindValue(14,"[]");
|
||||
|
||||
if (newsitem["attachments"]!=QJsonValue::Undefined){
|
||||
query.bindValue(15, QJsonDocument(newsitem["attachments"].toArray()).toJson(QJsonDocument::Compact).toBase64());
|
||||
};
|
||||
|
||||
query.bindValue(16, newsitem["friendica_owner"]["url"]);
|
||||
qDebug() << query.exec() <<query.lastError();
|
||||
//query.finish();
|
||||
}
|
||||
}
|
||||
QList<QJsonValue> newcontacts=findNewContacts(news);
|
||||
updateContacts(newcontacts);
|
||||
|
||||
emit updatesuccess();
|
||||
}
|
||||
|
||||
|
||||
QList <QJsonValue> UPDATENEWS::findNewContacts(QJsonDocument news){
|
||||
QSqlQuery query("SELECT profile_image_url FROM contacts",m_db);
|
||||
QList<QString> imageurls;
|
||||
while (query.next()){
|
||||
imageurls.append(query.value(0).toString());
|
||||
}
|
||||
QList<QJsonValue> newcontacts;
|
||||
QList<QString> newcontactimagelinks;
|
||||
qDebug()<<news.array().count();
|
||||
for (int i=0; i<news.array().count();i++){
|
||||
if(imageurls.contains(news[i]["user"]["profile_image_url"].toString()) || newcontactimagelinks.contains(news[i]["user"]["profile_image_url"].toString())){
|
||||
qDebug()<<"vorhanden";
|
||||
}
|
||||
else{ qDebug()<<"newcontact" <<news[i]["user"];
|
||||
newcontacts.append(news[i]["user"]);
|
||||
newcontactimagelinks.append(news[i]["user"]["profile_image_url"].toString());
|
||||
}
|
||||
}
|
||||
return newcontacts;
|
||||
}
|
||||
|
||||
|
||||
void UPDATENEWS::updateContacts(QList<QJsonValue> contacts){
|
||||
qint64 currentTime =QDateTime::currentMSecsSinceEpoch();
|
||||
for (int i=0; i < contacts.count();i++){
|
||||
QJsonValue contact=contacts[i];
|
||||
QSqlQuery query(m_db);
|
||||
|
||||
query.prepare("INSERT INTO contacts username, id, name, screen_name, location,imageAge, "
|
||||
"profile_image_url, description, profile_image, url, protected, followers_count, "
|
||||
"friends_count, created_at, favourites_count, utc_offset, time_zone, statuses_count,"
|
||||
" following, verified, statusnet_blocking, notifications, statusnet_profile_url,"
|
||||
" cid, network, isFriend, timestamp"
|
||||
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
|
||||
query.bindValue(0,username);
|
||||
query.bindValue(1, contact["id"]);
|
||||
query.bindValue(2, contact["name"].toString().toUtf8().toBase64());
|
||||
query.bindValue(3, contact["screen_name"]);
|
||||
query.bindValue(4, contact["location"]);
|
||||
query.bindValue(5, currentTime);
|
||||
query.bindValue(6, contact["profile_image_url"]);
|
||||
query.bindValue(7, contact["description"].toString().toUtf8().toBase64());
|
||||
//query.bindValue(8,"")
|
||||
query.bindValue(9, contact["url"]);
|
||||
query.bindValue(10,contact["protected"]);
|
||||
query.bindValue(11,contact["followers_count"]);
|
||||
query.bindValue(12,contact["friends_count"]);
|
||||
query.bindValue(13,contact["created_at"]);
|
||||
query.bindValue(14,contact["favorites_count"]);
|
||||
query.bindValue(15,contact["utc_offset"]);
|
||||
query.bindValue(16,contact["time_zone"]);
|
||||
query.bindValue(17,contact["statuses_count"]);
|
||||
query.bindValue(18,contact["following"]);
|
||||
query.bindValue(19,contact["verfied"]);
|
||||
query.bindValue(20,contact["statusnet_blocking"]);
|
||||
query.bindValue(21,contact["notifications"]);
|
||||
query.bindValue(22,contact["statusnet_profile_url"]);
|
||||
query.bindValue(23,contact["cid"]);
|
||||
query.bindValue(24,contact["network"]);
|
||||
query.bindValue(25, 1);
|
||||
//query.bindValue(26,image_timestamp);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
QString UPDATENEWS::url() const
|
||||
{
|
||||
return m_url;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void UPDATENEWS::setParam(QString name, QString value)
|
||||
{
|
||||
params.insert(name, value);
|
||||
}
|
||||
|
||||
void UPDATENEWS::clearParams()
|
||||
{
|
||||
files.clear();
|
||||
params.clear();
|
||||
}
|
||||
|
||||
|
||||
void UPDATENEWS::get()
|
||||
{
|
||||
QUrlQuery query;
|
||||
|
||||
QHashIterator<QString, QString> i(params);
|
||||
while(i.hasNext()) {
|
||||
i.next();
|
||||
query.addQueryItem(i.key(), i.value());
|
||||
}
|
||||
|
||||
QUrl requrl(m_url+m_api);
|
||||
qDebug() << requrl;
|
||||
requrl.setQuery(query);
|
||||
QByteArray loginData = m_login.toLocal8Bit().toBase64();
|
||||
QString headerData = "Basic " + loginData;
|
||||
request.setRawHeader("Authorization", headerData.toLocal8Bit());
|
||||
request.setUrl(requrl);
|
||||
reply = manager.get(request);
|
||||
|
||||
connect(reply, &QNetworkReply::finished, this, &UPDATENEWS::onReplySuccess);
|
||||
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onReplyError(QNetworkReply::NetworkError)));
|
||||
connect(reply, &QNetworkReply::readyRead, this, &UPDATENEWS::onReadyRead);
|
||||
connect(reply, &QNetworkReply::sslErrors, this, &UPDATENEWS::onSSLError);
|
||||
}
|
||||
|
||||
void UPDATENEWS::onReplyError(QNetworkReply::NetworkError code)
|
||||
{
|
||||
qDebug() << code;
|
||||
emit this->error( bufferToString(), m_url,m_api, (int) code);
|
||||
buffer.clear();
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
void UPDATENEWS::onReplySuccess()
|
||||
{
|
||||
qDebug() << "!";
|
||||
emit this->success( buffer, m_api);
|
||||
buffer.clear();
|
||||
// reply->deleteLater();
|
||||
}
|
||||
|
||||
void UPDATENEWS::onReadyRead()
|
||||
{
|
||||
qDebug() << ".";
|
||||
buffer += reply->readAll();
|
||||
}
|
||||
|
||||
void UPDATENEWS::onSSLError(const QList<QSslError> &errors)
|
||||
{
|
||||
qDebug() << "XHR::onSSLError :" ;
|
||||
QListIterator<QSslError> ierrs(errors);
|
||||
while(ierrs.hasNext()) {
|
||||
qDebug() << "\t" << ierrs.next().errorString();
|
||||
}
|
||||
}
|
||||
|
||||
QString UPDATENEWS::bufferToString()
|
||||
{
|
||||
return QTextCodec::codecForName("utf-8")->toUnicode(buffer);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue