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.

316 lines
12 KiB

  1. __author__ = 'DarkWeb'
  2. # Here, we are importing the auxiliary functions to clean or convert data
  3. from MarketPlaces.Utilities.utilities import *
  4. # Here, we are importing BeautifulSoup to search through the HTML tree
  5. from bs4 import BeautifulSoup
  6. # parses description pages, so takes html pages of description pages using soup object, and parses it for info it needs
  7. # stores info it needs in different lists, these lists are returned after being organized
  8. # @param: soup object looking at html page of description page
  9. # return: 'row' that contains a variety of lists that each hold info on the description page
  10. def ares_description_parser(soup):
  11. # Fields to be parsed
  12. vendor = "-1" # 0 *Vendor_Name
  13. success = "-1" # 1 Vendor_Successful_Transactions
  14. rating_vendor = "-1" # 2 Vendor_Rating
  15. name = "-1" # 3 *Product_Name
  16. describe = "-1" # 4 Product_Description
  17. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  18. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  19. category = "-1" # 7 Product_Category
  20. views = "-1" # 8 Product_Number_Of_Views
  21. reviews = "-1" # 9 Product_Number_Of_Reviews
  22. rating_item = "-1" # 10 Product_Rating
  23. addDate = "-1" # 11 Product_AddedDate
  24. BTC = "-1" # 12 Product_BTC_SellingPrice
  25. USD = "-1" # 13 Product_USD_SellingPrice
  26. EURO = "-1" # 14 Product_EURO_SellingPrice
  27. sold = "-1" # 15 Product_QuantitySold
  28. left = "-1" # 16 Product_QuantityLeft
  29. shipFrom = "-1" # 17 Product_ShippedFrom
  30. shipTo = "-1" # 18 Product_ShippedTo
  31. image = "-1" # 19 Product_Image
  32. vendor_image = "-1" # 20 Vendor_Image
  33. # Finding Product Name
  34. divmb = soup.find('div', {'class': "col-md-12 my-2"})
  35. name = divmb.find('span', {'class': "btn btn-sm btn-outline-dark w-100 active rounded-0"}).text
  36. name = name.replace('\n', ' ')
  37. name = name.replace(",", "")
  38. name = name.strip()
  39. box = soup.find('div', {'class': "col-md-7"}).find('span')
  40. box = box.findAll('span', {'class': "btn btn-mgray btn-sm w-100 active border-danger"})
  41. # Finding Vendor
  42. vendor = soup.find('a', {'class': "btn btn-sm btn-mgray my-1 w-100 text-white"}).get('href')
  43. vendor = vendor.split('otherParty=')[-1].strip()
  44. # Finding Vendor Rating
  45. temp = box[1]
  46. stars = len(temp.findAll('i', {"class": "fas fa-star"}))
  47. half_stars = len(temp.findAll('i', {'class': "fas fa-star-half-alt"}))
  48. rating_vendor = str(((stars - half_stars)/5) * 100)
  49. # Finding the Product Rating and Number of Product Reviews
  50. # reviews = temp[2].replace(" review)", "")
  51. # reviews = reviews.strip()
  52. #
  53. # temp = temp[1].split(")")
  54. # rating = temp[1].replace("Product Review : ", "")
  55. # rating = rating.replace("%", "")
  56. # rating_item = rating.strip()
  57. box2 = soup.find('div', {"class": "col-md-4 text-center"}).find('span', {"class": "text-left"}).findAll('span')
  58. # Finding Prices
  59. price = box2[0].text
  60. price = price.replace("$", "")
  61. price = price.replace('\n', '')
  62. price = price.strip()
  63. currency = box2[2].find('i').get('class')
  64. if 'bitcoin' in currency:
  65. BTC = price
  66. elif 'USD' in currency:
  67. USD = price
  68. elif 'monero' in currency:
  69. USD = (str(int(price) * 170.97))
  70. USD = box2[0].text
  71. USD = USD.replace('\n', '')
  72. USD = USD.replace('$', '')
  73. USD = USD.strip()
  74. # Finding Vendor Image
  75. vendor_image = soup.find('img', {"class": 'img-fluid'}).get('src')
  76. vendor_image = vendor_image.split('base64,')[-1]
  77. # Finding the Product Category
  78. # pmb = soup.findAll('p', {'class': "mb-1"})
  79. # category = pmb[-1].text
  80. # category = category.replace("Category: ", "").strip()
  81. # Finding the Product Quantity Available
  82. # left = divmb[-1].text
  83. # left = left.split(",", 1)[1]
  84. # left = left.replace("in stock", "")
  85. # left = left.strip()
  86. # Finding Number Sold
  87. # sold = divmb[-1].text
  88. # sold = sold.split(",", 1)[0]
  89. # sold = sold.replace("sold", "")
  90. # sold = sold.strip()
  91. # Finding Shipment Information (Origin)
  92. # pmb[0].text
  93. # shipFrom = shipFrom.replace("Ships from: ", "").strip()
  94. # Finding Shipment Information (Destination)
  95. # pmb[1].text
  96. # shipTo = shipTo.replace("Ships to: ", "").strip()
  97. # Finding the Product description
  98. cardbody = soup.find('div', {"class": 'row-md-12'}).find('div', {"class": 'col-md-4'}).find('textarea', {"class": 'disabled form-control form-control-sm w-100 bg-mgray text-white rounded-0 border-danger'})
  99. describe = cardbody.text.strip()
  100. # Finding Product Image
  101. image = soup.find('div', {"class": 'row-md-12'}).find('div', {"class": 'col-md-4 text-center'}).find('img')
  102. image = image.get('src')
  103. image = image.split('base64,')[-1]
  104. # Searching for CVE and MS categories
  105. cve = soup.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  106. if cve:
  107. CVE = " "
  108. for idx in cve:
  109. CVE += (idx)
  110. CVE += " "
  111. CVE = CVE.replace(',', ' ')
  112. CVE = CVE.replace('\n', '')
  113. ms = soup.findAll(text=re.compile('MS\d{2}-\d{3}'))
  114. if ms:
  115. MS = " "
  116. for im in ms:
  117. MS += (im)
  118. MS += " "
  119. MS = MS.replace(',', ' ')
  120. MS = MS.replace('\n', '')
  121. # Populating the final variable (this should be a list with all fields scraped)
  122. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  123. BTC, USD, EURO, sold, left, shipFrom, shipTo, image, vendor_image)
  124. # Sending the results
  125. return row
  126. # parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  127. # stores info it needs in different lists, these lists are returned after being organized
  128. # @param: soup object looking at html page of listing page
  129. # return: 'row' that contains a variety of lists that each hold info on the listing page
  130. def ares_listing_parser(soup):
  131. # Fields to be parsed
  132. nm = 0 # *Total_Products (Should be Integer)
  133. mktName = "Ares" # 0 *Marketplace_Name
  134. vendor = [] # 1 *Vendor y
  135. rating_vendor = [] # 2 Vendor_Rating
  136. success = [] # 3 Vendor_Successful_Transactions
  137. name = [] # 4 *Product_Name y
  138. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  139. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  140. category = [] # 7 Product_Category y
  141. describe = [] # 8 Product_Description
  142. views = [] # 9 Product_Number_Of_Views
  143. reviews = [] # 10 Product_Number_Of_Reviews
  144. rating_item = [] # 11 Product_Rating
  145. addDate = [] # 12 Product_AddDate
  146. BTC = [] # 13 Product_BTC_SellingPrice
  147. USD = [] # 14 Product_USD_SellingPrice y
  148. EURO = [] # 15 Product_EURO_SellingPrice
  149. sold = [] # 16 Product_QuantitySold
  150. qLeft = [] # 17 Product_QuantityLeft
  151. shipFrom = [] # 18 Product_ShippedFrom
  152. shipTo = [] # 19 Product_ShippedTo
  153. image = [] # 20 Product_Image
  154. image_vendor = [] # 21 Vendor_Image
  155. href = [] # 22 Product_Links
  156. listing = soup.find('div', {"class": 'card-body text-black text-left bg-dark'}).findAll('div', {"class": 'card mb-4 border-danger rounded-0'})
  157. # Populating the Number of Products
  158. nm = len(listing)
  159. for a in listing:
  160. # Adding the url to the list of urls
  161. link = a.find('a', {'class': "badge badge-danger w-100 text-white"}).get('href')
  162. link = cleanLink(link)
  163. href.append(link)
  164. # Finding the Product name
  165. product = a.find('div', {"class": 'marquee-parent'}).find('div', {"class": "marquee-child"}).text
  166. product = product.replace('\n', ' ')
  167. product = product.replace(",", "")
  168. product = product.replace("...", "")
  169. product = product.strip()
  170. name.append(product)
  171. # Finding Product Image
  172. product_image = a.find('img')
  173. product_image = product_image.get('src')
  174. product_image = product_image.split('base64,')[-1]
  175. image.append(product_image)
  176. # Finding Prices
  177. price = a.findAll('a', {"class": "text-white"})[-1].text
  178. price = price.replace("$","")
  179. price = price.strip()
  180. currency = a.find('div', {"class": 'card-header bg-mgray rounded-0'}).findAll('i')[1]
  181. if 'bitcoin' in currency.get('class'):
  182. BTC.append(price)
  183. elif 'USD' in currency.get('class'):
  184. USD.append(price)
  185. elif 'monero' in currency.get('class'):
  186. USD.append(str(int(price) * 170.97))
  187. # Finding the Vendor
  188. vendor_name = a.find('a', {"class": 'badge badge-dark w-100 text-white my-1'}).text
  189. vendor_name = vendor_name.replace(",", "")
  190. vendor_name = vendor_name.strip()
  191. vendor.append(vendor_name)
  192. image_vendor.append("-1")
  193. # Finding the Category
  194. # cat = lb[-1].find("span").text
  195. # cat = cat.replace("class:", "")
  196. # cat = cat.strip()
  197. # category.append(cat)
  198. # Finding Number of Views
  199. # num = span[0].text
  200. # num = num.replace("views:", "")
  201. # num = num.strip()
  202. # sold.append(num)
  203. # Finding Number Sold
  204. # num = span[2].text
  205. # num = num.replace("Sold:", "")
  206. # num = num.strip()
  207. # sold.append(num)
  208. # Finding Quantity Left
  209. # quant = span[1].text
  210. # quant = quant.replace("stock:", "")
  211. # quant = quant.strip()
  212. # qLeft.append(quant)
  213. # add shipping information
  214. # ship = lb[2].findAll('small')[1].findAll('span')[1].text.split("->")
  215. # shipFrom.append(ship[0].replace("Ship from ", "").strip())
  216. # shipTo.append(ship[1].replace("to ", "").strip())
  217. # Searching for CVE and MS categories
  218. cve = a.findAll(text=re.compile('CVE-\d{4}-\d{4}'))
  219. if not cve:
  220. cveValue = "-1"
  221. else:
  222. cee = " "
  223. for idx in cve:
  224. cee += (idx)
  225. cee += " "
  226. cee = cee.replace(',', ' ')
  227. cee = cee.replace('\n', '')
  228. cveValue = cee
  229. CVE.append(cveValue)
  230. ms = a.findAll(text=re.compile('MS\d{2}-\d{3}'))
  231. if not ms:
  232. MSValue = "-1"
  233. else:
  234. me = " "
  235. for im in ms:
  236. me += (im)
  237. me += " "
  238. me = me.replace(',', ' ')
  239. me = me.replace('\n', '')
  240. MSValue = me
  241. MS.append(MSValue)
  242. # Populate the final variable (this should be a list with all fields scraped)
  243. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  244. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href, image, image_vendor)
  245. # called by the crawler to get description links on a listing page
  246. # @param: beautifulsoup object that is using the correct html page (listing page)
  247. # return: list of description links from a listing page
  248. def ares_links_parser(soup):
  249. # Returning all links that should be visited by the Crawler
  250. href = []
  251. listing = soup.findAll('div', {"class": "col-md-4 my-md-0 my-2 col-12"})
  252. # for a in listing:
  253. # bae = a.find('a', {"class": "text-info"}, href=True)
  254. # link = bae['href']
  255. # href.append(link)
  256. for a in listing:
  257. bae = a.findAll('a', href=True)
  258. # Adding the url to the list of urls
  259. link = bae[0].get('href')
  260. href.append(link)
  261. return href