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.

352 lines
11 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. __author__ = 'DarkWeb'
  2. import codecs
  3. import glob
  4. import os, re
  5. import shutil
  6. from psycopg2.extras import RealDictCursor
  7. from Forums.DB_Connection.db_connection import *
  8. from Forums.BestCardingWorld.parser import *
  9. from Forums.Classifier.classify_product import predict
  10. # from DarkWebMining_Sample.Forums.Classifier.classify_product import predict_semi
  11. # controls the log id
  12. nError = 0
  13. # determines if forum is russian, not really used now but maybe later
  14. def isRussianForum(forum):
  15. with open('russian_forums.txt') as f:
  16. forums = f.readlines()
  17. result = False
  18. for iforum in forums:
  19. iforum = iforum.replace('\n','')
  20. if iforum == forum:
  21. result = True
  22. break
  23. return result
  24. #tries to match description pages to listing pages by using a key made for every description page and every link in listing page
  25. #once verified and matched, the info is merged into a 'rec', which is returned
  26. #@param: detPage is a list of keys of valid pages, rec is the row of data of an instance
  27. #return: rec, row of data, that may have additional data added on after matching description to listing page
  28. def mergePages(rmm, rec):
  29. # key = u"Top:" + rec[1].upper().strip() + u" User:" + rec[5].upper().strip()
  30. # key = rec[16]
  31. print ("----------------- Matched: " + rec[3] + "--------------------")
  32. if rmm[9] != "-1": # image_user
  33. rec[9] = rmm[9]
  34. rec[10] = rmm[1]
  35. rec[11] = rmm[2]
  36. rec[12] = rmm[3]
  37. rec[13] = rmm[4]
  38. rec[14] = rmm[5]
  39. rec[15] = rmm[6]
  40. rec[16] = rmm[7]
  41. rec[17] = rmm[8]
  42. rec[18] = rmm[10]
  43. return rec
  44. #gets a string of posts and joins them together into one string to be put in the database as one string of text
  45. #@param: list of strings (the posts of a thread)
  46. #return: string containing the concatenation of all the strings
  47. def getPosts(posts):
  48. strPosts = ' '.join(posts)
  49. return strPosts.strip()
  50. #uses db connection , another program, methods to persists values to the correct categories
  51. #@param: row is the list of entries for this instance, cur is the db connection object
  52. def persist_data(url, row, cur):
  53. forum = create_forum(cur, row, url)
  54. author = create_author(cur, row, forum)
  55. topic = create_topic(cur, forum, row, author)
  56. create_posts(cur, row, forum, topic)
  57. def incrementError():
  58. global nError
  59. nError += 1
  60. def read_file(filePath, createLog, logFile):
  61. try:
  62. html = codecs.open(filePath.strip('\n'), encoding='utf8')
  63. soup = BeautifulSoup(html, "html.parser")
  64. html.close()
  65. time.sleep(0.01) # making sure the file is closed before returning soup object
  66. return soup
  67. except:
  68. try:
  69. html = open(filePath.strip('\n'))
  70. soup = BeautifulSoup(html, "html.parser")
  71. html.close()
  72. time.sleep(0.01) # making sure the file is closed before returning soup object
  73. return soup
  74. except:
  75. incrementError()
  76. print("There was a problem to read the file " + filePath)
  77. if createLog:
  78. logFile.write(
  79. str(nError) + ". There was a problem to read the file " + filePath + "\n" + traceback.format_exc() + "\n")
  80. return None
  81. def parse_listing(forum, listingFile, soup, createLog, logFile):
  82. try:
  83. if forum == "BestCardingWorld":
  84. rw = bestcardingworld_listing_parser(soup)
  85. else:
  86. print("MISSING CALL TO LISTING PARSER IN PREPARE_PARSER.PY!")
  87. raise Exception
  88. return rw
  89. except:
  90. incrementError()
  91. print("There was a problem to parse the file " + listingFile + " in the listing section!")
  92. traceback.print_exc()
  93. if createLog:
  94. logFile.write(
  95. str(nError) + ". There was a problem to parse the file " + listingFile + " in the Listing section.\n"
  96. + traceback.format_exc() + "\n")
  97. return None
  98. def parse_description(forum, descriptionFile, soup, createLog, logFile):
  99. try:
  100. if forum == "BestCardingWorld":
  101. rmm = bestcardingworld_description_parser(soup)
  102. else:
  103. print("MISSING CALL TO DESCRIPTION PARSER IN PREPARE_PARSER.PY!")
  104. raise Exception
  105. return rmm
  106. except:
  107. incrementError()
  108. print("There was a problem to parse the file " + descriptionFile + " in the Description section!")
  109. traceback.print_exc()
  110. if createLog:
  111. logFile.write(
  112. str(nError) + ". There was a problem to parse the file " + descriptionFile + " in the Description section.\n"
  113. + traceback.format_exc() + "\n")
  114. return None
  115. def persist_record(url, rec, cur, con, createLog, logFile, listingFile, descriptionFile):
  116. try:
  117. persist_data(url, tuple(rec), cur)
  118. con.commit()
  119. return True
  120. except:
  121. con.rollback()
  122. incrementError()
  123. print(f"There was a problem to persist the files ({listingFile} + {descriptionFile}) in the database!")
  124. traceback.print_exc()
  125. if createLog:
  126. logFile.write(
  127. str(nError) + f". There was a problem to persist the files ({listingFile} + {descriptionFile}) in the database!\n"
  128. + traceback.format_exc() + "\n")
  129. return False
  130. def move_file(filePath, createLog, logFile):
  131. source = filePath
  132. destination = filePath.replace(os.path.basename(filePath), "") + 'Read\\' + os.path.basename(filePath)
  133. try:
  134. shutil.move(source, destination, shutil.copy2)
  135. return True
  136. except:
  137. try:
  138. shutil.move(source, destination, shutil.copytree)
  139. return True
  140. except:
  141. incrementError()
  142. print("There was a problem to move the file " + filePath)
  143. traceback.print_exc()
  144. if createLog:
  145. logFile.write(
  146. str(nError) + ". There was a problem to move the file " + filePath + "\n" + traceback.format_exc() + "\n")
  147. return False
  148. #main method for this program, what actually gets the parsed info from the parser, and persists them into the db
  149. #calls the different parser methods here depending on the type of html page
  150. def new_parse(forum, url, createLog):
  151. from Forums.Initialization.forums_mining import config, CURRENT_DATE
  152. global nError
  153. nError = 0
  154. print("Parsing the " + forum + " forum and conduct data classification to store the information in the database.")
  155. # Connecting to the database
  156. con = connectDataBase()
  157. cur = con.cursor(cursor_factory=RealDictCursor)
  158. # Creating the tables (The database should be created manually)
  159. create_database(cur, con)
  160. mainDir = os.path.join(config.get('Project', 'shared_folder'), "Forums\\" + forum + "\\HTML_Pages")
  161. # Creating the log file for each Forum
  162. if createLog:
  163. try:
  164. logFile = open(mainDir + f"/{CURRENT_DATE}/" + forum + "_" + CURRENT_DATE + ".log", "w")
  165. except:
  166. print("Could not open log file!")
  167. createLog = False
  168. logFile = None
  169. # raise SystemExit
  170. else:
  171. logFile = None
  172. # Reading the Listing Html Pages
  173. listings = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Listing", '*.html'))
  174. for listingIndex, listingFile in enumerate(listings):
  175. print("Reading listing folder of '" + forum + "', file '" + os.path.basename(listingFile) + "', index= " + str(
  176. listingIndex + 1) + " ... " + str(len(listings)))
  177. listingSoup = read_file(listingFile, createLog, logFile)
  178. # listing flags
  179. doParseListing = listingSoup is not None
  180. doDescription = False
  181. readDescriptionError = False
  182. parseDescriptionError = False
  183. persistDescriptionError = False
  184. moveDescriptionError = False
  185. findDescriptionError = False
  186. rw = []
  187. if doParseListing:
  188. rw = parse_listing(forum, listingFile, listingSoup, createLog, logFile)
  189. doDescription = rw is not None
  190. if doDescription:
  191. nFound = 0
  192. for rec in rw:
  193. rec = rec.split(',')
  194. descriptionPattern = cleanLink(rec[6]) + "page[0-9]*.html"
  195. # Reading the associated description Html Pages
  196. descriptions = glob.glob(os.path.join(mainDir, CURRENT_DATE + "\\Description", descriptionPattern))
  197. nFound += len(descriptions)
  198. for descriptionIndex, descriptionFile in enumerate(descriptions):
  199. print("Reading description folder of '" + forum + "', file '" + os.path.basename(
  200. descriptionFile) + "', index= " + str(descriptionIndex + 1) + " ... " + str(len(descriptions)))
  201. descriptionSoup = read_file(descriptionFile, createLog, logFile)
  202. # description flags
  203. doParseDescription = descriptionSoup is not None
  204. doPersistRecord = False
  205. doMoveDescription = False
  206. rmm = []
  207. if doParseDescription:
  208. rmm = parse_description(forum, descriptionFile, descriptionSoup, createLog, logFile)
  209. doPersistRecord = rmm is not None
  210. else:
  211. readDescriptionError = True
  212. parseDescriptionError = True
  213. if doPersistRecord:
  214. # Combining the information from Listing and Description Pages
  215. rec = mergePages(rmm, rec)
  216. # Append to the list the classification of the topic
  217. rec.append(str(predict(rec[3], getPosts(rec[15]), language='sup_english')))
  218. # Persisting the information in the database
  219. persistSuccess = persist_record(url, rec, cur, con, createLog, logFile, listingFile, descriptionFile)
  220. doMoveDescription = persistSuccess
  221. else:
  222. parseDescriptionError = True
  223. if doMoveDescription:
  224. # move description files of completed folder
  225. moveSuccess = move_file(descriptionFile, createLog, logFile)
  226. if not moveSuccess:
  227. moveDescriptionError = True
  228. else:
  229. moveDescriptionError = True
  230. if not (nFound > 0):
  231. findDescriptionError = True
  232. incrementError()
  233. print(f"There was a problem to locate the file(s) for {listingFile} in the Description section!")
  234. if createLog:
  235. logFile.write(
  236. str(nError) + f". There was a problem to locate the file(s) for {listingFile}"
  237. f" in the Description section!\n\n")
  238. if not (readDescriptionError or parseDescriptionError or persistDescriptionError
  239. or moveDescriptionError or findDescriptionError):
  240. # move listing files of completed folder
  241. move_file(listingFile, createLog, logFile)
  242. if createLog:
  243. logFile.close()
  244. cur.close()
  245. con.close()
  246. print("Parsing the " + forum + " forum and data classification done.")