Автор Тема: Qt 4.7.4 C++ Бот за сваляне на линкове  (Прочетена 3854 пъти)

0 Потребители и 1 Гост преглежда(т) тази тема.

haccac

  • Jr. Member
  • **
  • Благодарности
  • -Казани: 0
  • -Получени: 0
  • Публикации: 80
Qt 4.7.4 C++ Бот за сваляне на линкове
« -: 04 Септември 2011, 23:36:45 »
Кода, който помествам е с учебна цел и не поемам отговорност за какво ще бъде използуван, моля не злоупотребявайте!
Забележка: 1 Кода може да се дообработи оставям го на вас целта ми е да покажа как може да стане не да доизкусурявам.
2 Надявам се знаете защо използвам QHttp а не решението което предлага библиотеката webkit т.е QWebElementCollection  :) .

Код: C++ (QT)
  1. #------------------------LinkDownloader.pro-----------------------
  2.  
  3. #-------------------------------------------------
  4. #
  5. # Project created by QtCreator 2011-09-02T21:41:48
  6. #
  7. #-------------------------------------------------
  8.  
  9. QT       += core
  10. QT       += network
  11.  
  12.  
  13.  
  14. QT       -= gui
  15.  
  16. TARGET = LinkDownloader
  17. CONFIG   += console
  18. CONFIG   -= app_bundle
  19.  
  20. TEMPLATE = app
  21.  
  22.  
  23. SOURCES += main.cpp \
  24.     getsouce.cpp
  25.  
  26. HEADERS += \
  27.     getsouce.h
  28.  
  29.  
  30.  
  31. #----------------------------------getsouce.h------------------------
  32.  
  33.  
  34.  
  35. #ifndef GETSOUCE_H
  36. #define GETSOUCE_H
  37.  
  38. #include <QObject>
  39. #include <QHttp>
  40. #include <QFile>
  41. #include <QDir>
  42. #include <QTextStream>
  43. #include <QStringList>
  44. #include <QByteArray>
  45. #include <QString>
  46. #include <QDebug>
  47. #include <QRegExp>
  48. #include <QUrl>
  49. #include <QTimer>
  50.  
  51.  
  52. class GetSouce : public QObject
  53. {
  54.     Q_OBJECT
  55. public:
  56.     explicit GetSouce(QObject *parent = 0);
  57.     void GetCode(QString domain ,QString path);
  58.  
  59.     QByteArray b_html;
  60.     QString s_html;
  61.  
  62.     QStringList html_list;
  63.     QStringList link_list;
  64.     QStringList dom_list;
  65.     QStringList path_list;
  66.  
  67.     QStringList get_dom_ls;
  68.     QStringList get_path_ls;
  69.  
  70.     QTimer *timer;
  71.     QUrl dom;
  72.     int br;
  73.  
  74.     void GetDomain();
  75.  
  76. signals:
  77.  
  78. public slots:
  79.     void requestFinished(int id, bool error);
  80.     void Process();
  81. private:
  82.     QHttp *http;
  83.  
  84. };
  85.  
  86. #endif // GETSOUCE_H
  87.  
  88.  
  89.  
  90. #-------------------------------------getsouce.cpp--------------------------------
  91.  
  92.  
  93.  
  94. #include "getsouce.h"
  95.  
  96. GetSouce::GetSouce(QObject *parent) :
  97.     QObject(parent)
  98. {
  99.     br = 0;
  100.     timer = new QTimer(this); // Рестартира Process() през определения интервал timer->start(9000);.
  101.     connect(timer,SIGNAL(timeout()),this,SLOT(Process()));
  102.     timer->start(9000);
  103. }
  104.  
  105. void GetSouce::GetCode(QString domain ,QString path)
  106. {
  107.  
  108.     http = new QHttp(this);
  109.     connect(http,SIGNAL(requestFinished(int,bool)),
  110.             this,SLOT(requestFinished(int,bool)));
  111.     if(!domain.isEmpty())
  112.     {
  113.         http->setHost(domain);
  114.         http->get(path);
  115.     }
  116. }
  117.  
  118. void GetSouce::requestFinished(int id, bool error)
  119. {
  120.     if(error)
  121.     {
  122.         qDebug() << "Error!";
  123.     }
  124.     else
  125.     {
  126.         b_html = http->readAll(); // записва кода в QByteArray
  127.         s_html = b_html; // Преобразува в QString
  128.         html_list = s_html.split("<a href=\""); // Разделя стринга и записва в масив
  129.         for(int i = 0; i < html_list.size(); i++)
  130.         {
  131.             if(html_list.at(i).contains(QRegExp("^(http://)?[a-zA-Z0-9_-]+[_-./]*[a-zA-Z0-9_-/?]*"))) // Проверява дали е линк
  132.             {
  133.                 link_list.append(html_list.at(i).section("\"",0,0)); // унищожава всичко след "\" и прехвърля в друг масив
  134.             }
  135.         }
  136.         for(int n = 0; n < link_list.size(); n++)
  137.         {
  138.             dom.setUrl(link_list.at(n)); // Преобразува QString в QUrl
  139.             if(!dom.host().isEmpty())
  140.             {
  141.             dom_list.append(dom.host()); // Извличча хост
  142.             path_list.append(dom.path()); // Извлича път до страницата
  143.             }
  144.  
  145.         }
  146.  
  147.         QString filename1 = QDir::currentPath().append("/domain.txt");
  148.         if( !filename1.isNull() )
  149.         {
  150.             QFile mFile(filename1);
  151.             if(!mFile.open(QFile::Append | QFile::Text))
  152.             {
  153.                 qDebug() << "(W) Error in opening file.";
  154.                 return;
  155.             }
  156.             QTextStream out(&mFile);
  157.             QString op_text = dom_list.join("\n");
  158.             out << op_text;
  159.             mFile.flush();
  160.             mFile.close();
  161.         }
  162.         QString filename2 = QDir::currentPath().append("/path.txt");
  163.         if( !filename2.isNull() )
  164.         {
  165.             QFile mFile(filename2);
  166.             if(!mFile.open(QFile::Append | QFile::Text))
  167.             {
  168.                 qDebug() << "(W) Error in opening file.";
  169.                 return;
  170.             }
  171.             QTextStream out(&mFile);
  172.             QString op_text = path_list.join("\n");
  173.             out << op_text;
  174.             mFile.flush();
  175.             mFile.close();          
  176.         }
  177.     }
  178. }
  179.  
  180. void GetSouce::GetDomain()
  181. {
  182.     get_dom_ls.clear(); // Изчистваме get_dom_ls и get_path_ls преди всяко записване.
  183.     get_path_ls.clear();
  184.     QString filename1 = QDir::currentPath().append("/domain.txt");
  185.     if( !filename1.isNull() )
  186.     {
  187.         QFile mFile(filename1);
  188.         if(!mFile.open(QFile::ReadOnly | QFile::Text))
  189.         {
  190.             qDebug() << "(R) Error in opening file.";
  191.             return;
  192.         }
  193.         QTextStream in(&mFile);
  194.         get_dom_ls = in.readAll().split("\n");
  195.  
  196.     }
  197.  
  198.     QString filename2 = QDir::currentPath().append("/path.txt");
  199.     if( !filename2.isNull() )
  200.     {
  201.         QFile mFile(filename2);
  202.         if(!mFile.open(QFile::ReadOnly | QFile::Text))
  203.         {
  204.             qDebug() << "(R) Error in opening file.";
  205.             return;
  206.         }
  207.         QTextStream in(&mFile);
  208.         get_path_ls = in.readAll().split("\n");
  209.     }
  210. }
  211.  
  212. void GetSouce::Process()
  213. {
  214.  
  215.          GetDomain();
  216.          b_html.clear();
  217.          s_html.clear();
  218.          html_list.clear();
  219.          link_list.clear();
  220.          dom_list.clear();
  221.          path_list.clear();
  222.          GetCode(get_dom_ls.at(br) ,get_path_ls.at(br));
  223.          br++; // Помни позицията, от която четем.
  224.          GetDomain();
  225.          qDebug() << "new Searching";
  226.  
  227. }
  228.  
  229.  
  230.  
  231. #-------------------------------main.cpp------------------------
  232.  
  233.  
  234.  
  235. #include <QtCore/QCoreApplication>
  236. #include "getsouce.h"
  237. int main(int argc, char *argv[])
  238. {
  239.     QCoreApplication a(argc, argv);
  240.     GetSouce gs ;
  241.     qDebug() << "Izchakajte";
  242.     gs.GetCode("katalozi.free.bg","/");
  243.  
  244.  
  245.  
  246.     return a.exec();
  247. }
  248.  
  249.  

не забравяйте да сложите path.txt и domain.txt
« Последна редакция: 12 Май 2015, 22:02:10 от Avalanche »