cachewebpage.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "cachewebpage.h"
  2. #include "httpdownload.h"
  3. #include "sncdatabase.h"
  4. CacheWebPage::CacheWebPage(QString pageUrl, QString cachePath, QObject *parent):
  5. QThread(parent)
  6. {
  7. this->pageUrl = pageUrl;
  8. this->cachePath = cachePath;
  9. this->stop = false;
  10. createPageCachePath();
  11. }
  12. CacheWebPage::~CacheWebPage()
  13. {
  14. qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
  15. <<"CacheWebPage Destroyed.";
  16. }
  17. void CacheWebPage::createPageCachePath()
  18. {
  19. QString md5;
  20. QByteArray ba;
  21. ba = QCryptographicHash::hash ( this->pageUrl.toAscii(), QCryptographicHash::Md5 );
  22. md5.append(ba.toHex());
  23. QDir dir(this->cachePath);
  24. if(!dir.exists(md5))
  25. {
  26. dir.mkdir(md5);
  27. }
  28. if(this->cachePath.endsWith('/'))
  29. this->cachePath += md5 + "/";
  30. else
  31. this->cachePath += "/" + md5 + "/";
  32. }
  33. void CacheWebPage::abort()
  34. {
  35. this-> stop = true;
  36. }
  37. void CacheWebPage::downloadPage()
  38. {
  39. HttpDownload download;
  40. QString storePath = createLocalDirByUrl(this->pageUrl);
  41. int retCode = download.downloadFile(this->pageUrl,storePath);
  42. if(retCode<0)
  43. return ;
  44. if(stop)
  45. return ;
  46. QUrl url(this->pageUrl);
  47. QFileInfo fileInfo(url.path());
  48. QFile file(storePath + fileInfo.fileName());
  49. if(!file.open(QIODevice::ReadOnly))
  50. return ;
  51. QTextStream in(&file);
  52. QString souceText = in.readAll();
  53. file.close();
  54. QStringList elements = getMatchElementFromPage(souceText);
  55. QFileInfo fileInfo2(this->pageUrl);
  56. for(int i=0;i<elements.size();i++)
  57. {
  58. if(stop)
  59. break;
  60. QString link = this->pageUrl.replace(fileInfo2.fileName(),"") + elements.at(i);
  61. QString path = createLocalDirByUrl(link);
  62. HttpDownload down;
  63. down.downloadFile(link,path);
  64. }
  65. }
  66. QString CacheWebPage::createLocalDirByUrl(QString pageUrl)
  67. {
  68. QUrl url(pageUrl);
  69. QString path = this->cachePath;
  70. QStringList subDirs = url.path().split("/");
  71. QDir dir(path);
  72. if(subDirs.size()>2)
  73. {
  74. for(int i=1;i<subDirs.size()-1;i++)
  75. {
  76. path += subDirs.at(i) + "/";
  77. if(!dir.exists(path))
  78. {
  79. dir.mkdir(path);
  80. }
  81. }
  82. }
  83. return path;
  84. }
  85. QStringList CacheWebPage::getMatchElementFromPage(QString souceText)
  86. {
  87. QRegExp rx ("[a-zA-Z0-9]+/?[a-zA-Z0-9]+\\.{1}((jpg)|(gif)|(bmp)|(ico)|(png)|(js)|(css))") ;
  88. int pos = 0;
  89. QStringList elements;
  90. while ((pos = rx.indexIn(souceText, pos)) != -1)
  91. {
  92. pos += rx.matchedLength();
  93. elements<<rx.capturedTexts().at(0);
  94. }
  95. return elements;
  96. }
  97. void CacheWebPage::run()
  98. {
  99. downloadPage();
  100. }