this is based on calsyslab project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
8.8 KiB

  1. __author__ = 'DarkWeb'
  2. '''
  3. Atlas Marketplace Crawler (Selenium)
  4. '''
  5. from selenium import webdriver
  6. from selenium.common.exceptions import NoSuchElementException
  7. from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
  8. from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
  9. from selenium.webdriver.firefox.service import Service
  10. from selenium.webdriver.support.ui import WebDriverWait
  11. from selenium.webdriver.support.ui import Select
  12. from selenium.webdriver.support import expected_conditions as EC
  13. from selenium.webdriver.common.by import By
  14. from PIL import Image
  15. import urllib.parse as urlparse
  16. import os, re, time
  17. from datetime import date
  18. import subprocess
  19. import configparser
  20. from bs4 import BeautifulSoup
  21. from MarketPlaces.Initialization.prepare_parser import new_parse
  22. from MarketPlaces.Atlas.parser import atlas_links_parser
  23. from MarketPlaces.Utilities.utilities import cleanHTML
  24. counter = 1
  25. baseURL = 'http://5jbmfuip2ohxabshoapzy23kz36movs4clpivnsu7lnrn4osazhzdtqd.onion'
  26. def startCrawling():
  27. mktName = getMKTName()
  28. # driver = getAccess()
  29. #
  30. # if driver != 'down':
  31. # try:
  32. # login(driver)
  33. # crawlForum(driver)
  34. # except Exception as e:
  35. # print(driver.current_url, e)
  36. # closeDriver(driver)
  37. new_parse(mktName, baseURL, True)
  38. # Returns the name of the website
  39. def getMKTName():
  40. name = 'Atlas'
  41. return name
  42. # Return the base link of the website
  43. def getFixedURL():
  44. url = 'http://5jbmfuip2ohxabshoapzy23kz36movs4clpivnsu7lnrn4osazhzdtqd.onion'
  45. return url
  46. # Closes Tor Browser
  47. def closeDriver(driver):
  48. # global pid
  49. # os.system("taskkill /pid " + str(pro.pid))
  50. # os.system("taskkill /t /f /im tor.exe")
  51. print('Closing Tor...')
  52. driver.close()
  53. time.sleep(3)
  54. return
  55. # Creates FireFox 'driver' and configure its 'Profile'
  56. # to use Tor proxy and socket
  57. def createFFDriver():
  58. from MarketPlaces.Initialization.markets_mining import config
  59. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  60. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  61. ff_prof.set_preference("places.history.enabled", False)
  62. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  63. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  64. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  65. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  66. ff_prof.set_preference("signon.rememberSignons", False)
  67. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  68. # ff_prof.set_preference("network.dns.disablePrefetch", True)
  69. # ff_prof.set_preference("network.http.sendRefererHeader", 0)
  70. ff_prof.set_preference("permissions.default.image", 3)
  71. ff_prof.set_preference("browser.download.folderList", 2)
  72. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  73. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  74. ff_prof.set_preference('network.proxy.type', 1)
  75. ff_prof.set_preference("network.proxy.socks_version", 5)
  76. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  77. ff_prof.set_preference('network.proxy.socks_port', 9150)
  78. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  79. ff_prof.set_preference("javascript.enabled", False)
  80. ff_prof.update_preferences()
  81. service = Service(config.get('TOR', 'geckodriver_path'))
  82. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  83. driver.maximize_window()
  84. return driver
  85. #the driver 'gets' the url, attempting to get on the site, if it can't access return 'down'
  86. def getAccess():
  87. url = getFixedURL()
  88. driver = createFFDriver()
  89. try:
  90. driver.get(url)
  91. return driver
  92. except:
  93. driver.close()
  94. return 'down'
  95. def agreeToTerms(driver):
  96. try:
  97. agree_button = driver.find_element(by=By.XPATH, value='//input[@type="submit" and @name="agree" and @value="Yes, I agree"]')
  98. agree_button.click()
  99. except Exception as e:
  100. print('Problem with clicking agree button', e)
  101. def login(driver):
  102. # input("Press ENTER when CAPTCHA is complete and login page has loaded\n")
  103. input("Press ENTER when captcha is solved")
  104. WebDriverWait(driver, 100).until(EC.visibility_of_element_located(
  105. (By.XPATH, '//*[@id="username"]')))
  106. # entering username and password into input boxes
  107. usernameBox = driver.find_element(by=By.XPATH, value='//*[@id="username"]')
  108. # Username here
  109. usernameBox.send_keys('atldark')
  110. passwordBox = driver.find_element(by=By.XPATH, value='//*[@id="password"]')
  111. # Password here
  112. passwordBox.send_keys('a1T2l3dark')
  113. input("Press ENTER when captcha is solved and you're logged in")
  114. #
  115. # try:
  116. # agree_button = driver.find_element(by=By.XPATH, value='/html/body/main/div/form/div[6]/button')
  117. # agree_button.click()
  118. # except Exception as e:
  119. # print('Problem with clicking login button', e)
  120. def savePage(driver, page, url):
  121. cleanPage = cleanHTML(driver, page)
  122. filePath = getFullPathName(url)
  123. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  124. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  125. return
  126. def getFullPathName(url):
  127. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  128. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  129. fileName = getNameFromURL(url)
  130. if isDescriptionLink(url):
  131. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  132. else:
  133. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  134. return fullPath
  135. def getMKTName() -> str:
  136. name = 'Atlas'
  137. return name
  138. def getNameFromURL(url):
  139. global counter
  140. name = ''.join(e for e in url if e.isalnum())
  141. if name == '':
  142. name = str(counter)
  143. counter = counter + 1
  144. return name
  145. def getInterestedLinks():
  146. links = []
  147. # hacking
  148. # links.append('http://5jbmfuip2ohxabshoapzy23kz36movs4clpivnsu7lnrn4osazhzdtqd.onion/c/hacking')
  149. # fraud
  150. links.append('http://5jbmfuip2ohxabshoapzy23kz36movs4clpivnsu7lnrn4osazhzdtqd.onion/c/fraud')
  151. # software
  152. links.append('http://5jbmfuip2ohxabshoapzy23kz36movs4clpivnsu7lnrn4osazhzdtqd.onion/c/software')
  153. # services
  154. # links.append('http://5jbmfuip2ohxabshoapzy23kz36movs4clpivnsu7lnrn4osazhzdtqd.onion/c/services')
  155. return links
  156. def crawlForum(driver):
  157. print("Crawling the Atlas market")
  158. linksToCrawl = getInterestedLinks()
  159. i = 0
  160. while i < len(linksToCrawl):
  161. link = linksToCrawl[i]
  162. print('Crawling :', link)
  163. try:
  164. has_next_page = True
  165. count = 0
  166. while has_next_page:
  167. try:
  168. driver.get(link)
  169. except:
  170. driver.refresh()
  171. html = driver.page_source
  172. savePage(driver, html, link)
  173. list = productPages(html)
  174. for item in list:
  175. itemURL = urlparse.urljoin(baseURL, str(item))
  176. try:
  177. driver.get(itemURL)
  178. except:
  179. driver.refresh()
  180. savePage(driver, driver.page_source, item)
  181. driver.back()
  182. # comment out
  183. break
  184. # comment out
  185. # if count == 1:
  186. # break
  187. try:
  188. temp = driver.find_element(by=By.XPATH, value='/html/body/div[2]/form/section/div/div/div[2]/div[2]')
  189. temp = temp.find_elements(by=By.TAG_NAME, value='a')
  190. a_tag = temp[-1]
  191. if a_tag:
  192. temp = a_tag.find_element(by=By.CLASS_NAME, value='double-arrow-right')
  193. if temp:
  194. link = a_tag.get_attribute('href')
  195. else:
  196. link = ''
  197. if link == "":
  198. raise NoSuchElementException
  199. count += 1
  200. except NoSuchElementException:
  201. has_next_page = False
  202. except Exception as e:
  203. print(link, e)
  204. i += 1
  205. print("Crawling the vortex market done.")
  206. # Returns 'True' if the link is Topic link, may need to change for every website
  207. def isDescriptionLink(url):
  208. if 'product' in url:
  209. return True
  210. return False
  211. # Returns True if the link is a listingPage link, may need to change for every website
  212. def isListingLink(url):
  213. if '/c/' in url:
  214. return True
  215. return False
  216. def productPages(html):
  217. soup = BeautifulSoup(html, "html.parser")
  218. return atlas_links_parser(soup)
  219. def crawler():
  220. startCrawling()