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.

491 lines
17 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
6 months ago
6 months ago
1 year ago
6 months 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.AnonymousMarketplace.parser import *
  11. from MarketPlaces.TheDarkMarket.parser import *
  12. from MarketPlaces.ViceCity.parser import *
  13. from MarketPlaces.M00nkeyMarket.parser import *
  14. from MarketPlaces.MikesGrandStore.parser import *
  15. from MarketPlaces.PabloEscobarMarket.parser import *
  16. from MarketPlaces.CityMarket.parser import *
  17. from MarketPlaces.DarkBazar.parser import *
  18. from MarketPlaces.Sonanza.parser import *
  19. from MarketPlaces.Kingdom.parser import *
  20. from MarketPlaces.BlackPyramid.parser import *
  21. from MarketPlaces.Quest.parser import *
  22. from MarketPlaces.Ares.parser import *
  23. from MarketPlaces.CypherMarketplace.parser import *
  24. from MarketPlaces.WeTheNorth.parser import *
  25. from MarketPlaces.Torzon.parser import *
  26. from MarketPlaces.GoFish.parser import *
  27. from MarketPlaces.ZeroDay.parser import *
  28. from MarketPlaces.DarkMarket.parser import *
  29. from MarketPlaces.DarkDock.parser import *
  30. from MarketPlaces.SilkRoad4.parser import *
  31. from MarketPlaces.DarkRoad.parser import *
  32. from MarketPlaces.Classifier.classify_product import predict
  33. from Translator.translate import translate
  34. nError = 0
  35. def mergePages(rmm, rec):
  36. # key = u"Pr:" + rec[1].upper() + u" Vendor:" + rec[18].upper()
  37. # key = rec[23]
  38. print("----------------- Matched: " + rec[4] + "--------------------")
  39. if rec[1] == "-1": # name_vendor
  40. rec[1] = rmm[0]
  41. if rec[2] == "-1": # rating_vendor
  42. rec[2] = rmm[1]
  43. if rec[3] == "-1": # success_vendor
  44. rec[3] = rmm[2]
  45. if rec[4] == "-1": # name_item
  46. rec[4] = rmm[3]
  47. if rec[5] == "-1": # description_item
  48. rec[5] = rmm[4]
  49. if rec[6] == "-1": # cve_item
  50. rec[6] = rmm[5]
  51. if rec[7] == "-1": # ms_item
  52. rec[7] = rmm[6]
  53. if rec[8] == "-1": # category_item
  54. rec[8] = rmm[7]
  55. if rec[9] == "-1": # views_item
  56. rec[9] = rmm[8]
  57. if rec[10] == "-1": # reviews_item
  58. rec[10] = rmm[9]
  59. if rec[11] == "-1": # rating_item
  60. rec[11] = rmm[10]
  61. if rec[12] == "-1": # adddate_item
  62. rec[12] = rmm[11]
  63. if rec[13] == "-1": # btc_item
  64. rec[13] = rmm[12]
  65. if rec[14] == "-1": # usd_item
  66. rec[14] = rmm[13]
  67. if rec[15] == "-1": # euro_item
  68. rec[15] = rmm[14]
  69. if rec[16] == "-1": # quantitysold_item
  70. rec[16] = rmm[15]
  71. if rec[17] == "-1": # quantityleft_item
  72. rec[17] = rmm[16]
  73. if rec[18] == "-1": # shippedfrom_item
  74. rec[18] = rmm[17]
  75. if rec[19] == "-1": # shippedto_item
  76. rec[19] = rmm[18]
  77. if rmm[19] != "-1": # image
  78. rec[20] = rmm[19]
  79. if rmm[20] != "-1": # image_vendor
  80. rec[21] = rmm[20]
  81. return rec
  82. def persist_data(url, row, cur):
  83. marketPlace = create_marketPlace(cur, row, url)
  84. vendor = create_vendor(cur, row, marketPlace)
  85. create_items(cur, row, marketPlace, vendor)
  86. def incrementError():
  87. global nError
  88. nError += 1
  89. def read_file(filePath, createLog, logFile):
  90. try:
  91. html = codecs.open(filePath.strip('\n'), encoding='utf8')
  92. soup = BeautifulSoup(html, "html.parser")
  93. html.close()
  94. time.sleep(0.01) # making sure the file is closed before returning soup object
  95. return soup
  96. except:
  97. try:
  98. html = open(filePath.strip('\n'))
  99. soup = BeautifulSoup(html, "html.parser")
  100. html.close()
  101. time.sleep(0.01) # making sure the file is closed before returning soup object
  102. return soup
  103. except:
  104. incrementError()
  105. print("There was a problem to read the file " + filePath)
  106. if createLog:
  107. logFile.write(
  108. str(nError) + ". There was a problem to read the file " + filePath + "\n" + traceback.format_exc() + "\n")
  109. return None
  110. def parse_listing(marketPlace, listingFile, soup, createLog, logFile):
  111. try:
  112. if marketPlace == "DarkFox":
  113. rw = darkfox_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 == "M00nkeyMarket":
  119. rw = m00nkey_listing_parser(soup)
  120. elif marketPlace == "MikesGrandStore":
  121. rw = MikesGrandStore_listing_parser(soup)
  122. elif marketPlace == "PabloEscobarMarket":
  123. rw = pabloescobarmarket_listing_parser(soup)
  124. elif marketPlace == "CityMarket":
  125. rw = city_listing_parser(soup)
  126. elif marketPlace == "Ares":
  127. rw = ares_listing_parser(soup)
  128. elif marketPlace == "DarkBazar":
  129. rw = darkbazar_listing_parser(soup)
  130. elif marketPlace == "Sonanza":
  131. rw = sonanza_listing_parser(soup)
  132. elif marketPlace == "Kingdom":
  133. rw = kingdom_listing_parser(soup)
  134. elif marketPlace == "BlackPyramid":
  135. rw = blackpyramid_listing_parser(soup)
  136. elif marketPlace == "Quest":
  137. rw = quest_listing_parser(soup)
  138. elif marketPlace == "CypherMarketplace":
  139. rw = cyphermarketplace_listing_parser(soup)
  140. elif marketPlace == "TheDarkMarket":
  141. rw = thedarkmarket_listing_parser(soup)
  142. elif marketPlace == "WeTheNorth":
  143. rw = wethenorth_listing_parser(soup)
  144. elif marketPlace == "GoFish":
  145. rw = gofish_listing_parser(soup)
  146. elif marketPlace == "ZeroDay":
  147. rw = zeroday_listing_parser(soup)
  148. elif marketPlace == "Torzon":
  149. rw = torzon_listing_parser(soup)
  150. elif marketPlace == "DarkMarket":
  151. rw = darkmarket_listing_parser(soup)
  152. elif marketPlace == "DarkDock":
  153. rw = darkdock_listing_parser(soup)
  154. elif marketPlace == "SilkRoad4":
  155. rw = silkroad4_listing_parser(soup)
  156. elif marketplace == "DarkRoad":
  157. rw = darkroad_listing_parser(soup)
  158. else:
  159. print("MISSING CALL TO LISTING PARSER IN PREPARE_PARSER.PY!")
  160. raise Exception
  161. return rw
  162. except:
  163. incrementError()
  164. print("There was a problem to parse the file " + listingFile + " in the listing section!")
  165. traceback.print_exc()
  166. if createLog:
  167. logFile.write(
  168. str(nError) + ". There was a problem to parse the file " + listingFile + " in the Listing section.\n"
  169. + traceback.format_exc() + "\n")
  170. return None
  171. def parse_description(marketPlace, descriptionFile, soup, createLog, logFile):
  172. try:
  173. if marketPlace == "DarkFox":
  174. rmm = darkfox_description_parser(soup)
  175. elif marketPlace == "AnonymousMarketplace":
  176. rmm = anonymousMarketplace_description_parser(soup)
  177. elif marketPlace == "ViceCity":
  178. rmm = vicecity_description_parser(soup)
  179. elif marketPlace == "M00nkeyMarket":
  180. rmm = m00nkey_description_parser(soup)
  181. elif marketPlace == "MikesGrandStore":
  182. rmm = MikesGrandStore_description_parser(soup)
  183. elif marketPlace == "PabloEscobarMarket":
  184. rmm = pabloescobarmarket_description_parser(soup)
  185. elif marketPlace == "CityMarket":
  186. rmm = city_description_parser(soup)
  187. elif marketPlace == "Ares":
  188. rmm = ares_description_parser(soup)
  189. elif marketPlace == "DarkBazar":
  190. rmm = darkbazar_description_parser(soup)
  191. elif marketPlace == "Sonanza":
  192. rmm = sonanza_description_parser(soup)
  193. elif marketPlace == "Kingdom":
  194. rmm = kingdom_description_parser(soup)
  195. elif marketPlace == "BlackPyramid":
  196. rmm = blackpyramid_description_parser(soup)
  197. elif marketPlace == "Quest":
  198. rmm = quest_description_parser(soup)
  199. elif marketPlace == "CypherMarketplace":
  200. rmm = cyphermarketplace_description_parser(soup)
  201. elif marketPlace == "TheDarkMarket":
  202. rmm = thedarkmarket_description_parser(soup)
  203. elif marketPlace == "WeTheNorth":
  204. rmm = wethenorth_description_parser(soup)
  205. elif marketPlace == "GoFish":
  206. rmm = gofish_description_parser(soup)
  207. elif marketPlace == "ZeroDay":
  208. rmm = zeroday_description_parser(soup)
  209. elif marketPlace == "Torzon":
  210. rmm = torzon_description_parser(soup)
  211. elif marketPlace == "DarkMarket":
  212. rmm = darkmarket_description_parser(soup)
  213. elif marketPlace == "DarkDock":
  214. rmm = darkdock_description_parser(soup)
  215. elif marketPlace == "SilkRoad4":
  216. rmm = silkroad4_description_parser(soup)
  217. elif marketPlace == "DarkRoad":
  218. rmm = darkroad_description_parser(soup)
  219. else:
  220. print("MISSING CALL TO DESCRIPTION PARSER IN PREPARE_PARSER.PY!")
  221. raise Exception
  222. return rmm
  223. except:
  224. incrementError()
  225. print("There was a problem to parse the file " + descriptionFile + " in the Description section!")
  226. traceback.print_exc()
  227. if createLog:
  228. logFile.write(
  229. str(nError) + ". There was a problem to parse the file " + descriptionFile + " in the Description section.\n"
  230. + traceback.format_exc() + "\n")
  231. return None
  232. def get_source_language(marketPlace):
  233. if marketPlace == "BestCardingWorld":
  234. lang = 'english'
  235. elif marketPlace == "CryptBB":
  236. lang = 'english'
  237. elif marketPlace == "Incogsnoo":
  238. lang = 'english'
  239. elif marketPlace == "CityMarket":
  240. lang = 'english'
  241. elif marketPlace == "DarkMarket":
  242. lang = 'english'
  243. else:
  244. print("MISSING CALL TO GET LANGUAGE IN PREPARE_PARSER.PY!")
  245. lang = 'auto'
  246. return lang
  247. def persist_record(url, rec, cur, con, createLog, logFile, listingFile, descriptionFile):
  248. try:
  249. persist_data(url, tuple(rec), cur)
  250. con.commit()
  251. return True
  252. except:
  253. con.rollback()
  254. incrementError()
  255. print(f"There was a problem to persist the files ({listingFile} + {descriptionFile}) in the database!")
  256. traceback.print_exc()
  257. if createLog:
  258. logFile.write(
  259. str(nError) + f". There was a problem to persist the files ({listingFile} + {descriptionFile}) in the database!\n"
  260. + traceback.format_exc() + "\n")
  261. return False
  262. def move_file(filePath, createLog, logFile):
  263. source = filePath
  264. destination = filePath.replace(os.path.basename(filePath), "") + 'Read\\' + os.path.basename(filePath)
  265. try:
  266. shutil.move(source, destination, shutil.copy2)
  267. return True
  268. except:
  269. try:
  270. shutil.move(source, destination, shutil.copytree)
  271. return True
  272. except:
  273. incrementError()
  274. print("There was a problem to move the file " + filePath)
  275. traceback.print_exc()
  276. if createLog:
  277. logFile.write(
  278. str(nError) + ". There was a problem to move the file " + filePath + "\n" + traceback.format_exc() + "\n")
  279. return False
  280. def new_parse(marketPlace, url, createLog):
  281. from MarketPlaces.Initialization.markets_mining import config, CURRENT_DATE
  282. global nError
  283. nError = 0
  284. print("Parsing the " + marketPlace + " market and conduct data classification to store the information in the database.")
  285. # Connecting to the database
  286. con = connectDataBase()
  287. cur = con.cursor(cursor_factory=RealDictCursor)
  288. # Creating the tables (The database should be created manually)
  289. create_database(cur, con)
  290. mainDir = os.path.join(config.get('Project', 'shared_folder'), "MarketPlaces\\" + marketPlace + "\\HTML_Pages")
  291. # Creating the log file for each Forum
  292. if createLog:
  293. try:
  294. logFile = open(mainDir + f"/{CURRENT_DATE}/" + marketPlace + "_" + CURRENT_DATE + ".log", "w")
  295. except:
  296. print("Could not open log file!")
  297. createLog = False
  298. logFile = None
  299. # raise SystemExit
  300. else:
  301. logFile = None
  302. source_lang = get_source_language(marketPlace)
  303. # Reading the Listing Html Pages
  304. listings = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Listing", '*.html'))
  305. listings.sort(key=os.path.getmtime)
  306. for listingIndex, listingFile in enumerate(listings):
  307. print("Reading listing folder of '" + marketPlace + "', file '" + os.path.basename(listingFile) + "', index= " + str(
  308. listingIndex + 1) + " ... " + str(len(listings)))
  309. listingSoup = read_file(listingFile, createLog, logFile)
  310. # listing flags
  311. doParseListing = listingSoup is not None
  312. doDescription = False
  313. readDescriptionError = False
  314. parseDescriptionError = False
  315. persistDescriptionError = False
  316. moveDescriptionError = False
  317. findDescriptionError = False
  318. rw = []
  319. if doParseListing:
  320. rw = parse_listing(marketPlace, listingFile, listingSoup, createLog, logFile)
  321. doDescription = rw is not None
  322. if doDescription:
  323. nFound = 0
  324. for rec in rw:
  325. rec = rec.split(',')
  326. descriptionPattern = cleanLink(rec[22]) + ".html"
  327. # Reading the associated description Html Pages
  328. descriptions = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Description", descriptionPattern))
  329. descriptions.sort(key=os.path.getmtime)
  330. nFound += len(descriptions)
  331. for descriptionIndex, descriptionFile in enumerate(descriptions):
  332. print("Reading description folder of '" + marketPlace + "', file '" + os.path.basename(
  333. descriptionFile) + "', index= " + str(descriptionIndex + 1) + " ... " + str(len(descriptions)))
  334. descriptionSoup = read_file(descriptionFile, createLog, logFile)
  335. # description flags
  336. doParseDescription = descriptionSoup is not None
  337. doPersistRecord = False
  338. doMoveDescription = False
  339. rmm = []
  340. if doParseDescription:
  341. rmm = parse_description(marketPlace, descriptionFile, descriptionSoup, createLog, logFile)
  342. doPersistRecord = rmm is not None
  343. else:
  344. readDescriptionError = True
  345. parseDescriptionError = True
  346. if doPersistRecord:
  347. # Combining the information from Listing and Description Pages
  348. rec = mergePages(rmm, rec)
  349. title = translate(rec[4], source_lang)
  350. content = translate(rec[5], source_lang)
  351. # Append to the list the classification of the topic
  352. rec.append(str(predict(title, content, language='sup_english')))
  353. # Persisting the information in the database
  354. persistSuccess = persist_record(url, rec, cur, con, createLog, logFile, listingFile,
  355. descriptionFile)
  356. doMoveDescription = persistSuccess
  357. else:
  358. parseDescriptionError = True
  359. if doMoveDescription:
  360. # move description files of completed folder
  361. moveSuccess = move_file(descriptionFile, createLog, logFile)
  362. if not moveSuccess:
  363. moveDescriptionError = True
  364. else:
  365. moveDescriptionError = True
  366. if not (nFound > 0):
  367. findDescriptionError = True
  368. incrementError()
  369. print(f"There was a problem to locate the file(s) for {listingFile} in the Description section!")
  370. if createLog:
  371. logFile.write(
  372. str(nError) + f". There was a problem to locate the file(s) for {listingFile}"
  373. f" in the Description section!\n\n")
  374. if not (readDescriptionError or parseDescriptionError or persistDescriptionError
  375. or moveDescriptionError or findDescriptionError):
  376. # move listing files of completed folder
  377. move_file(listingFile, createLog, logFile)
  378. # registering the current forum status (up/down) and the number of scraped pages in the database
  379. marketId = verifyMarketPlace(cur, marketPlace)
  380. if (marketId > 0):
  381. readListings = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Listing\\read", '*.html'))
  382. readDescriptions = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Description\\read", '*.html'))
  383. create_status(cur, marketId, CURRENT_DATE, len(readListings), len(readDescriptions), '1' if len(listings) > 0 else '0')
  384. con.commit()
  385. if createLog:
  386. logFile.close()
  387. cur.close()
  388. con.close()
  389. print("Parsing the " + marketPlace + " market and data classification done.")