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.

329 lines
13 KiB

1 year ago
  1. __author__ = 'DarkWeb'
  2. # Here, we are importing the auxiliary functions to clean or convert data
  3. from Forums.Utilities.utilities import *
  4. from datetime import date
  5. from datetime import timedelta
  6. import re
  7. # Here, we are importing BeautifulSoup to search through the HTML tree
  8. from bs4 import BeautifulSoup
  9. # This is the method to parse the Description Pages (one page to each topic in the Listing Pages)
  10. def dwForums_description_parser(soup):
  11. # Fields to be parsed
  12. topic = "-1" # topic name
  13. user = [] # all users of each post
  14. addDate = [] # all dated of each post
  15. feedback = [] # all feedbacks of each vendor (this was found in just one Forum and with a number format)
  16. status = [] # all user's authority in each post such as (adm, member, dangerous)
  17. reputation = [] # all users's karma in each post (usually found as a number)
  18. sign = [] # all user's signature in each post (usually a standard message after the content of the post)
  19. post = [] # all messages of each post
  20. interest = [] # all user's interest in each post
  21. # Finding the topic (should be just one coming from the Listing Page)
  22. li = soup.find("h1", {"class": "p-title-value"})
  23. topic = li.text
  24. topic = topic.replace(u'\xa0', ' ')
  25. topic = topic.replace(",","")
  26. topic = topic.replace("\n","")
  27. topic = cleanString(topic.strip())
  28. # print(topic)
  29. # Finding the repeated tag that corresponds to the listing of posts
  30. # posts = soup.find("form", {"name": "quickModForm"}).findAll('div', {"class": "windowbg"}) + \
  31. # soup.find("form", {"name": "quickModForm"}).findAll('div', {"class": "windowbg2"})
  32. try:
  33. posts = soup.find('div', {"class": "js-replyNewMessageContainer"}).find_all(
  34. 'article', {"class": "js-post"}, recursive=False)
  35. # print(len(posts))
  36. # For each message (post), get all the fields we are interested to:
  37. for ipost in posts:
  38. # Finding a first level of the HTML page
  39. # post_wrapper = ipost.find('div', {"class": "post_wrapper"}).find('div', {"class": "poster"})
  40. post_wrapper = ipost.find('h4', {"class": "message-name"})
  41. # Finding the author (user) of the post
  42. # author = post_wrapper.find('h4')
  43. author = post_wrapper.text.strip()
  44. # print("author " + author)
  45. user.append(cleanString(author)) # Remember to clean the problematic characters
  46. # Finding the status of the author
  47. # Testing here two possibilities to find this status and combine them
  48. # if ipost.find('h5', {"class": "deleted_post_author"}):
  49. # status.append(-1)
  50. # interest.append(-1)
  51. # reputation.append(-1)
  52. # addDate.append(-1)
  53. # post.append("THIS POST HAS BEEN REMOVED!")
  54. # sign.append(-1)
  55. # feedback.append(-1)
  56. # continue
  57. # CryptBB does have membergroup and postgroup
  58. membergroup = ipost.find('h5', {"class": "userTitle"})
  59. # DWForums doesnt have postgroups
  60. postgroup = None
  61. if membergroup != None:
  62. membergroup = membergroup.text.strip()
  63. if postgroup != None:
  64. postgroup = postgroup.text.strip()
  65. membergroup = membergroup + " - " + postgroup
  66. else:
  67. if postgroup != None:
  68. membergroup = postgroup.text.strip()
  69. else:
  70. membergroup = "-1"
  71. status.append(cleanString(membergroup))
  72. # print("status " + cleanString(membergroup))
  73. # Finding the interest of the author
  74. # DWForums does not have blurb
  75. blurb = ipost.find('li', {"class": "blurb"})
  76. if blurb != None:
  77. blurb = blurb.text.strip()
  78. else:
  79. blurb = "-1"
  80. interest.append(cleanString(blurb))
  81. # Finding the reputation of the user
  82. # CryptBB does have reputation
  83. author_stats = ipost.find('div', {"class": "message-userExtras"})
  84. if author_stats != None:
  85. karma = author_stats.find_all('dl', {"class": "pairs"})[2]
  86. else:
  87. karma = None
  88. if karma != None:
  89. karma = karma.text
  90. karma = karma.replace("Reaction score","")
  91. karma = karma.replace(":", "")
  92. karma = karma.strip()
  93. else:
  94. karma = "-1"
  95. reputation.append(cleanString(karma))
  96. # print("karma " + cleanString(karma))
  97. # Getting here another good tag to find the post date, post content and users' signature
  98. postarea = ipost.find('div', {"class": "message-attribution-main"})
  99. dt = postarea.find('time', {"class": "u-dt"})['datetime']
  100. # dt = dt.strip().split()
  101. dt = dt.strip()[:16]
  102. dt = dt.replace("T",", ")
  103. day=date.today()
  104. if "Yesterday" in dt:
  105. yesterday = day - timedelta(days=1)
  106. yesterday = yesterday.strftime('%m-%d-%Y')
  107. stime = dt.replace('Yesterday,','').strip()
  108. date_time_obj = yesterday+ ', '+stime
  109. date_time_obj = datetime.strptime(date_time_obj,'%m-%d-%Y, %H:%M')
  110. elif "hours ago" in dt:
  111. day = day.strftime('%m-%d-%Y')
  112. date_time_obj = postarea.find('span', {"class": "post_date"}).find('span')['title']
  113. date_time_obj = datetime.strptime(date_time_obj, '%m-%d-%Y, %H:%M')
  114. else:
  115. date_time_obj = datetime.strptime(dt, '%Y-%m-%d, %H:%M')
  116. stime = date_time_obj.strftime('%b %d, %Y')
  117. sdate = date_time_obj.strftime('%I:%M %p')
  118. addDate.append(date_time_obj)
  119. # print("date " + str(date_time_obj))
  120. # Finding the date of the post
  121. # date_time_obj = datetime.strptime(dt, '%a %b %d, %Y %I:%M %p')
  122. # smalltext = postarea.find('div', {"class": "flow_hidden"}).find('div', {"class": "keyinfo"})\
  123. # .find('div', {"class": "smalltext"})
  124. # sdatetime = smalltext.text
  125. # sdatetime = sdatetime.replace(u"\xab","") # Removing unnecessary characters
  126. # sdatetime = sdatetime.replace(u"\xbb","") # Removing unnecessary characters
  127. # sdatetime = sdatetime.split("on: ") # Removing unnecessary characters
  128. # sdatetime = sdatetime[1].strip()
  129. # stime = sdatetime[:-12:-1] # Finding the time of the post
  130. # stime = stime[::-1]
  131. # sdate = sdatetime.replace(stime,"") # Finding the date of the post
  132. # sdate = sdate.replace(",","")
  133. # sdate = sdate.strip()
  134. # Covert the date of the post that can be informed as: "12 February 2016", "today", "yesterday". We need
  135. # a date format here as "mm/dd/yyyy"
  136. # addDate.append(convertDate(sdate,"english", crawlerDate) + " " + stime)
  137. # Finding the post
  138. inner = ipost.find('article', {"class": "message-body"})
  139. inner = inner.text.strip()
  140. # print(inner)
  141. post.append(cleanString(inner))
  142. # Finding the users's signature
  143. # signature = ipost.find('div', {"class": "post_wrapper"}).find('div', {"class": "moderatorbar"}).find('div', {"class": "signature"})
  144. signature = ipost.find('aside', {"class": "message-signature"})
  145. if signature != None:
  146. signature = signature.text.strip()
  147. # print(signature)
  148. else:
  149. signature = "-1"
  150. sign.append(cleanString(signature))
  151. # As no information about users's feedback was found, just assign "-1" to the variable
  152. feedback.append("-1")
  153. except:
  154. print("error when parsing posts")
  155. # if soup.find('td', {"class": "trow1"}).text == " You do not have permission to access this page. ":
  156. # user.append("-1")
  157. # status.append(-1)
  158. # interest.append(-1)
  159. # reputation.append(-1)
  160. # addDate.append(-1)
  161. # post.append("NO ACCESS TO THIS PAGE!")
  162. # sign.append(-1)
  163. # feedback.append(-1)
  164. # Populate the final variable (this should be a list with all fields scraped)
  165. row = (topic, post, user, addDate, feedback, status, reputation, sign, interest)
  166. # Sending the results
  167. return row
  168. # This is the method to parse the Listing Pages (one page with many posts)
  169. def dwForums_listing_parser(soup):
  170. board = "-1" # board name (the previous level of the topic in the Forum categorization tree.
  171. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  172. nm = 0 # this variable should receive the number of topics
  173. topic = [] # all topics
  174. user = [] # all users of each topic
  175. post = [] # number of posts of each topic
  176. view = [] # number of views of each topic
  177. addDate = [] # when the topic was created (difficult to find)
  178. href = [] # this variable should receive all cleaned urls (we will use this to do the marge between
  179. # Listing and Description pages)
  180. # Finding the board (should be just one)
  181. board = soup.find('h1', {"class": "p-title-value"}).text
  182. board = cleanString(board.strip())
  183. # Finding the repeated tag that corresponds to the listing of topics
  184. regex = re.compile('.*structItem--thread.*')
  185. itopics = soup.find_all("div", {"class": regex})
  186. index = 0
  187. for itopic in itopics:
  188. # For each topic found, the structure to get the rest of the information can be of two types. Testing all of them
  189. # to don't miss any topic
  190. # tds = itopic.findAll('td', {"class": "subject stickybg2"})
  191. #
  192. # if len(tds) > 0:
  193. # tag.append("strong")
  194. # tag.append("subject stickybg2")
  195. # tag.append("stats stickybg")
  196. # else:
  197. # tds = itopic.findAll('td', {"class": "subject windowbg2"})
  198. # if len(tds) > 0:
  199. # tag.append("span")
  200. # tag.append("subject windowbg2")
  201. # tag.append("stats windowbg")
  202. # Adding the topic to the topic list
  203. topics = itopic.find("div", {"class": "structItem-title"}).text
  204. topics = topics.replace(",", "")
  205. topics = topics.replace("\n", "")
  206. topic.append(cleanString(topics.strip()))
  207. # Counting how many topics we have found so far
  208. nm = len(topic)
  209. # Adding the url to the list of urls
  210. link = itopic.select_one('a[href^="/threads/"]')
  211. link = link['href']
  212. link = cleanLink(link)
  213. href.append(link)
  214. # Finding the author of the topic
  215. ps = itopic.find('a', {"class": "username"}).text
  216. author = ps.strip()
  217. user.append(cleanString(author))
  218. # Finding the number of replies
  219. meta = itopic.find("div", {"class": "structItem-cell--meta"})
  220. meta = meta.find_all("dl")
  221. posts = meta[0].find("dd").text
  222. post.append(cleanString(posts))
  223. # Finding the number of Views
  224. tview = meta[1].find("dd").text
  225. view.append(cleanString(tview))
  226. # If no information about when the topic was added, just assign "-1" to the variable
  227. minor = itopic.find("div", {"class": "structItem-minor"})
  228. dt = minor.find('time')['datetime']
  229. dt = dt.strip()[:16]
  230. dt = dt.replace("T", ", ")
  231. day = date.today()
  232. if "Yesterday" in dt:
  233. yesterday = day - timedelta(days=1)
  234. yesterday = yesterday.strftime('%m-%d-%Y')
  235. stime = dt.replace('Yesterday,', '').strip()
  236. date_time_obj = yesterday + ', ' + stime
  237. date_time_obj = datetime.strptime(date_time_obj, '%m-%d-%Y, %H:%M')
  238. else:
  239. date_time_obj = datetime.strptime(dt, '%Y-%m-%d, %H:%M')
  240. stime = date_time_obj.strftime('%b %d, %Y')
  241. sdate = date_time_obj.strftime('%I:%M %p')
  242. addDate.append(date_time_obj)
  243. index += 1
  244. return organizeTopics("DWForums", nm, topic, board, view, post, user, addDate, href)
  245. def dwForums_links_parser(soup):
  246. # Returning all links that should be visited by the Crawler
  247. href = []
  248. #print(soup.find('table', {"class": "tborder clear"}).find(
  249. # 'tbody').find_all('tr', {"class": "inline_row"}))
  250. regex = re.compile('.*structItem--thread.*')
  251. listing = soup.find_all("div", {"class": regex})
  252. for a in listing:
  253. link = a.select_one('a[href^="/threads/"]')
  254. link = link['href']
  255. href.append(link)
  256. return href