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.

311 lines
12 KiB

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