gta5view/DatabaseThread.cpp

297 lines
12 KiB
C++
Raw Normal View History

2016-03-22 05:20:42 +01:00
/*****************************************************************************
* gta5sync GRAND THEFT AUTO V SYNC
2017-02-11 04:21:55 +01:00
* Copyright (C) 2016-2017 Syping
2016-03-22 05:20:42 +01:00
*
* 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.
*
* 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 "DatabaseThread.h"
#include "CrewDatabase.h"
#include "AppEnv.h"
2016-03-22 05:20:42 +01:00
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStringList>
#include <QVariantMap>
#include <QEventLoop>
#include <QTimer>
#include <QDebug>
#include <QUrl>
DatabaseThread::DatabaseThread(CrewDatabase *crewDB, QObject *parent) : QThread(parent), crewDB(crewDB)
{
2016-03-23 07:45:30 +01:00
crewMaxPages = 83;
2016-04-13 16:03:08 +02:00
threadRunning = true;
2016-03-22 05:20:42 +01:00
}
void DatabaseThread::run()
{
QEventLoop threadLoop;
2016-03-27 11:21:53 +02:00
QStringList crewList;
2016-03-23 07:45:30 +01:00
2017-02-17 12:07:37 +01:00
// Register thread loop end signal
QObject::connect(this, SIGNAL(threadEndCommited()), &threadLoop, SLOT(quit()));
2016-03-23 07:45:30 +01:00
// Quick time scan
if (crewList.length() <= 3)
{
scanCrewReference(crewList, 2500);
scanCrewMembersList(crewList, 3, 2500);
2016-03-23 07:45:30 +01:00
emit playerNameUpdated();
}
else if (crewList.length() <= 5)
2016-03-22 05:20:42 +01:00
{
scanCrewReference(crewList, 2500);
scanCrewMembersList(crewList, 2, 2500);
2016-03-23 07:45:30 +01:00
emit playerNameUpdated();
}
2016-04-13 16:03:08 +02:00
QEventLoop *waitingLoop = new QEventLoop();
QTimer::singleShot(10000, waitingLoop, SLOT(quit()));
2017-02-17 12:07:37 +01:00
QObject::connect(this, SIGNAL(threadEndCommited()), waitingLoop, SLOT(quit()));
2016-04-13 16:03:08 +02:00
waitingLoop->exec();
delete waitingLoop;
2016-03-23 07:45:30 +01:00
2016-04-13 16:03:08 +02:00
while (threadRunning)
{
crewList = crewDB->getCrews();
2016-03-27 11:21:53 +02:00
2016-04-13 16:03:08 +02:00
// Long time scan
scanCrewReference(crewList, 10000);
2016-04-13 16:03:08 +02:00
scanCrewMembersList(crewList, crewMaxPages, 10000);
emit playerNameUpdated();
2016-03-22 05:20:42 +01:00
2017-02-17 12:07:37 +01:00
if (threadRunning)
{
QTimer::singleShot(300000, &threadLoop, SLOT(quit()));
threadLoop.exec();
}
2016-04-13 16:03:08 +02:00
}
2016-03-23 07:45:30 +01:00
}
2016-03-22 05:20:42 +01:00
// void DatabaseThread::scanCrewReference(QStringList crewList, int requestDelay)
// {
// foreach (const QString &crewID, crewList)
// {
// if (threadRunning && crewID != "0")
// {
// QNetworkAccessManager *netManager = new QNetworkAccessManager();
// QNetworkRequest netRequest(AppEnv::getCrewFetchingUrl(crewID));
// netRequest.setRawHeader("User-Agent", AppEnv::getUserAgent());
// netRequest.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// netRequest.setRawHeader("Accept-Language", "en-US;q=0.5,en;q=0.3");
// netRequest.setRawHeader("Connection", "keep-alive");
// QNetworkReply *netReply = netManager->get(netRequest);
// QEventLoop *downloadLoop = new QEventLoop();
// QObject::connect(netReply, SIGNAL(finished()), downloadLoop, SLOT(quit()));
// QObject::connect(this, SIGNAL(threadEndCommited()), downloadLoop, SLOT(quit()));
// QTimer::singleShot(30000, downloadLoop, SLOT(quit()));
// downloadLoop->exec();
// delete downloadLoop;
// if (netReply->isFinished())
// {
// QByteArray crewJson = netReply->readAll();
// QJsonDocument crewDocument = QJsonDocument::fromJson(crewJson);
// QJsonObject crewObject = crewDocument.object();
// QVariantMap crewMap = crewObject.toVariantMap();
// QString crewName;
// bool isFound = false;
// if (crewMap.contains("activities"))
// {
// QList<QVariant> activitiesList = crewMap["activities"].toList();
// foreach (const QVariant &activitiesVariant, activitiesList)
// {
// QMap<QString, QVariant> activityRootMap = activitiesVariant.toMap();
// foreach(const QVariant &activityRootVariant, activityRootMap)
// {
// QMap<QString, QVariant> activityMap = activityRootVariant.toMap();
// foreach(const QVariant &activityVariant, activityMap)
// {
// QMap<QString, QVariant> activityFinalMap = activityVariant.toMap();
// if (activityFinalMap.contains("id") && activityFinalMap["id"] == crewID)
// {
// if (activityFinalMap.contains("name") && isFound == false)
// {
// isFound = true;
// crewName = activityFinalMap["name"].toString();
// }
// }
// }
// }
// }
// }
// if (!crewName.isNull())
// {
// crewDB->setCrewName(crewID.toInt(), crewName);
// }
// }
// QEventLoop *waitingLoop = new QEventLoop();
// QTimer::singleShot(requestDelay, waitingLoop, SLOT(quit()));
// QObject::connect(this, SIGNAL(threadEndCommited()), waitingLoop, SLOT(quit()));
// waitingLoop->exec();
// delete waitingLoop;
// delete netReply;
// delete netManager;
// }
// }
// }
void DatabaseThread::scanCrewReference(QStringList crewList, int requestDelay)
{
foreach (const QString &crewID, crewList)
{
2017-02-17 12:07:37 +01:00
if (threadRunning && crewID != "0")
{
QNetworkAccessManager *netManager = new QNetworkAccessManager();
QNetworkRequest netRequest(AppEnv::getCrewFetchingUrl(crewID));
#if QT_VERSION >= 0x050600
netRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
#endif
netRequest.setRawHeader("User-Agent", AppEnv::getUserAgent());
netRequest.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
netRequest.setRawHeader("Accept-Language", "en-US;q=0.5,en;q=0.3");
netRequest.setRawHeader("Connection", "keep-alive");
QNetworkReply *netReply = netManager->get(netRequest);
QEventLoop *downloadLoop = new QEventLoop();
QObject::connect(netReply, SIGNAL(finished()), downloadLoop, SLOT(quit()));
2017-02-17 12:07:37 +01:00
QObject::connect(this, SIGNAL(threadEndCommited()), downloadLoop, SLOT(quit()));
QTimer::singleShot(30000, downloadLoop, SLOT(quit()));
downloadLoop->exec();
delete downloadLoop;
if (netReply->isFinished())
{
QString crewName;
QByteArray crewHtml = netReply->readAll();
QStringList crewHtmlSplit1 = QString::fromUtf8(crewHtml).split("<title>Rockstar Games Social Club - Crew : ");
if (crewHtmlSplit1.length() >= 2)
{
QStringList crewHtmlSplit2 = QString(crewHtmlSplit1.at(1)).split("</title>");
if (crewHtmlSplit2.length() >= 1)
{
crewName = crewHtmlSplit2.at(0);
}
}
if (!crewName.isEmpty())
{
crewDB->setCrewName(crewID.toInt(), crewName);
}
}
QEventLoop *waitingLoop = new QEventLoop();
QTimer::singleShot(requestDelay, waitingLoop, SLOT(quit()));
2017-02-17 12:07:37 +01:00
QObject::connect(this, SIGNAL(threadEndCommited()), waitingLoop, SLOT(quit()));
waitingLoop->exec();
delete waitingLoop;
delete netReply;
delete netManager;
}
}
}
2016-03-23 07:45:30 +01:00
void DatabaseThread::scanCrewMembersList(QStringList crewList, int maxPages, int requestDelay)
{
foreach (const QString &crewID, crewList)
{
2017-02-17 12:07:37 +01:00
if (threadRunning && crewID != "0")
2016-03-22 05:20:42 +01:00
{
int currentPage = 0;
int foundPlayers = 0;
int totalPlayers = 1000;
2016-03-23 07:45:30 +01:00
while(foundPlayers < totalPlayers && currentPage < maxPages)
{
QNetworkAccessManager *netManager = new QNetworkAccessManager();
2016-03-23 07:45:30 +01:00
QNetworkRequest netRequest(AppEnv::getPlayerFetchingUrl(crewID, QString::number(currentPage)));
#if QT_VERSION >= 0x050600
netRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
#endif
netRequest.setRawHeader("User-Agent", AppEnv::getUserAgent());
netRequest.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
netRequest.setRawHeader("Accept-Language", "en-US;q=0.5,en;q=0.3");
netRequest.setRawHeader("Connection", "keep-alive");
2016-03-23 07:45:30 +01:00
QNetworkReply *netReply = netManager->get(netRequest);
2016-03-23 07:45:30 +01:00
QEventLoop *downloadLoop = new QEventLoop();
QObject::connect(netReply, SIGNAL(finished()), downloadLoop, SLOT(quit()));
2017-02-17 12:07:37 +01:00
QObject::connect(this, SIGNAL(threadEndCommited()), downloadLoop, SLOT(quit()));
QTimer::singleShot(30000, downloadLoop, SLOT(quit()));
downloadLoop->exec();
delete downloadLoop;
2016-03-23 07:45:30 +01:00
if (netReply->isFinished())
2016-03-22 05:20:42 +01:00
{
QByteArray crewJson = netReply->readAll();
QJsonDocument crewDocument = QJsonDocument::fromJson(crewJson);
QJsonObject crewObject = crewDocument.object();
QVariantMap crewMap = crewObject.toVariantMap();
if (crewMap.contains("Total")) { totalPlayers = crewMap["Total"].toInt(); }
if (crewMap.contains("Members"))
2016-03-22 05:20:42 +01:00
{
QList<QVariant> memberList = crewMap["Members"].toList();
foreach (const QVariant &memberVariant, memberList)
2016-03-22 05:20:42 +01:00
{
QMap<QString, QVariant> memberMap = memberVariant.toMap();
foundPlayers++;
if (memberMap.contains("RockstarId") && memberMap.contains("Name"))
2016-03-23 07:45:30 +01:00
{
int RockstarId = memberMap["RockstarId"].toInt();
QString memberName = memberMap["Name"].toString();
if (memberName != "" && RockstarId != 0)
{
emit playerNameFound(RockstarId, memberName);
}
2016-03-23 07:45:30 +01:00
}
2016-03-22 05:20:42 +01:00
}
}
2016-03-23 07:45:30 +01:00
QEventLoop *waitingLoop = new QEventLoop();
QTimer::singleShot(requestDelay, waitingLoop, SLOT(quit()));
2017-02-17 12:07:37 +01:00
QObject::connect(this, SIGNAL(threadEndCommited()), waitingLoop, SLOT(quit()));
waitingLoop->exec();
delete waitingLoop;
2016-03-23 07:45:30 +01:00
currentPage++;
}
delete netReply;
delete netManager;
}
2016-03-22 05:20:42 +01:00
}
}
}
2017-02-17 12:07:37 +01:00
void DatabaseThread::doEndThread()
{
threadRunning = false;
emit threadEndCommited();
}