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.

256 lines
7.7 KiB

  1. __author__ = 'chris'
  2. '''
  3. RobinhoodMarket Market 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.common.by import By
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.support.ui import WebDriverWait
  13. from PIL import Image
  14. import urllib.parse as urlparse
  15. import os, re, time
  16. import subprocess
  17. import configparser
  18. from bs4 import BeautifulSoup
  19. from MarketPlaces.Initialization.prepare_parser import new_parse
  20. from MarketPlaces.RobinhoodMarket.parser import Robinhood_links_parser
  21. from MarketPlaces.Utilities.utilities import cleanHTML
  22. counter = 1
  23. baseURL = 'http://ilr3qzubfnx33vbhal7l5coo4ftqlkv2tboph4ujog5crz6m5ua2b2ad.onion/'
  24. # Opens Tor Browser, crawls the website
  25. def startCrawling():
  26. marketName = getMKTName()
  27. driver = getAccess()
  28. if driver != 'down':
  29. try:
  30. # Captcha
  31. input("Press ENTER when website has loaded")
  32. # Robinhood doesn't need login
  33. # login(driver)
  34. crawlForum(driver)
  35. except Exception as e:
  36. print(driver.current_url, e)
  37. closeDriver(driver)
  38. new_parse(marketName, baseURL, True)
  39. # Login is not needed in Robinhood
  40. def login(driver):
  41. pass
  42. # Returns the name of the website
  43. def getMKTName():
  44. name = 'RobinhoodMarket'
  45. return name
  46. # Return the link of the website
  47. def getFixedURL():
  48. url = 'http://ilr3qzubfnx33vbhal7l5coo4ftqlkv2tboph4ujog5crz6m5ua2b2ad.onion/'
  49. return url
  50. # Closes Tor Browser
  51. def closeDriver(driver):
  52. # global pid
  53. # os.system("taskkill /pid " + str(pro.pid))
  54. # os.system("taskkill /t /f /im tor.exe")
  55. print('Closing Tor...')
  56. driver.quit()
  57. time.sleep(3)
  58. return
  59. # Creates FireFox 'driver' and configure its 'Profile'
  60. # to use Tor proxy and socket
  61. def createFFDriver():
  62. from MarketPlaces.Initialization.markets_mining import config
  63. ff_binary = FirefoxBinary(config.get('TOR', 'firefox_binary_path'))
  64. ff_prof = FirefoxProfile(config.get('TOR', 'firefox_profile_path'))
  65. ff_prof.set_preference("places.history.enabled", False)
  66. ff_prof.set_preference("privacy.clearOnShutdown.offlineApps", True)
  67. ff_prof.set_preference("privacy.clearOnShutdown.passwords", True)
  68. ff_prof.set_preference("privacy.clearOnShutdown.siteSettings", True)
  69. ff_prof.set_preference("privacy.sanitize.sanitizeOnShutdown", True)
  70. ff_prof.set_preference("signon.rememberSignons", False)
  71. ff_prof.set_preference("network.cookie.lifetimePolicy", 2)
  72. ff_prof.set_preference("network.dns.disablePrefetch", True)
  73. ff_prof.set_preference("network.http.sendRefererHeader", 0)
  74. ff_prof.set_preference("permissions.default.image", 3)
  75. ff_prof.set_preference("browser.download.folderList", 2)
  76. ff_prof.set_preference("browser.download.manager.showWhenStarting", False)
  77. ff_prof.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
  78. ff_prof.set_preference('network.proxy.type', 1)
  79. ff_prof.set_preference("network.proxy.socks_version", 5)
  80. ff_prof.set_preference('network.proxy.socks', '127.0.0.1')
  81. ff_prof.set_preference('network.proxy.socks_port', 9150)
  82. ff_prof.set_preference('network.proxy.socks_remote_dns', True)
  83. ff_prof.set_preference("javascript.enabled", False)
  84. ff_prof.update_preferences()
  85. service = Service(config.get('TOR', 'geckodriver_path'))
  86. driver = webdriver.Firefox(firefox_binary=ff_binary, firefox_profile=ff_prof, service=service)
  87. driver.maximize_window()
  88. return driver
  89. def getAccess():
  90. url = getFixedURL()
  91. driver = createFFDriver()
  92. try:
  93. driver.get(url)
  94. return driver
  95. except:
  96. driver.close()
  97. return 'down'
  98. # Saves the crawled html page
  99. def savePage(driver, page, url):
  100. cleanPage = cleanHTML(driver, page)
  101. filePath = getFullPathName(url)
  102. os.makedirs(os.path.dirname(filePath), exist_ok=True)
  103. open(filePath, 'wb').write(cleanPage.encode('utf-8'))
  104. return
  105. # Gets the full path of the page to be saved along with its appropriate file name
  106. def getFullPathName(url):
  107. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  108. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + getMKTName() + "/HTML_Pages")
  109. fileName = getNameFromURL(url)
  110. if isDescriptionLink(url):
  111. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Description\\' + fileName + '.html')
  112. else:
  113. fullPath = os.path.join(mainDir, CURRENT_DATE + r'\\Listing\\' + fileName + '.html')
  114. return fullPath
  115. # Creates the file name from passed URL
  116. def getNameFromURL(url):
  117. global counter
  118. name = ''.join(e for e in url if e.isalnum())
  119. if name == '':
  120. name = str(counter)
  121. counter = counter + 1
  122. return name
  123. def getInterestedLinks():
  124. links = []
  125. # Hacking
  126. links.append('http://ilr3qzubfnx33vbhal7l5coo4ftqlkv2tboph4ujog5crz6m5ua2b2ad.onion/product-category/hacking/')
  127. # Other Software
  128. links.append('http://ilr3qzubfnx33vbhal7l5coo4ftqlkv2tboph4ujog5crz6m5ua2b2ad.onion/product-category/other-software/')
  129. return links
  130. def crawlForum(driver):
  131. print("Crawling the Robinhood market")
  132. linksToCrawl = getInterestedLinks()
  133. i = 0
  134. while i < len(linksToCrawl):
  135. link = linksToCrawl[i]
  136. print('Crawling :', link)
  137. try:
  138. has_next_page = True
  139. count = 0
  140. while has_next_page:
  141. try:
  142. driver.get(link)
  143. except:
  144. driver.refresh()
  145. html = driver.page_source
  146. savePage(driver, html, link)
  147. list = productPages(html)
  148. for c, item in enumerate(list):
  149. itemURL = urlparse.urljoin(baseURL, str(item))
  150. try:
  151. driver.get(itemURL)
  152. except:
  153. driver.refresh()
  154. savePage(driver, driver.page_source, item)
  155. driver.back()
  156. # comment out
  157. # if c == 4:
  158. # break
  159. # comment out
  160. # if count == 1:
  161. # break
  162. # go to next page of market
  163. try:
  164. nav = driver.find_element(by=By.XPATH, value="//a[@class='next page-numbers']")
  165. link = nav.get_attribute('href')
  166. if link == "":
  167. raise NoSuchElementException
  168. count += 1
  169. except NoSuchElementException:
  170. has_next_page = False
  171. except Exception as e:
  172. print(link, e)
  173. i += 1
  174. print("Crawling the Robinhood market done.")
  175. # Returns 'True' if the link is Topic link
  176. def isDescriptionLink(url):
  177. if 'product' in url and 'category' not in url:
  178. return True
  179. return False
  180. # Returns True if the link is a listingPage link
  181. def isListingLink(url):
  182. if 'category=' in url:
  183. return True
  184. return False
  185. # calling the parser to define the links
  186. def productPages(html):
  187. soup = BeautifulSoup(html, "html.parser")
  188. return Robinhood_links_parser(soup)
  189. def crawler():
  190. startCrawling()
  191. # print("Crawling and Parsing BestCardingWorld .... DONE!")
  192. if __name__ == '__main__':
  193. startCrawling()