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.

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