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.

255 lines
11 KiB

  1. __author__ = 'Helium'
  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 cryptBB_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("td", {"class": "thead"}).find('strong')
  23. topic = li.text
  24. topic = re.sub("\[\w*\]", '', topic)
  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('table', {"class": "tborder tfixed clear"}).find('td', {"id": "posts_container"}).find_all(
  34. 'div', {"class": "post"})
  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('span', {"class": "largetext"})
  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. smalltext = ipost.find('div', {"class": "post_author"})
  48. # Testing here two possibilities to find this status and combine them
  49. if ipost.find('div', {"class": "deleted_post_author"}):
  50. status.append(-1)
  51. interest.append(-1)
  52. reputation.append(-1)
  53. addDate.append(-1)
  54. post.append("THIS POST HAS BEEN REMOVED!")
  55. sign.append(-1)
  56. feedback.append(-1)
  57. continue
  58. # CryptBB does have membergroup and postgroup
  59. membergroup = smalltext.find('div', {"class": "profile-rank"})
  60. postgroup = smalltext.find('div', {"class": "postgroup"})
  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. # CryptBB does not have blurb
  75. blurb = smalltext.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 = smalltext.find('div', {"class": "author_statistics"})
  84. karma = author_stats.find('strong')
  85. if karma != None:
  86. karma = karma.text
  87. karma = karma.replace("Community Rating: ", "")
  88. karma = karma.replace("Karma: ", "")
  89. karma = karma.strip()
  90. else:
  91. karma = "-1"
  92. reputation.append(cleanString(karma))
  93. # print("karma " + cleanString(karma))
  94. # Getting here another good tag to find the post date, post content and users' signature
  95. postarea = ipost.find('div', {"class": "post_content"})
  96. dt = postarea.find('span', {"class": "post_date"}).text
  97. # dt = dt.strip().split()
  98. dt = dt.strip()
  99. day=date.today()
  100. if "Yesterday" in dt:
  101. yesterday = day - timedelta(days=1)
  102. yesterday = yesterday.strftime('%m-%d-%Y')
  103. stime = dt.replace('Yesterday,','').strip()
  104. date_time_obj = yesterday+ ', '+stime
  105. date_time_obj = datetime.strptime(date_time_obj,'%m-%d-%Y, %I:%M %p')
  106. elif "hours ago" in dt:
  107. day = day.strftime('%m-%d-%Y')
  108. date_time_obj = postarea.find('span', {"class": "post_date"}).find('span')['title']
  109. date_time_obj = datetime.strptime(date_time_obj, '%m-%d-%Y, %I:%M %p')
  110. else:
  111. date_time_obj = datetime.strptime(dt, '%m-%d-%Y, %I:%M %p')
  112. stime = date_time_obj.strftime('%b %d, %Y')
  113. sdate = date_time_obj.strftime('%I:%M %p')
  114. addDate.append(date_time_obj)
  115. # print("date " + str(date_time_obj))
  116. # Finding the date of the post
  117. # date_time_obj = datetime.strptime(dt, '%a %b %d, %Y %I:%M %p')
  118. # smalltext = postarea.find('div', {"class": "flow_hidden"}).find('div', {"class": "keyinfo"})\
  119. # .find('div', {"class": "smalltext"})
  120. # sdatetime = smalltext.text
  121. # sdatetime = sdatetime.replace(u"\xab","") # Removing unnecessary characters
  122. # sdatetime = sdatetime.replace(u"\xbb","") # Removing unnecessary characters
  123. # sdatetime = sdatetime.split("on: ") # Removing unnecessary characters
  124. # sdatetime = sdatetime[1].strip()
  125. # stime = sdatetime[:-12:-1] # Finding the time of the post
  126. # stime = stime[::-1]
  127. # sdate = sdatetime.replace(stime,"") # Finding the date of the post
  128. # sdate = sdate.replace(",","")
  129. # sdate = sdate.strip()
  130. # Covert the date of the post that can be informed as: "12 February 2016", "today", "yesterday". We need
  131. # a date format here as "mm/dd/yyyy"
  132. # addDate.append(convertDate(sdate,"english", crawlerDate) + " " + stime)
  133. # Finding the post
  134. inner = postarea.find('div', {"class": "post_body scaleimages"})
  135. inner = inner.text.strip()
  136. # print(inner)
  137. post.append(cleanString(inner))
  138. # Finding the users's signature
  139. # signature = ipost.find('div', {"class": "post_wrapper"}).find('div', {"class": "moderatorbar"}).find('div', {"class": "signature"})
  140. signature = ipost.find('div', {"class": "signature scaleimages"})
  141. if signature != None:
  142. signature = signature.text.strip()
  143. # print(signature)
  144. else:
  145. signature = "-1"
  146. sign.append(cleanString(signature))
  147. # As no information about users's feedback was found, just assign "-1" to the variable
  148. feedback.append("-1")
  149. except:
  150. if soup.find('td', {"class": "trow1"}).text == " You do not have permission to access this page. ":
  151. user.append("-1")
  152. status.append(-1)
  153. interest.append(-1)
  154. reputation.append(-1)
  155. addDate.append(-1)
  156. post.append("NO ACCESS TO THIS PAGE!")
  157. sign.append(-1)
  158. feedback.append(-1)
  159. # Populate the final variable (this should be a list with all fields scraped)
  160. row = (topic, post, user, addDate, feedback, status, reputation, sign, interest)
  161. # Sending the results
  162. return row
  163. # This is the method to parse the Listing Pages (one page with many posts)
  164. def AbyssForums_listing_parser(soup: BeautifulSoup):
  165. board = "-1" # board name (the previous level of the topic in the Forum categorization tree.
  166. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  167. nm = 0 # this variable should receive the number of topics
  168. topic = [] # all topics
  169. user = [] # all users of each topic
  170. post = [] # number of posts of each topic
  171. view = [] # number of views of each topic
  172. addDate = [] # when the topic was created (difficult to find)
  173. href = [] # this variable should receive all cleaned urls (we will use this to do the marge between
  174. # Listing and Description pages)
  175. #finding the board
  176. board = soup.find("title").text
  177. board = cleanString(board.strip())
  178. type_of_posts = soup.find_all("li", {"class": re.compile("row bg\d")} )
  179. for literature in type_of_posts:
  180. title_of_post = literature.find("a", {"class": "topictitle"}).text
  181. topic.append(title_of_post)
  182. author = literature.find("div", {"class": "topic-poster responsive-hide left-box"}).find("a", {"class": "username"}).text
  183. user.append(author)
  184. num_post = literature.find("dd", {"class": "posts"}).text[1:-3]
  185. post.append(num_post)
  186. num_view = literature.find("dd", {"class": "views"}).text[1:-3]
  187. view.append(num_view)
  188. if int(num_post) != 0:
  189. reply = literature.find("dd", {"class": "lastpost"}).find("a", {"class": "username"}).text
  190. user.append(reply)
  191. date_added = literature.find("time").text
  192. addDate.append(date_added)
  193. nm = len(topic)
  194. def abyssForum_links_parser(soup):
  195. # Returning all links that should be visited by the Crawler
  196. href = []
  197. #print(soup.find('table', {"class": "tborder clear"}).find(
  198. # 'tbody').find_all('tr', {"class": "inline_row"}))
  199. listing = soup.find_all('dl', {"class": "row-item topic_read"})
  200. for a in listing:
  201. link = a.find('div', {"class": "list-inner"}).find('a').get('href')
  202. href.append(link)
  203. return href