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.

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