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.

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