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.

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