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.

442 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. time.sleep(0.01) # making sure the file is closed before returning soup object
  92. return soup
  93. except:
  94. try:
  95. html = open(filePath.strip('\n'))
  96. soup = BeautifulSoup(html, "html.parser")
  97. html.close()
  98. time.sleep(0.01) # making sure the file is closed before returning soup object
  99. return soup
  100. except:
  101. incrementError()
  102. print("There was a problem to read the file " + filePath)
  103. if createLog:
  104. logFile.write(
  105. str(nError) + ". There was a problem to read the file " + filePath + "\n" + traceback.format_exc() + "\n")
  106. return None
  107. def parse_listing(marketPlace, listingFile, soup, createLog, logFile):
  108. try:
  109. if marketPlace == "DarkFox":
  110. rw = darkfox_listing_parser(soup)
  111. elif marketPlace == "Tor2door":
  112. rw = tor2door_listing_parser(soup)
  113. elif marketPlace == "Apocalypse":
  114. rw = apocalypse_listing_parser(soup)
  115. elif marketPlace == "ThiefWorld":
  116. rw = thiefWorld_listing_parser(soup)
  117. elif marketPlace == "AnonymousMarketplace":
  118. rw = anonymousMarketplace_listing_parser(soup)
  119. elif marketPlace == "ViceCity":
  120. rw = vicecity_listing_parser(soup)
  121. elif marketPlace == "TorBay":
  122. rw = torbay_listing_parser(soup)
  123. elif marketPlace == "M00nkeyMarket":
  124. rw = m00nkey_listing_parser(soup)
  125. elif marketPlace == "HiddenMarket":
  126. rw = hiddenmarket_listing_parser(soup)
  127. elif marketPlace == "DarkMatter":
  128. rw = darkmatter_listing_parser(soup)
  129. elif marketPlace == "DigitalThriftShop":
  130. rw = digitalThriftShop_listing_parser(soup)
  131. elif marketPlace == "LionMarketplace":
  132. rw = lionmarketplace_listing_parser(soup)
  133. elif marketPlace == "TorMarket":
  134. rw = tormarket_listing_parser(soup)
  135. elif marketPlace == "RobinhoodMarket":
  136. rw = Robinhood_listing_parser(soup)
  137. elif marketPlace == "Nexus":
  138. rw = nexus_listing_parser(soup)
  139. elif marketPlace == "MikesGrandStore":
  140. rw = mikesGrandStore_listing_parser(soup)
  141. elif marketPlace == "DarkBazar":
  142. rw = darkbazar_listing_parser(soup)
  143. elif marketPlace == "PabloEscobarMarket":
  144. rw = pabloescobarmarket_listing_parser(soup)
  145. elif marketPlace == "AnonMarket":
  146. rw = AnonMarket_listing_parser(soup)
  147. elif marketPlace == "CityMarket":
  148. rw = city_listing_parser(soup)
  149. elif marketPlace == "MetaVerseMarket":
  150. rw = metaversemarket_listing_parser(soup)
  151. else:
  152. print("MISSING CALL TO LISTING PARSER IN PREPARE_PARSER.PY!")
  153. raise Exception
  154. return rw
  155. except:
  156. incrementError()
  157. print("There was a problem to parse the file " + listingFile + " in the listing section!")
  158. traceback.print_exc()
  159. if createLog:
  160. logFile.write(
  161. str(nError) + ". There was a problem to parse the file " + listingFile + " in the Listing section.\n"
  162. + traceback.format_exc() + "\n")
  163. return None
  164. def parse_description(marketPlace, descriptionFile, soup, createLog, logFile):
  165. try:
  166. if marketPlace == "DarkFox":
  167. rmm = darkfox_description_parser(soup)
  168. elif marketPlace == "Tor2door":
  169. rmm = tor2door_description_parser(soup)
  170. elif marketPlace == "Apocalypse":
  171. rmm = apocalypse_description_parser(soup)
  172. elif marketPlace == "ThiefWorld":
  173. rmm = thiefWorld_description_parser(soup)
  174. elif marketPlace == "AnonymousMarketplace":
  175. rmm = anonymousMarketplace_description_parser(soup)
  176. elif marketPlace == "ViceCity":
  177. rmm = vicecity_description_parser(soup)
  178. elif marketPlace == "TorBay":
  179. rmm = torbay_description_parser(soup)
  180. elif marketPlace == "M00nkeyMarket":
  181. rmm = m00nkey_description_parser(soup)
  182. elif marketPlace == "HiddenMarket":
  183. rmm = hiddenmarket_description_parser(soup)
  184. elif marketPlace == "DarkMatter":
  185. rmm = darkmatter_description_parser(soup)
  186. elif marketPlace == "DigitalThriftShop":
  187. rmm = digitalThriftShop_description_parser(soup)
  188. elif marketPlace == "LionMarketplace":
  189. rmm = lionmarketplace_description_parser(soup)
  190. elif marketPlace == "TorMarket":
  191. rmm = tormarket_description_parser(soup)
  192. elif marketPlace == "RobinhoodMarket":
  193. rmm = Robinhood_description_parser(soup)
  194. elif marketPlace == "Nexus":
  195. rmm = nexus_description_parser(soup)
  196. elif marketPlace == "MikesGrandStore":
  197. rmm = mikesGrandStore_description_parser(soup)
  198. elif marketPlace == "DarkBazar":
  199. rmm = darkbazar_description_parser(soup)
  200. elif marketPlace == "PabloEscobarMarket":
  201. rmm = pabloescobarmarket_description_parser(soup)
  202. elif marketPlace == "AnonMarket":
  203. rmm = AnonMarket_description_parser(soup)
  204. elif marketPlace == "CityMarket":
  205. rmm = city_description_parser(soup)
  206. elif marketPlace == "MetaVerseMarket":
  207. rmm = metaversemarket_description_parser(soup)
  208. else:
  209. print("MISSING CALL TO DESCRIPTION PARSER IN PREPARE_PARSER.PY!")
  210. raise Exception
  211. return rmm
  212. except:
  213. incrementError()
  214. print("There was a problem to parse the file " + descriptionFile + " in the Description section!")
  215. traceback.print_exc()
  216. if createLog:
  217. logFile.write(
  218. str(nError) + ". There was a problem to parse the file " + descriptionFile + " in the Description section.\n"
  219. + traceback.format_exc() + "\n")
  220. return None
  221. def persist_record(url, rec, cur, con, createLog, logFile, listingFile, descriptionFile):
  222. try:
  223. persist_data(url, tuple(rec), cur)
  224. con.commit()
  225. return True
  226. except:
  227. con.rollback()
  228. incrementError()
  229. print(f"There was a problem to persist the files ({listingFile} + {descriptionFile}) in the database!")
  230. traceback.print_exc()
  231. if createLog:
  232. logFile.write(
  233. str(nError) + f". There was a problem to persist the files ({listingFile} + {descriptionFile}) in the database!\n"
  234. + traceback.format_exc() + "\n")
  235. return False
  236. def move_file(filePath, createLog, logFile):
  237. source = filePath
  238. destination = filePath.replace(os.path.basename(filePath), "") + 'Read\\' + os.path.basename(filePath)
  239. try:
  240. shutil.move(source, destination, shutil.copy2)
  241. return True
  242. except:
  243. try:
  244. shutil.move(source, destination, shutil.copytree)
  245. return True
  246. except:
  247. incrementError()
  248. print("There was a problem to move the file " + filePath)
  249. traceback.print_exc()
  250. if createLog:
  251. logFile.write(
  252. str(nError) + ". There was a problem to move the file " + filePath + "\n" + traceback.format_exc() + "\n")
  253. return False
  254. def new_parse(marketPlace, url, createLog):
  255. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  256. global nError
  257. nError = 0
  258. print("Parsing the " + marketPlace + " market and conduct data classification to store the information in the database.")
  259. # Connecting to the database
  260. con = connectDataBase()
  261. cur = con.cursor(cursor_factory=RealDictCursor)
  262. # Creating the tables (The database should be created manually)
  263. create_database(cur, con)
  264. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces\\" + marketPlace + "\\HTML_Pages")
  265. # Creating the log file for each Forum
  266. if createLog:
  267. try:
  268. logFile = open(mainDir + f"/{CURRENT_DATE}/" + marketPlace + "_" + CURRENT_DATE + ".log", "w")
  269. except:
  270. print("Could not open log file!")
  271. createLog = False
  272. logFile = None
  273. # raise SystemExit
  274. else:
  275. logFile = None
  276. # Reading the Listing Html Pages
  277. listings = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Listing", '*.html'))
  278. for listingIndex, listingFile in enumerate(listings):
  279. print("Reading listing folder of '" + marketPlace + "', file '" + os.path.basename(listingFile) + "', index= " + str(
  280. listingIndex + 1) + " ... " + str(len(listings)))
  281. listingSoup = read_file(listingFile, createLog, logFile)
  282. # listing flags
  283. doParseListing = listingSoup is not None
  284. doDescription = False
  285. readDescriptionError = False
  286. parseDescriptionError = False
  287. persistDescriptionError = False
  288. moveDescriptionError = False
  289. findDescriptionError = False
  290. rw = []
  291. if doParseListing:
  292. rw = parse_listing(marketPlace, listingFile, listingSoup, createLog, logFile)
  293. doDescription = rw is not None
  294. if doDescription:
  295. nFound = 0
  296. for rec in rw:
  297. rec = rec.split(',')
  298. descriptionPattern = cleanLink(rec[22]) + ".html"
  299. # Reading the associated description Html Pages
  300. descriptions = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Description", descriptionPattern))
  301. nFound += len(descriptions)
  302. for descriptionIndex, descriptionFile in enumerate(descriptions):
  303. print("Reading description folder of '" + marketPlace + "', file '" + os.path.basename(
  304. descriptionFile) + "', index= " + str(descriptionIndex + 1) + " ... " + str(len(descriptions)))
  305. descriptionSoup = read_file(descriptionFile, createLog, logFile)
  306. # description flags
  307. doParseDescription = descriptionSoup is not None
  308. doPersistRecord = False
  309. doMoveDescription = False
  310. rmm = []
  311. if doParseDescription:
  312. rmm = parse_description(marketPlace, descriptionFile, descriptionSoup, createLog, logFile)
  313. doPersistRecord = rmm is not None
  314. else:
  315. readDescriptionError = True
  316. parseDescriptionError = True
  317. if doPersistRecord:
  318. # Combining the information from Listing and Description Pages
  319. rec = mergePages(rmm, rec)
  320. # Append to the list the classification of the topic
  321. rec.append(str(predict(rec[4], rec[5], language='sup_english')))
  322. # Persisting the information in the database
  323. persistSuccess = persist_record(url, rec, cur, con, createLog, logFile, listingFile,
  324. descriptionFile)
  325. doMoveDescription = persistSuccess
  326. else:
  327. parseDescriptionError = True
  328. if doMoveDescription:
  329. # move description files of completed folder
  330. moveSuccess = move_file(descriptionFile, createLog, logFile)
  331. if not moveSuccess:
  332. moveDescriptionError = True
  333. else:
  334. moveDescriptionError = True
  335. if not (nFound > 0):
  336. findDescriptionError = True
  337. incrementError()
  338. print(f"There was a problem to locate the file(s) for {listingFile} in the Description section!")
  339. if createLog:
  340. logFile.write(
  341. str(nError) + f". There was a problem to locate the file(s) for {listingFile}"
  342. f" in the Description section!\n\n")
  343. if not (readDescriptionError or parseDescriptionError or persistDescriptionError
  344. or moveDescriptionError or findDescriptionError):
  345. # move listing files of completed folder
  346. move_file(listingFile, createLog, logFile)
  347. if createLog:
  348. logFile.close()
  349. cur.close()
  350. con.close()
  351. print("Parsing the " + marketPlace + " market and data classification done.")