askanton

Бот для парcинга телеграм + UI

Jun 9th, 2020
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 20.45 KB | None | 0 0
  1. #Tutorial https://www.youtube.com/watch?v=uvbM2mejVk8
  2.  
  3. # -*- coding: utf-8 -*-
  4. from PySide2.QtCore import (QCoreApplication, QMetaObject, QRect)
  5. # from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
  6. #                            QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
  7. # rom PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
  8. #                          QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
  9. #                         QPixmap, QRadialGradient)
  10. from PySide2.QtWidgets import *
  11. from asyncqt import QEventLoop
  12.  
  13. from importlib import reload
  14. import telebot
  15. import time
  16. import logging
  17. from bs4 import BeautifulSoup
  18. import requests
  19. from telebot import apihelper
  20. from threading import Timer
  21.  
  22. logging.basicConfig(filename="logfile.py", level=logging.INFO)
  23.  
  24. list = ['https://t.me/SecLabNews/', 7728, 'https://t.me/overlamer1/', 621, 'https://t.me/thehackernews/', 717,
  25.         'https://t.me/haccking/', 5006, 'https://t.me/opennet_ru/', 4377, 'https://t.me/dataleak/', 1693,
  26.         'https://t.me/Social_engineering/', 1028, 'https://t.me/pystyle/', 110,
  27.         'https://t.me/alexmakus/', 3489, 'https://t.me/xakep_ru/', 9200, 'https://t.me/webware/', 3062,
  28.         'https://t.me/habr_com/', 44013, 'https://t.me/bookflow/', 1052,
  29.         'https://t.me/thehaking/', 365, 'https://t.me/proglibrary/', 3925, 'https://t.me/python_lounge/', 284,
  30.         'https://t.me/thecodemedia/', 2002, 'https://t.me/osintru/', 61, 'https://t.me/antichat/', 8426,
  31.         'https://t.me/nuancesproghumor/', 751, 'https://t.me/howdyho_official/', 2334, 'https://t.me/gurupython', 188]
  32.  
  33.  
  34. class Kanal:
  35.     name: str = list[0]
  36.     stpage: int = list[1]
  37.     embaded: str = "?embed=1"
  38.  
  39.  
  40.  
  41.  
  42. test2 = str(list)
  43.  
  44.  
  45. class Ui_MainWindow(object):
  46.     def setupUi(self, MainWindow):
  47.         if not MainWindow.objectName():
  48.             MainWindow.setObjectName(u"MainWindow")
  49.         MainWindow.resize(731, 640)
  50.         sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
  51.         sizePolicy.setHorizontalStretch(0)
  52.         sizePolicy.setVerticalStretch(0)
  53.         sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
  54.         MainWindow.setSizePolicy(sizePolicy)
  55.         self.centralwidget = QWidget(MainWindow)
  56.         self.centralwidget.setObjectName(u"centralwidget")
  57.         self.groupBox = QGroupBox(self.centralwidget)
  58.         self.groupBox.setObjectName(u"groupBox")
  59.         self.groupBox.setGeometry(QRect(10, 20, 711, 181))
  60.         self.lineEdit_4 = QLineEdit(self.groupBox)
  61.         self.lineEdit_4.setObjectName(u"lineEdit_4")
  62.         self.lineEdit_4.setGeometry(QRect(250, 60, 451, 20))
  63.         self.label_4 = QLabel(self.groupBox)
  64.         self.label_4.setObjectName(u"label_4")
  65.         self.label_4.setGeometry(QRect(10, 60, 231, 21))
  66.         self.lineEdit_3 = QLineEdit(self.groupBox)
  67.         self.lineEdit_3.setObjectName(u"lineEdit_3")
  68.         self.lineEdit_3.setGeometry(QRect(390, 20, 311, 20))
  69.         self.label_3 = QLabel(self.groupBox)
  70.         self.label_3.setObjectName(u"label_3")
  71.         self.label_3.setGeometry(QRect(10, 20, 381, 21))
  72.         self.pushButton = QPushButton(self.groupBox)
  73.         self.pushButton.setObjectName(u"pushButton")
  74.         self.pushButton.setGeometry(QRect(600, 120, 101, 31))
  75.         self.CheckBox = QCheckBox(self.groupBox)
  76.         self.CheckBox.setObjectName(u"CheckBox")
  77.         self.CheckBox.setGeometry(QRect(10, 90, 131, 17))
  78.         self.lineEdit_5 = QLineEdit(self.groupBox)
  79.         self.lineEdit_5.setObjectName(u"lineEdit_5")
  80.         self.lineEdit_5.setGeometry(QRect(220, 120, 201, 20))
  81.         self.lineEdit_6 = QLineEdit(self.groupBox)
  82.         self.lineEdit_6.setObjectName(u"lineEdit_6")
  83.         self.lineEdit_6.setGeometry(QRect(220, 150, 201, 20))
  84.         self.label_5 = QLabel(self.groupBox)
  85.         self.label_5.setObjectName(u"label_5")
  86.         self.label_5.setGeometry(QRect(10, 120, 171, 21))
  87.         self.label_6 = QLabel(self.groupBox)
  88.         self.label_6.setObjectName(u"label_6")
  89.         self.label_6.setGeometry(QRect(10, 150, 171, 21))
  90.         self.groupBox_2 = QGroupBox(self.centralwidget)
  91.         self.groupBox_2.setObjectName(u"groupBox_2")
  92.         self.groupBox_2.setGeometry(QRect(10, 430, 711, 151))
  93.         self.label = QLabel(self.groupBox_2)
  94.         self.label.setObjectName(u"label")
  95.         self.label.setGeometry(QRect(10, 20, 271, 41))
  96.         self.label_2 = QLabel(self.groupBox_2)
  97.         self.label_2.setObjectName(u"label_2")
  98.         self.label_2.setGeometry(QRect(10, 70, 431, 21))
  99.         self.lineEdit_2 = QLineEdit(self.groupBox_2)
  100.         self.lineEdit_2.setObjectName(u"lineEdit_2")
  101.         self.lineEdit_2.setGeometry(QRect(450, 70, 251, 20))
  102.         self.lineEdit = QLineEdit(self.groupBox_2)
  103.         self.lineEdit.setObjectName(u"lineEdit")
  104.         self.lineEdit.setGeometry(QRect(300, 30, 401, 20))
  105.         self.pushButton_2 = QPushButton(self.groupBox_2)
  106.         self.pushButton_2.setObjectName(u"pushButton_2")
  107.         self.pushButton_2.setGeometry(QRect(600, 110, 101, 31))
  108.         self.groupBox_3 = QGroupBox(self.centralwidget)
  109.         self.groupBox_3.setObjectName(u"groupBox_3")
  110.         self.groupBox_3.setGeometry(QRect(10, 210, 711, 131))
  111.         self.textBrowser = QTextBrowser(self.groupBox_3)
  112.         self.textBrowser.setObjectName(u"textBrowser")
  113.         self.textBrowser.setGeometry(QRect(10, 20, 691, 101))
  114.         self.groupBox_4 = QGroupBox(self.centralwidget)
  115.         self.groupBox_4.setObjectName(u"groupBox_4")
  116.         self.groupBox_4.setGeometry(QRect(10, 350, 711, 71))
  117.         self.label_7 = QLabel(self.groupBox_4)
  118.         self.label_7.setObjectName(u"label_7")
  119.         self.label_7.setGeometry(QRect(10, 30, 271, 21))
  120.         self.lineEdit_7 = QLineEdit(self.groupBox_4)
  121.         self.lineEdit_7.setObjectName(u"lineEdit_7")
  122.         self.lineEdit_7.setGeometry(QRect(280, 30, 171, 20))
  123.         self.pushButton_3 = QPushButton(self.groupBox_4)
  124.         self.pushButton_3.setObjectName(u"pushButton_3")
  125.         self.pushButton_3.setGeometry(QRect(480, 20, 101, 31))
  126.         self.pushButton_6 = QPushButton(self.groupBox_4)
  127.         self.pushButton_6.setObjectName(u"pushButton_6")
  128.         self.pushButton_6.setGeometry(QRect(600, 20, 91, 31))
  129.         self.pushButton_4 = QPushButton(self.centralwidget)
  130.         self.pushButton_4.setObjectName(u"pushButton_4")
  131.         self.pushButton_4.setGeometry(QRect(250, 590, 91, 31))
  132.         self.pushButton_5 = QPushButton(self.centralwidget)
  133.         self.pushButton_5.setObjectName(u"pushButton_5")
  134.         self.pushButton_5.setGeometry(QRect(400, 590, 151, 31))
  135.         MainWindow.setCentralWidget(self.centralwidget)
  136.         self.statusbar = QStatusBar(MainWindow)
  137.         self.statusbar.setObjectName(u"statusbar")
  138.         MainWindow.setStatusBar(self.statusbar)
  139.  
  140.         self.retranslateUi(MainWindow)
  141.  
  142.         QMetaObject.connectSlotsByName(MainWindow)
  143.  
  144.     # setupUi
  145.  
  146.     def retranslateUi(self, MainWindow):
  147.         MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"News collector", None))
  148.         self.groupBox.setTitle(QCoreApplication.translate("MainWindow",
  149.                                                           u"Main data",
  150.                                                           None))
  151.         self.lineEdit_4.setText(QCoreApplication.translate("MainWindow", u"-108**********", None))
  152.         self.label_4.setText(QCoreApplication.translate("MainWindow",
  153.                                                         u"ID Your chat id: -108**********",
  154.                                                         None))
  155.         self.lineEdit_3.setText(
  156.             QCoreApplication.translate("MainWindow", u"11***********:AA***********", None))
  157.         self.label_3.setText(QCoreApplication.translate("MainWindow",
  158.                                                         u"Your Telegram token: '11***********:AA***********'",
  159.                                                         None))
  160.         self.pushButton.setText(QCoreApplication.translate("MainWindow", u"Apply", None))
  161.         self.CheckBox.setText(QCoreApplication.translate("MainWindow",
  162.                                                          u"Use proxy",
  163.                                                          None))
  164.         self.lineEdit_5.setText(QCoreApplication.translate("MainWindow", u"8.8.8.8", None))
  165.         self.lineEdit_5.setDisabled(True)
  166.         self.lineEdit_6.setText(QCoreApplication.translate("MainWindow", u"8080", None))
  167.         self.lineEdit_6.setDisabled(True)
  168.         self.label_5.setText(QCoreApplication.translate("MainWindow",
  169.                                                         u"IP address, example: https://8.8.8.8",
  170.                                                         None))
  171.         self.label_6.setText(QCoreApplication.translate("MainWindow",
  172.                                                         u"Port, example: 8080",
  173.                                                         None))
  174.         self.groupBox_2.setTitle(QCoreApplication.translate("MainWindow",
  175.                                                             u"Add channel",
  176.                                                             None))
  177.         self.groupBox_2.setDisabled(True)
  178.         self.label.setText(QCoreApplication.translate("MainWindow",
  179.                                                       u"Channel lik: https://t.me/cnlname/",
  180.                                                       None))
  181.         self.label_2.setText(QCoreApplication.translate("MainWindow",
  182.                                                         u"News number, only the last one will be resend: 9999",
  183.                                                         None))
  184.         self.lineEdit_2.setText(QCoreApplication.translate("MainWindow", u"Enter news number to add...", None))
  185.         self.textBrowser.setText(QCoreApplication.translate("MainWindow", str(list), None))
  186.         self.textBrowser.setDisabled(True)
  187.         self.lineEdit.setText(QCoreApplication.translate("MainWindow", u"Enter link to add...", None))
  188.         self.pushButton_2.setText(QCoreApplication.translate("MainWindow", u"Add", None))
  189.         self.groupBox_3.setTitle(QCoreApplication.translate("MainWindow",
  190.                                                             u"List of entered channels",
  191.                                                             None))
  192.         self.groupBox_3.setDisabled(True)
  193.         self.groupBox_4.setTitle(QCoreApplication.translate("MainWindow",
  194.                                                             u"Delete channel",
  195.                                                             None))
  196.         self.groupBox_4.setDisabled(True)
  197.         self.label_7.setText(
  198.             QCoreApplication.translate("MainWindow", u"Enter chanel name: https://t.me/chlname/", None))
  199.         self.lineEdit_7.setText(QCoreApplication.translate("MainWindow", u"Enter chanel number...", None))
  200.         self.pushButton_3.setText(QCoreApplication.translate("MainWindow", u"Delite", None))
  201.         self.pushButton_6.setText(QCoreApplication.translate("MainWindow", u"Delite all", None))
  202.         self.pushButton_4.setText(QCoreApplication.translate("MainWindow", u"Start", None))
  203.         self.pushButton_4.setDisabled(True)
  204.         self.pushButton_5.setText(QCoreApplication.translate("MainWindow", u"Stop after next checking", None))
  205.         self.pushButton_5.setDisabled(True)
  206.     # retranslateUi
  207.  
  208.  
  209. print(list)
  210. listtt = []
  211.  
  212.  
  213. def aph_reload(apihelper):
  214.     apihelper = reload(apihelper)
  215.  
  216.  
  217. class MainWindow(QMainWindow, Ui_MainWindow):
  218.     global TOKEN
  219.  
  220.     def starter(self):
  221.         listtt.clear()
  222.         self.collector()
  223.  
  224.     def proxy(self):
  225.         if self.CheckBox.isChecked() == True:
  226.             self.lineEdit_5.setEnabled(True)
  227.             self.lineEdit_6.setEnabled(True)
  228.         if self.CheckBox.isChecked() == False:
  229.             self.lineEdit_5.setEnabled(False)
  230.             self.lineEdit_6.setEnabled(False)
  231.  
  232.     def delall(self):
  233.         list.clear()
  234.         self.textBrowser.clear()
  235.         print(list)
  236.  
  237.     def add_chanel(self):
  238.         print(list)
  239.         showlist1 = str(list)
  240.         self.textBrowser.setText(showlist1)
  241.         try:
  242.             req = requests.get(self.lineEdit.text() + self.lineEdit_2.text())
  243.             chn_to_test = req.text
  244.             soup = BeautifulSoup(chn_to_test, 'lxml')
  245.             members = soup.find_all("meta", {"content": "https://telegram.org/img/t_logo.png"})
  246.             if len(members) > 0:
  247.                 self.lineEdit.setText(
  248.                     QCoreApplication.translate("MainWindow", u"Wrong channel or news number", None))
  249.                 self.lineEdit_2.setText(
  250.                     QCoreApplication.translate("MainWindow", u"Wrong channel or news number", None))
  251.             else:
  252.                 list.append(self.lineEdit.text())
  253.                 list.append(self.lineEdit_2.text())
  254.                 self.textBrowser.clear()
  255.                 showlist = str(list)
  256.                 self.textBrowser.setText(showlist)
  257.         except Exception:
  258.             self.lineEdit.setText(
  259.                 QCoreApplication.translate("MainWindow", u"Wrong channel or news number", None))
  260.             self.lineEdit_2.setText(
  261.                 QCoreApplication.translate("MainWindow", u"Wrong channel or news number", None))
  262.  
  263.     def delete_chanel(self):
  264.         delind = list.index(self.lineEdit_7.text())
  265.         del list[delind]
  266.         del list[delind]
  267.         self.textBrowser.clear()
  268.         showlist2 = str(list)
  269.         self.textBrowser.setText(showlist2)
  270.  
  271.     def apply(self):
  272.  
  273.         aph_reload(apihelper)
  274.         try:
  275.             if self.CheckBox.isChecked() == False:
  276.                 bot = telebot.TeleBot(self.lineEdit_3.text())
  277.                 info = bot.get_me()
  278.                 if info != None:
  279.                     print(f'Bot info: {info}')
  280.                     chadi = self.lineEdit_4.text()
  281.                     try:
  282.                         bot.send_message(chat_id=chadi, text="Connection test")
  283.                         print('No problem detected. Message send')
  284.                         self.groupBox_2.setDisabled(False)
  285.                         self.textBrowser.setDisabled(False)
  286.                         self.pushButton_4.setDisabled(False)
  287.                         self.groupBox_3.setDisabled(False)
  288.                         self.groupBox_4.setDisabled(False)
  289.                         self.pushButton_5.setDisabled(False)
  290.                     except Exception:
  291.                         self.lineEdit_4.setText(
  292.                             QCoreApplication.translate("MainWindow", u"Wrong channel name!", None))
  293.                         print("ConnectionError!!!")
  294.             else:
  295.                 prxaddr = self.lineEdit_5.text()
  296.                 port = self.lineEdit_6.text()
  297.                 proxy_full = ("socks5://" + prxaddr + ":" + port)
  298.                 print(proxy_full)
  299.                 try:
  300.                     apihelper.proxy = {'https': proxy_full}
  301.                     apihelper.proxy = {'https': proxy_full}
  302.                     bot = telebot.TeleBot(self.lineEdit_3.text())
  303.                     info = bot.get_me()
  304.                     print(info)
  305.                     if info != None:
  306.                         print(f'Bot info: {info}')
  307.                         chadi = self.lineEdit_4.text()
  308.                         try:
  309.                             bot.send_message(chat_id=chadi, text="Connection test")
  310.                             print('No problem detected. Message send')
  311.                             self.groupBox_2.setDisabled(False)
  312.                             self.textBrowser.setDisabled(False)
  313.                             self.pushButton_4.setDisabled(False)
  314.                             self.groupBox_3.setDisabled(False)
  315.                             self.groupBox_4.setDisabled(False)
  316.                             self.pushButton_5.setDisabled(False)
  317.                         except Exception:
  318.                             self.lineEdit_4.setText(
  319.                                 QCoreApplication.translate("MainWindow", u"Wrong channel name!", None))
  320.                             print("ConnectionError!!!")
  321.                 except Exception:
  322.                     print("Proxy error")
  323.                     self.lineEdit_5.setText(QCoreApplication.translate("MainWindow", u"Proxy error", None))
  324.                     self.lineEdit_6.setText(QCoreApplication.translate("MainWindow", u"Proxy error", None))
  325.         except TypeError:
  326.             self.lineEdit_3.setText(QCoreApplication.translate("MainWindow", u"Token error!", None))
  327.             print("Error")
  328.  
  329.     def solver(self, link, counter2):
  330.         print(link)
  331.         print(list[counter2])
  332.         bot = telebot.TeleBot(self.lineEdit_3.text())
  333.         chadi = self.lineEdit_4.text()
  334.  
  335.         try:
  336.             bot.send_message(chat_id=chadi, text=link)
  337.             print('No problem detected. Message send')
  338.         except OSError:
  339.             print("ConnectionError - Sending again after 5 seconds!!!")
  340.             time.sleep(5)
  341.             bot.send_message(chat_id=chadi, text=link)
  342.             print('Problem solved')
  343.  
  344.     def timer(self):
  345.         print("timer on")
  346.         t = Timer(500, self.collector)
  347.         t.start()
  348.         print("timer off")
  349.  
  350.     def collector(self):
  351.         print("Collector started")
  352.         self.groupBox.setEnabled(False)
  353.         self.groupBox_2.setEnabled(False)
  354.         self.groupBox_4.setEnabled(False)
  355.         self.pushButton_4.setEnabled(False)
  356.         self.pushButton_5.setEnabled(True)
  357.         counter = 0
  358.         counter2 = 1
  359.         Kanal.name = list[counter]
  360.         Kanal.stpage = list[counter2]
  361.         while True:
  362.             print("Testing chanell", Kanal.name, Kanal.stpage)
  363.             time.sleep(1)
  364.             r = requests.get(Kanal.name + str(Kanal.stpage) + Kanal.embaded)
  365.             data = r.text
  366.             soup = BeautifulSoup(data, 'lxml')
  367.             pg_not_fnd = soup.find_all("div", {"class": "tgme_widget_message_error"})
  368.             if len(pg_not_fnd) > 0:
  369.                 print(list[counter2])
  370.                 print(Kanal.stpage)
  371.  
  372.                 if int(list[counter2]) < int(Kanal.stpage):
  373.                     list[counter2] = int(Kanal.stpage)
  374.                     Kanal.stpage = str(int(Kanal.stpage) - 1)
  375.                     print(Kanal.name + str(Kanal.stpage) + Kanal.embaded)
  376.                     r1 = requests.get(Kanal.name + str(Kanal.stpage) + Kanal.embaded)
  377.                     data1 = r1.text
  378.                     soup1 = BeautifulSoup(data1, 'lxml')
  379.                     for results in soup1.find_all("a", {"class": "tgme_widget_message_date"}):
  380.                         link = results.get('href')
  381.                         print(list[counter2])
  382.  
  383.                         self.solver(link, counter2)
  384.                         print(list)
  385.                 counter = counter + 2
  386.                 counter2 = counter2 + 2
  387.                 stop = len(list)
  388.                 if counter >= stop:
  389.                     print(list)
  390.                     if len(listtt) == 0:
  391.                         self.timer()
  392.                         print("Sleeping1")
  393.                     else:
  394.                         aph_reload(apihelper)
  395.                         print("api stoped")
  396.                         self.groupBox.setEnabled(True)
  397.                         self.groupBox_2.setEnabled(True)
  398.                         self.groupBox_3.setEnabled(True)
  399.                         self.groupBox_4.setEnabled(True)
  400.                         self.pushButton_4.setEnabled(True)
  401.                         self.pushButton_5.setEnabled(False)
  402.  
  403.                     print("stoper")
  404.                     break
  405.                 Kanal.name = list[counter]
  406.                 Kanal.stpage = list[counter2]
  407.             else:
  408.                 print(55)
  409.                 Kanal.stpage = str(int(Kanal.stpage) + 1)
  410.  
  411.                 print(list[counter2])
  412.     def stop(self):
  413.         listtt.append(1)
  414.         print("Stop pushed")
  415.  
  416.     def __init__(self):
  417.         super().__init__()
  418.         self.setupUi(self)
  419.         self.pushButton.clicked.connect(self.apply)
  420.         self.pushButton_3.clicked.connect(self.delete_chanel)
  421.         self.pushButton_2.clicked.connect(self.add_chanel)
  422.         self.pushButton_4.clicked.connect(self.starter)
  423.         self.pushButton_5.clicked.connect(self.stop)
  424.         self.pushButton_6.clicked.connect(self.delall)
  425.         self.CheckBox.clicked.connect(self.proxy)
  426.  
  427.     async def start(self):
  428.         self.show()
  429.  
  430.  
  431. app = QApplication()
  432. loop = QEventLoop(app)
  433. window = MainWindow()
  434. loop.create_task(window.start())
  435. loop.run_forever()
Add Comment
Please, Sign In to add comment