browser.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. from selenium import webdriver
  2. import time
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.action_chains import ActionChains
  5. from selenium.webdriver.common.by import By
  6. from random import randint, choice
  7. from Console import console
  8. from settings import STANDART_DELAY
  9. import requests
  10. import undetected_chromedriver.v2 as uc
  11. import os
  12. class ChromeBrowser:
  13. fake_brows = []
  14. def __init__(self):
  15. self.driver = None
  16. page = requests.get('https://fake-useragent.herokuapp.com/browsers/0.1.11')
  17. js = page.json()
  18. brows = js['browsers']
  19. for el in brows:
  20. if el == 'chrome':
  21. for br in brows[el]:
  22. self.fake_brows.append(br)
  23. def get_user_agent(self, others=False):
  24. browser = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"
  25. if others:
  26. browser = choice(self.fake_brows)
  27. ua = "user-agent=" + browser
  28. return ua
  29. def GetUrl(self, url, delay=60, use_old_browser=False, PROXY=None):
  30. selenium_url = "http://localhost:4444/wd/hub"
  31. caps = {'browserName': 'chrome'}
  32. options = webdriver.ChromeOptions()
  33. options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
  34. options.add_experimental_option('useAutomationExtension', False)
  35. options.add_argument("--disable-blink-features=AutomationControlled")
  36. options.add_argument('--headless')
  37. options.add_argument(self.get_user_agent(others=True))
  38. # options.add_argument(
  39. # "user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36")
  40. options.add_argument("--disable-dev-shm-usage")
  41. options.add_argument("--no-sandbox")
  42. if PROXY:
  43. options.add_argument('--proxy-server=https://%s' % PROXY)
  44. self.driver = webdriver.Remote(command_executor=selenium_url, desired_capabilities=caps, options=options)
  45. # action = ActionChains(driver)
  46. self.driver.maximize_window()
  47. script = "window.open('{}')".format(url)
  48. self.driver.execute_script(script)
  49. time.sleep(1)
  50. self.driver.switch_to.window(self.driver.window_handles[1])
  51. self.driver.save_screenshot("photos/gg1.png")
  52. console.send_message(self.driver.title)
  53. time.sleep(3)
  54. console.send_message(self.driver.current_url)
  55. self.driver.save_screenshot("photos/gg.png")
  56. for i in range(delay // STANDART_DELAY + (delay % STANDART_DELAY) * STANDART_DELAY // STANDART_DELAY):
  57. els = self.driver.find_elements(value="button.btn.btn-primary.g-recaptcha", by=By.CSS_SELECTOR)
  58. if els:
  59. els[0].click()
  60. ActionChains(self.driver).click(els[0]).perform()
  61. time.sleep(3)
  62. console.send_message("Clicking on button")
  63. self.driver.execute_script("window.scrollTo(0, {})".format(randint(1, 15))) # session timeout - 60 sec
  64. time.sleep(STANDART_DELAY)
  65. self.driver.save_screenshot("photos/g1g.png")
  66. self.driver.close()
  67. self.driver.quit()
  68. def GetBotUrl(self, url, delay=60, use_old_browser=False, PROXY=None):
  69. selenium_url = "http://localhost:4444/wd/hub"
  70. caps = {'browserName': 'chrome'}
  71. options = webdriver.ChromeOptions()
  72. options.add_argument("start-maximized")
  73. options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
  74. options.add_experimental_option('useAutomationExtension', False)
  75. options.add_argument("--disable-blink-features=AutomationControlled")
  76. options.add_argument('--headless')
  77. options.add_argument(self.get_user_agent(others=True))
  78. options.add_argument("--disable-dev-shm-usage")
  79. options.add_argument("--no-sandbox")
  80. if PROXY:
  81. options.add_argument('--proxy-server=https://%s' % PROXY)
  82. self.driver = webdriver.Remote(command_executor=selenium_url, desired_capabilities=caps, options=options)
  83. self.driver.maximize_window()
  84. script = "window.open('{}')".format(url)
  85. self.driver.execute_script(script)
  86. time.sleep(1)
  87. self.driver.switch_to.window(self.driver.window_handles[1])
  88. self.driver.save_screenshot("photos/gg1.png")
  89. console.send_message(self.driver.title)
  90. self.driver.execute_script("window.scrollTo(0, {})".format(randint(1, 15))) # session timeout - 60 sec
  91. time.sleep(10)
  92. self.driver.execute_script("window.scrollTo(0, {})".format(randint(1, 15))) # session timeout - 60 sec
  93. time.sleep(10)
  94. els = self.driver.find_elements(value="button.btn.btn-primary.g-recaptcha", by=By.CSS_SELECTOR)
  95. if els:
  96. els[0].click()
  97. ActionChains(self.driver).click(els[0]).perform()
  98. time.sleep(3)
  99. console.send_message("Clicking on button")
  100. self.driver.execute_script("window.scrollTo(0, {})".format(randint(1, 15))) # session timeout - 60 sec
  101. time.sleep(STANDART_DELAY)
  102. self.driver.save_screenshot("photos/g1g.png")
  103. telegram_link =self.driver.find_elements(value="tgme_page_extra", by=By.CLASS_NAME)
  104. bot_name = self.driver.find_elements(value= "tgme_page_title", by=By.CLASS_NAME)
  105. tg_link, bt_name = None, None
  106. if telegram_link:
  107. tg_link = telegram_link[0].text
  108. if bot_name:
  109. bt_name = bot_name[0].text
  110. self.driver.close()
  111. self.driver.quit()
  112. return [tg_link, bt_name]
  113. def GetGroupUrl(self, url, delay=60, use_old_browser=False, PROXY=None):
  114. selenium_url = "http://localhost:4444/wd/hub"
  115. caps = {'browserName': 'chrome'}
  116. options = webdriver.ChromeOptions()
  117. options.add_argument("start-maximized")
  118. options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
  119. options.add_experimental_option('useAutomationExtension', False)
  120. options.add_argument("--disable-blink-features=AutomationControlled")
  121. options.add_argument('--headless')
  122. options.add_argument(self.get_user_agent(others=True))
  123. options.add_argument("--disable-dev-shm-usage")
  124. options.add_argument("--no-sandbox")
  125. if PROXY:
  126. options.add_argument('--proxy-server=https://%s' % PROXY)
  127. self.driver = webdriver.Remote(command_executor=selenium_url, desired_capabilities=caps, options=options)
  128. script = "window.open('{}')".format(url)
  129. self.driver.execute_script(script)
  130. time.sleep(0.5)
  131. self.driver.switch_to.window(self.driver.window_handles[1])
  132. self.driver.save_screenshot("photos/gg1.png")
  133. console.send_message(self.driver.title)
  134. self.driver.execute_script("window.scrollTo(0, {})".format(randint(1, 15)))
  135. time.sleep(1)
  136. self.driver.execute_script("window.scrollTo(0, {})".format(randint(1, 15)))
  137. time.sleep(5)
  138. els = self.driver.find_elements(value="button.btn.btn-primary.g-recaptcha", by=By.CSS_SELECTOR)
  139. if els:
  140. els[0].click()
  141. ActionChains(self.driver).click(els[0]).perform()
  142. time.sleep(3)
  143. console.send_message("Clicking on button")
  144. self.driver.execute_script("window.scrollTo(0, {})".format(randint(1, 15))) # session timeout - 60 sec
  145. time.sleep(STANDART_DELAY)
  146. self.driver.save_screenshot("photos/g1g.png")
  147. channel_name = self.driver.find_elements(value="tgme_page_title", by=By.CLASS_NAME)
  148. channel_link = self.driver.find_elements(value="tgme_action_button_new", by= By.CLASS_NAME)
  149. channel_link_text, channel_name_text = None, None
  150. if channel_name:
  151. channel_name_text = channel_name[0].text
  152. if "Telegram: Contact" in self.driver.title:
  153. channel_link_text = self.driver.title
  154. channel_link_text = channel_link_text.replace("Telegram: Contact ", "")
  155. return [channel_name_text, channel_link_text]
  156. if __name__ == '__main__':
  157. pass