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.

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