Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/

DatabaseThread.cpp 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2016-2017 Syping
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *****************************************************************************/
  18. #include "DatabaseThread.h"
  19. #include "CrewDatabase.h"
  20. #include "AppEnv.h"
  21. #include <QNetworkAccessManager>
  22. #include <QNetworkRequest>
  23. #include <QNetworkReply>
  24. #include <QJsonDocument>
  25. #include <QJsonObject>
  26. #include <QStringList>
  27. #include <QVariantMap>
  28. #include <QEventLoop>
  29. #include <QTimer>
  30. #include <QDebug>
  31. #include <QUrl>
  32. #define crewMaxPages 83
  33. #define maxLoadFails 3
  34. DatabaseThread::DatabaseThread(CrewDatabase *crewDB, QObject *parent) : QThread(parent), crewDB(crewDB)
  35. {
  36. continueLastCrew = true;
  37. threadRunning = true;
  38. }
  39. void DatabaseThread::run()
  40. {
  41. QEventLoop threadLoop;
  42. QObject::connect(this, SIGNAL(threadTerminated()), &threadLoop, SLOT(quit()));
  43. while (threadRunning)
  44. {
  45. if (threadRunning)
  46. {
  47. QTimer::singleShot(300000, &threadLoop, SLOT(quit()));
  48. threadLoop.exec();
  49. }
  50. }
  51. }
  52. void DatabaseThread::scanCrewReference(const QStringList &crewList, const int &requestDelay)
  53. {
  54. for (QString crewID : crewList)
  55. {
  56. if (threadRunning && crewID != QLatin1String("0"))
  57. {
  58. QNetworkAccessManager *netManager = new QNetworkAccessManager();
  59. QNetworkRequest netRequest(AppEnv::getCrewFetchingUrl(crewID));
  60. #if QT_VERSION >= 0x050600
  61. netRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
  62. #endif
  63. netRequest.setRawHeader("User-Agent", AppEnv::getUserAgent());
  64. netRequest.setRawHeader("Accept", "text/html");
  65. netRequest.setRawHeader("Accept-Charset", "utf-8");
  66. netRequest.setRawHeader("Accept-Language", "en-US,en;q=0.9");
  67. netRequest.setRawHeader("Connection", "keep-alive");
  68. QNetworkReply *netReply = netManager->get(netRequest);
  69. QEventLoop *downloadLoop = new QEventLoop();
  70. QObject::connect(netReply, SIGNAL(finished()), downloadLoop, SLOT(quit()));
  71. if (!continueLastCrew) { QObject::connect(this, SIGNAL(threadTerminated()), downloadLoop, SLOT(quit())); }
  72. QTimer::singleShot(30000, downloadLoop, SLOT(quit()));
  73. downloadLoop->exec();
  74. downloadLoop->disconnect();
  75. delete downloadLoop;
  76. if (netReply->isFinished())
  77. {
  78. QString crewName;
  79. QByteArray crewHtml = netReply->readAll();
  80. QStringList crewHtmlSplit1 = QString::fromUtf8(crewHtml).split("<title>Rockstar Games Social Club - Crew : ");
  81. if (crewHtmlSplit1.length() >= 2)
  82. {
  83. QStringList crewHtmlSplit2 = QString(crewHtmlSplit1.at(1)).split("</title>");
  84. if (crewHtmlSplit2.length() >= 1)
  85. {
  86. crewName = crewHtmlSplit2.at(0);
  87. }
  88. }
  89. if (!crewName.isEmpty())
  90. {
  91. emit crewNameFound(crewID.toInt(), crewName);
  92. }
  93. }
  94. else
  95. {
  96. netReply->abort();
  97. }
  98. if (threadRunning)
  99. {
  100. QEventLoop *waitingLoop = new QEventLoop();
  101. QTimer::singleShot(requestDelay, waitingLoop, SLOT(quit()));
  102. if (!continueLastCrew) { QObject::connect(this, SIGNAL(threadTerminated()), waitingLoop, SLOT(quit())); }
  103. waitingLoop->exec();
  104. waitingLoop->disconnect();
  105. delete waitingLoop;
  106. }
  107. delete netReply;
  108. delete netManager;
  109. }
  110. }
  111. }
  112. void DatabaseThread::scanCrewMembersList(const QStringList &crewList, const int &maxPages, const int &requestDelay)
  113. {
  114. for (QString crewID : crewList)
  115. {
  116. if (threadRunning && crewID != QLatin1String("0"))
  117. {
  118. int currentFail = 0;
  119. int currentPage = 0;
  120. int foundPlayers = 0;
  121. int totalPlayers = 1000;
  122. while(foundPlayers < totalPlayers && currentPage < maxPages && (continueLastCrew ? true : threadRunning))
  123. {
  124. QNetworkAccessManager *netManager = new QNetworkAccessManager();
  125. QNetworkRequest netRequest(AppEnv::getPlayerFetchingUrl(crewID, currentPage));
  126. #if QT_VERSION >= 0x050600
  127. netRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
  128. #endif
  129. netRequest.setRawHeader("User-Agent", AppEnv::getUserAgent());
  130. netRequest.setRawHeader("Accept", "application/json");
  131. netRequest.setRawHeader("Accept-Charset", "utf-8");
  132. netRequest.setRawHeader("Accept-Language", "en-US,en;q=0.9");
  133. netRequest.setRawHeader("Connection", "keep-alive");
  134. QNetworkReply *netReply = netManager->get(netRequest);
  135. QEventLoop *downloadLoop = new QEventLoop();
  136. QObject::connect(netReply, SIGNAL(finished()), downloadLoop, SLOT(quit()));
  137. if (!continueLastCrew) { QObject::connect(this, SIGNAL(threadTerminated()), downloadLoop, SLOT(quit())); }
  138. QTimer::singleShot(30000, downloadLoop, SLOT(quit()));
  139. downloadLoop->exec();
  140. downloadLoop->disconnect();
  141. delete downloadLoop;
  142. if (netReply->isFinished())
  143. {
  144. QByteArray crewJson = netReply->readAll();
  145. QJsonDocument crewDocument = QJsonDocument::fromJson(crewJson);
  146. QJsonObject crewObject = crewDocument.object();
  147. QVariantMap crewMap = crewObject.toVariantMap();
  148. if (crewMap.contains("Total")) { totalPlayers = crewMap["Total"].toInt(); }
  149. if (crewMap.contains("Members"))
  150. {
  151. const QList<QVariant> memberList = crewMap["Members"].toList();
  152. for (QVariant memberVariant : memberList)
  153. {
  154. QMap<QString, QVariant> memberMap = memberVariant.toMap();
  155. if (memberMap.contains("RockstarId") && memberMap.contains("Name"))
  156. {
  157. int RockstarId = memberMap["RockstarId"].toInt();
  158. QString memberName = memberMap["Name"].toString();
  159. if (!memberName.isEmpty() && RockstarId != 0)
  160. {
  161. foundPlayers++;
  162. emit playerNameFound(RockstarId, memberName);
  163. }
  164. }
  165. }
  166. }
  167. currentPage++;
  168. }
  169. else
  170. {
  171. currentFail++;
  172. if (currentFail == maxLoadFails)
  173. {
  174. currentFail = 0;
  175. currentPage++;
  176. }
  177. }
  178. delete netReply;
  179. delete netManager;
  180. if (foundPlayers < totalPlayers && currentPage < maxPages && (continueLastCrew ? true : threadRunning))
  181. {
  182. QEventLoop *waitingLoop = new QEventLoop();
  183. QTimer::singleShot(requestDelay, waitingLoop, SLOT(quit()));
  184. if (!continueLastCrew) { QObject::connect(this, SIGNAL(threadTerminated()), waitingLoop, SLOT(quit())); }
  185. waitingLoop->exec();
  186. waitingLoop->disconnect();
  187. delete waitingLoop;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. void DatabaseThread::deleteCompatibleCrews(QStringList *crewList)
  194. {
  195. for (QString crewNID : *crewList)
  196. {
  197. if (crewDB->isCompatibleCrew(crewNID))
  198. {
  199. crewList->removeAll(crewNID);
  200. }
  201. }
  202. }
  203. QStringList DatabaseThread::deleteCompatibleCrews(const QStringList &crewList)
  204. {
  205. QStringList crewListR = crewList;
  206. for (QString crewNID : crewListR)
  207. {
  208. if (crewDB->isCompatibleCrew(crewNID))
  209. {
  210. crewListR.removeAll(crewNID);
  211. }
  212. }
  213. return crewListR;
  214. }
  215. void DatabaseThread::terminateThread()
  216. {
  217. threadRunning = false;
  218. emit threadTerminated();
  219. }