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.

418 lines
15 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. __author__ = 'DarkWeb'
  2. import glob
  3. import os
  4. import codecs
  5. import shutil
  6. from MarketPlaces.DB_Connection.db_connection import *
  7. from MarketPlaces.DarkFox.parser import *
  8. from MarketPlaces.Tor2door.parser import *
  9. from MarketPlaces.Apocalypse.parser import *
  10. from MarketPlaces.ThiefWorld.parser import *
  11. from MarketPlaces.AnonymousMarketplace.parser import *
  12. from MarketPlaces.ViceCity.parser import *
  13. from MarketPlaces.TorBay.parser import *
  14. from MarketPlaces.M00nkeyMarket.parser import *
  15. from MarketPlaces.DarkMatter.parser import *
  16. from MarketPlaces.DigitalThriftShop.parser import *
  17. from MarketPlaces.LionMarketplace.parser import *
  18. from MarketPlaces.TorMarket.parser import *
  19. from MarketPlaces.HiddenMarket.parser import *
  20. from MarketPlaces.RobinhoodMarket.parser import *
  21. from MarketPlaces.Nexus.parser import *
  22. from MarketPlaces.MikesGrandStore.parser import *
  23. from MarketPlaces.DarkBazar.parser import *
  24. from MarketPlaces.PabloEscobarMarket.parser import *
  25. from MarketPlaces.AnonMarket.parser import *
  26. from MarketPlaces.Classifier.classify_product import predict
  27. nError = 0
  28. def mergePages(rmm, rec):
  29. # key = u"Pr:" + rec[1].upper() + u" Vendor:" + rec[18].upper()
  30. # key = rec[23]
  31. print("----------------- Matched: " + rec[4] + "--------------------")
  32. if rec[1] == "-1": # name_vendor
  33. rec[1] = rmm[0]
  34. if rec[2] == "-1": # rating_vendor
  35. rec[2] = rmm[1]
  36. if rec[3] == "-1": # success_vendor
  37. rec[3] = rmm[2]
  38. if rec[4] == "-1": # name_item
  39. rec[4] = rmm[3]
  40. if rec[5] == "-1": # description_item
  41. rec[5] = rmm[4]
  42. if rec[6] == "-1": # cve_item
  43. rec[6] = rmm[5]
  44. if rec[7] == "-1": # ms_item
  45. rec[7] = rmm[6]
  46. if rec[8] == "-1": # category_item
  47. rec[8] = rmm[7]
  48. if rec[9] == "-1": # views_item
  49. rec[9] = rmm[8]
  50. if rec[10] == "-1": # reviews_item
  51. rec[10] = rmm[9]
  52. if rec[11] == "-1": # rating_item
  53. rec[11] = rmm[10]
  54. if rec[12] == "-1": # adddate_item
  55. rec[12] = rmm[11]
  56. if rec[13] == "-1": # btc_item
  57. rec[13] = rmm[12]
  58. if rec[14] == "-1": # usd_item
  59. rec[14] = rmm[13]
  60. if rec[15] == "-1": # euro_item
  61. rec[15] = rmm[14]
  62. if rec[16] == "-1": # quantitysold_item
  63. rec[16] = rmm[15]
  64. if rec[17] == "-1": # quantityleft_item
  65. rec[17] = rmm[16]
  66. if rec[18] == "-1": # shippedfrom_item
  67. rec[18] = rmm[17]
  68. if rec[19] == "-1": # shippedto_item
  69. rec[19] = rmm[18]
  70. if rmm[19] != "-1": # image
  71. rec[20] = rmm[19]
  72. if rmm[20] != "-1": # image_vendor
  73. rec[21] = rmm[20]
  74. return rec
  75. def persist_data(url, row, cur):
  76. marketPlace = create_marketPlace(cur, row, url)
  77. vendor = create_vendor(cur, row, marketPlace)
  78. create_items(cur, row, marketPlace, vendor)
  79. def incrementError():
  80. global nError
  81. nError += 1
  82. def read_file(filePath, createLog, logFile):
  83. try:
  84. html = codecs.open(filePath.strip('\n'), encoding='utf8')
  85. soup = BeautifulSoup(html, "html.parser")
  86. html.close()
  87. return soup
  88. except:
  89. try:
  90. html = open(filePath.strip('\n'))
  91. soup = BeautifulSoup(html, "html.parser")
  92. html.close()
  93. return soup
  94. except:
  95. incrementError()
  96. print("There was a problem to read the file " + filePath)
  97. if createLog:
  98. logFile.write(
  99. str(nError) + ". There was a problem to read the file " + filePath + "\n")
  100. return None
  101. def parse_listing(marketPlace, listingFile, soup, createLog, logFile):
  102. try:
  103. if marketPlace == "DarkFox":
  104. rw = darkfox_listing_parser(soup)
  105. elif marketPlace == "Tor2door":
  106. rw = tor2door_listing_parser(soup)
  107. elif marketPlace == "Apocalypse":
  108. rw = apocalypse_listing_parser(soup)
  109. elif marketPlace == "ThiefWorld":
  110. rw = thiefWorld_listing_parser(soup)
  111. elif marketPlace == "AnonymousMarketplace":
  112. rw = anonymousMarketplace_listing_parser(soup)
  113. elif marketPlace == "ViceCity":
  114. rw = vicecity_listing_parser(soup)
  115. elif marketPlace == "TorBay":
  116. rw = torbay_listing_parser(soup)
  117. elif marketPlace == "M00nkeyMarket":
  118. rw = m00nkey_listing_parser(soup)
  119. elif marketPlace == "HiddenMarket":
  120. rw = hiddenmarket_listing_parser(soup)
  121. elif marketPlace == "DarkMatter":
  122. rw = darkmatter_listing_parser(soup)
  123. elif marketPlace == "DigitalThriftShop":
  124. rw = digitalThriftShop_listing_parser(soup)
  125. elif marketPlace == "LionMarketplace":
  126. rw = lionmarketplace_listing_parser(soup)
  127. elif marketPlace == "TorMarket":
  128. rw = tormarket_listing_parser(soup)
  129. elif marketPlace == "RobinhoodMarket":
  130. rw = Robinhood_listing_parser(soup)
  131. elif marketPlace == "Nexus":
  132. rw = nexus_listing_parser(soup)
  133. elif marketPlace == "MikesGrandStore":
  134. rw = mikesGrandStore_listing_parser(soup)
  135. elif marketPlace == "DarkBazar":
  136. rw = darkbazar_listing_parser(soup)
  137. elif marketPlace == "PabloEscobarMarket":
  138. rw = pabloescobarmarket_listing_parser(soup)
  139. elif marketPlace == "AnonMarket":
  140. rw = AnonMarket_listing_parser(soup)
  141. else:
  142. print("MISSING CALL TO LISTING PARSER IN PREPARE_PARSER.PY!")
  143. raise Exception
  144. return rw
  145. except:
  146. incrementError()
  147. print("There was a problem to parse the file " + listingFile + " in the listing section!")
  148. traceback.print_exc()
  149. if createLog:
  150. logFile.write(
  151. str(nError) + ". There was a problem to parse the file " + listingFile + " in the Listing section.\n")
  152. return None
  153. def parse_description(marketPlace, descriptionFile, soup, createLog, logFile):
  154. try:
  155. if marketPlace == "DarkFox":
  156. rmm = darkfox_description_parser(soup)
  157. elif marketPlace == "Tor2door":
  158. rmm = tor2door_description_parser(soup)
  159. elif marketPlace == "Apocalypse":
  160. rmm = apocalypse_description_parser(soup)
  161. elif marketPlace == "ThiefWorld":
  162. rmm = thiefWorld_description_parser(soup)
  163. elif marketPlace == "AnonymousMarketplace":
  164. rmm = anonymousMarketplace_description_parser(soup)
  165. elif marketPlace == "ViceCity":
  166. rmm = vicecity_description_parser(soup)
  167. elif marketPlace == "TorBay":
  168. rmm = torbay_description_parser(soup)
  169. elif marketPlace == "M00nkeyMarket":
  170. rmm = m00nkey_description_parser(soup)
  171. elif marketPlace == "HiddenMarket":
  172. rmm = hiddenmarket_description_parser(soup)
  173. elif marketPlace == "DarkMatter":
  174. rmm = darkmatter_description_parser(soup)
  175. elif marketPlace == "DigitalThriftShop":
  176. rmm = digitalThriftShop_description_parser(soup)
  177. elif marketPlace == "LionMarketplace":
  178. rmm = lionmarketplace_description_parser(soup)
  179. elif marketPlace == "TorMarket":
  180. rmm = tormarket_description_parser(soup)
  181. elif marketPlace == "RobinhoodMarket":
  182. rmm = Robinhood_description_parser(soup)
  183. elif marketPlace == "Nexus":
  184. rmm = nexus_description_parser(soup)
  185. elif marketPlace == "MikesGrandStore":
  186. rmm = mikesGrandStore_description_parser(soup)
  187. elif marketPlace == "DarkBazar":
  188. rmm = darkbazar_description_parser(soup)
  189. elif marketPlace == "PabloEscobarMarket":
  190. rmm = pabloescobarmarket_description_parser(soup)
  191. elif marketPlace == "AnonMarket":
  192. rmm = AnonMarket_description_parser(soup)
  193. else:
  194. print("MISSING CALL TO DESCRIPTION PARSER IN PREPARE_PARSER.PY!")
  195. raise Exception
  196. return rmm
  197. except:
  198. incrementError()
  199. print("There was a problem to parse the file " + descriptionFile + " in the Description section!")
  200. traceback.print_exc()
  201. if createLog:
  202. logFile.write(
  203. str(nError) + ". There was a problem to parse the file " + descriptionFile + " in the Description section.\n")
  204. return None
  205. def persist_record(url, rec, cur, con, createLog, logFile, listingFile, descriptionFile):
  206. try:
  207. persist_data(url, tuple(rec), cur)
  208. con.commit()
  209. return True
  210. except:
  211. con.rollback()
  212. trace = traceback.format_exc()
  213. if trace.find("already exists") == -1:
  214. incrementError()
  215. print(f"There was a problem to persist the files ({listingFile} + {descriptionFile}) in the database!")
  216. traceback.print_exc()
  217. if createLog:
  218. logFile.write(
  219. str(nError) + f". There was a problem to persist the files ({listingFile} + {descriptionFile}) in the database!\n")
  220. return False
  221. else:
  222. return True
  223. def move_file(filePath, createLog, logFile):
  224. # source = line2.replace(os.path.basename(line2), "") + filename
  225. source = filePath
  226. destination = filePath.replace(os.path.basename(filePath), "") + r'Read/'
  227. try:
  228. shutil.move(source, destination)
  229. return True
  230. except:
  231. print("There was a problem to move the file " + filePath)
  232. incrementError()
  233. if createLog:
  234. logFile.write(
  235. str(nError) + ". There was a problem to move the file " + filePath + "\n")
  236. return False
  237. def new_parse(marketPlace, url, createLog):
  238. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  239. print("Parsing the " + marketPlace + " market and conduct data classification to store the information in the database.")
  240. # Connecting to the database
  241. con = connectDataBase()
  242. cur = con.cursor()
  243. # Creating the tables (The database should be created manually)
  244. create_database(cur, con)
  245. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces/" + marketPlace + "/HTML_Pages")
  246. # Creating the log file for each Forum
  247. if createLog:
  248. try:
  249. logFile = open(mainDir + f"/{CURRENT_DATE}/" + marketPlace + "_" + CURRENT_DATE + ".log", "w")
  250. except:
  251. print("Could not open log file!")
  252. createLog = False
  253. logFile = None
  254. # raise SystemExit
  255. else:
  256. logFile = None
  257. # Reading the Listing Html Pages
  258. listings = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Listing", '*.html'))
  259. for listingIndex, listingFile in enumerate(listings):
  260. print("Reading listing folder of '" + marketPlace + "', file '" + os.path.basename(listingFile) + "', index= " + str(
  261. listingIndex + 1) + " ... " + str(len(listings)))
  262. listingSoup = read_file(listingFile, createLog, logFile)
  263. # listing flags
  264. doParseListing = listingSoup is not None
  265. doDescription = False
  266. readDescriptionError = False
  267. parseDescriptionError = False
  268. persistDescriptionError = False
  269. moveDescriptionError = False
  270. findDescriptionError = False
  271. rw = []
  272. if doParseListing:
  273. rw = parse_listing(marketPlace, listingFile, listingSoup, createLog, logFile)
  274. doDescription = rw is not None
  275. if doDescription:
  276. nFound = 0
  277. for rec in rw:
  278. rec = rec.split(',')
  279. descriptionPattern = cleanLink(rec[22]) + ".html"
  280. # Reading the associated description Html Pages
  281. descriptions = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Description", descriptionPattern))
  282. nFound += len(descriptions)
  283. for descriptionIndex, descriptionFile in enumerate(descriptions):
  284. print("Reading description folder of '" + marketPlace + "', file '" + os.path.basename(
  285. descriptionFile) + "', index= " + str(descriptionIndex + 1) + " ... " + str(len(descriptions)))
  286. descriptionSoup = read_file(descriptionFile, createLog, logFile)
  287. # description flags
  288. doParseDescription = descriptionSoup is not None
  289. doPersistRecord = False
  290. doMoveDescription = False
  291. rmm = []
  292. if doParseDescription:
  293. rmm = parse_description(marketPlace, descriptionFile, descriptionSoup, createLog, logFile)
  294. doPersistRecord = rmm is not None
  295. else:
  296. readDescriptionError = True
  297. parseDescriptionError = True
  298. if doPersistRecord:
  299. # Combining the information from Listing and Description Pages
  300. rec = mergePages(rmm, rec)
  301. # Append to the list the classification of the topic
  302. rec.append(str(predict(rec[4], rec[5], language='sup_english')))
  303. # Persisting the information in the database
  304. persistSuccess = persist_record(url, rec, cur, con, createLog, logFile, listingFile,
  305. descriptionFile)
  306. doMoveDescription = persistSuccess
  307. else:
  308. parseDescriptionError = True
  309. if doMoveDescription:
  310. # move description files of completed folder
  311. moveSuccess = move_file(descriptionFile, createLog, logFile)
  312. if not moveSuccess:
  313. moveDescriptionError = True
  314. else:
  315. moveDescriptionError = True
  316. if not (nFound > 0):
  317. findDescriptionError = True
  318. incrementError()
  319. print(f"There was a problem to locate the file(s) for {listingFile} in the Description section!")
  320. if createLog:
  321. logFile.write(
  322. str(nError) + f". There was a problem to locate the file(s) for {listingFile}"
  323. f" in the Description section!\n")
  324. if not (readDescriptionError or parseDescriptionError or persistDescriptionError
  325. or moveDescriptionError or findDescriptionError):
  326. # move listing files of completed folder
  327. move_file(listingFile, createLog, logFile)
  328. if createLog:
  329. logFile.close()
  330. print("Parsing the " + marketPlace + " market and data classification done.")