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.

253 lines
9.2 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. __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 cryptBB_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("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. # Finding the repeated tag that corresponds to the listing of posts
  29. # try:
  30. posts = soup.find('table', {"class": "tborder tfixed clear"}).find('td', {"id": "posts_container"}).find_all(
  31. 'div', {"class": "post"})
  32. # For each message (post), get all the fields we are interested to:
  33. for ipost in posts:
  34. # Finding a first level of the HTML page
  35. post_wrapper = ipost.find('span', {"class": "largetext"})
  36. # Finding the author (user) of the post
  37. author = post_wrapper.text.strip()
  38. user.append(cleanString(author)) # Remember to clean the problematic characters
  39. # Finding the status of the author
  40. smalltext = ipost.find('div', {"class": "post_author"})
  41. '''
  42. # Testing here two possibilities to find this status and combine them
  43. if ipost.find('div', {"class": "deleted_post_author"}):
  44. status.append(-1)
  45. interest.append(-1)
  46. reputation.append(-1)
  47. addDate.append(-1)
  48. post.append("THIS POST HAS BEEN REMOVED!")
  49. sign.append(-1)
  50. feedback.append(-1)
  51. continue
  52. '''
  53. # CryptBB does have membergroup and postgroup
  54. membergroup = smalltext.find('div', {"class": "profile-rank"})
  55. postgroup = smalltext.find('div', {"class": "postgroup"})
  56. if membergroup != None:
  57. membergroup = membergroup.text.strip()
  58. if postgroup != None:
  59. postgroup = postgroup.text.strip()
  60. membergroup = membergroup + " - " + postgroup
  61. else:
  62. if postgroup != None:
  63. membergroup = postgroup.text.strip()
  64. else:
  65. membergroup = "-1"
  66. status.append(cleanString(membergroup))
  67. # Finding the interest of the author
  68. # CryptBB does not have blurb
  69. blurb = smalltext.find('li', {"class": "blurb"})
  70. if blurb != None:
  71. blurb = blurb.text.strip()
  72. else:
  73. blurb = "-1"
  74. interest.append(cleanString(blurb))
  75. # Finding the reputation of the user
  76. # CryptBB does have reputation
  77. author_stats = smalltext.find('div', {"class": "author_statistics"})
  78. karma = author_stats.find('strong')
  79. if karma != None:
  80. karma = karma.text
  81. karma = karma.replace("Community Rating: ", "")
  82. karma = karma.replace("Karma: ", "")
  83. karma = karma.strip()
  84. else:
  85. karma = "-1"
  86. reputation.append(cleanString(karma))
  87. # Getting here another good tag to find the post date, post content and users' signature
  88. postarea = ipost.find('div', {"class": "post_content"})
  89. dt = postarea.find('span', {"class": "post_date"}).text
  90. # dt = dt.strip().split()
  91. dt = dt.strip()
  92. day=date.today()
  93. if "Yesterday" in dt:
  94. yesterday = day - timedelta(days=1)
  95. yesterday = yesterday.strftime('%m-%d-%Y')
  96. stime = dt.replace('Yesterday,','').strip()
  97. date_time_obj = yesterday+ ', '+stime
  98. date_time_obj = datetime.strptime(date_time_obj,'%m-%d-%Y, %I:%M %p')
  99. elif "hours ago" in dt:
  100. day = day.strftime('%m-%d-%Y')
  101. date_time_obj = postarea.find('span', {"class": "post_date"}).find('span')['title']
  102. date_time_obj = datetime.strptime(date_time_obj, '%m-%d-%Y, %I:%M %p')
  103. else:
  104. date_time_obj = datetime.strptime(dt, '%m-%d-%Y, %I:%M %p')
  105. stime = date_time_obj.strftime('%b %d, %Y')
  106. sdate = date_time_obj.strftime('%I:%M %p')
  107. addDate.append(date_time_obj)
  108. # Finding the post
  109. inner = postarea.find('div', {"class": "post_body scaleimages"})
  110. inner = inner.text.strip()
  111. post.append(cleanString(inner))
  112. # Finding the user's signature
  113. # signature = ipost.find('div', {"class": "post_wrapper"}).find('div', {"class": "moderatorbar"}).find('div', {"class": "signature"})
  114. signature = ipost.find('div', {"class": "signature scaleimages"})
  115. if signature != None:
  116. signature = signature.text.strip()
  117. # print(signature)
  118. else:
  119. signature = "-1"
  120. sign.append(cleanString(signature))
  121. # As no information about user's feedback was found, just assign "-1" to the variable
  122. feedback.append("-1")
  123. # Populate the final variable (this should be a list with all fields scraped)
  124. row = (topic, user, status, reputation, interest, sign, post, feedback, addDate)
  125. # Sending the results
  126. return row
  127. # This is the method to parse the Listing Pages (one page with many posts)
  128. def cryptBB_listing_parser(soup):
  129. nm = 0 # *this variable should receive the number of topics
  130. forum = "OnniForums" # 0 *forum name
  131. board = "-1" # 1 *board name (the previous level of the topic in the Forum categorization tree.
  132. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  133. author = [] # 2 *all authors of each topic
  134. topic = [] # 3 *all topics
  135. views = [] # 4 number of views of each topic
  136. posts = [] # 5 number of posts of each topic
  137. href = [] # 6 this variable should receive all cleaned urls (we will use this to do the marge between
  138. # Listing and Description pages)
  139. addDate = [] # 7 when the topic was created (difficult to find)
  140. # Finding the board (should be just one)
  141. board = soup.find('span', {"class": "active"}).text
  142. board = cleanString(board.strip())
  143. # Finding the repeated tag that corresponds to the listing of topics
  144. itopics = soup.find_all('tr',{"class": "inline_row"})
  145. for itopic in itopics:
  146. # For each topic found, the structure to get the rest of the information can be of two types. Testing all of them
  147. # to don't miss any topic
  148. # Adding the topic to the topic list
  149. try:
  150. topics = itopic.find('span', {"class": "subject_old"}).find('a').text
  151. except:
  152. topics = itopic.find('span', {"class": "subject_new"}).find('a').text
  153. topics = re.sub("\[\w*\]", '', topics)
  154. topic.append(cleanString(topics))
  155. # Counting how many topics we have found so far
  156. nm = len(topic)
  157. # Adding the url to the list of urls
  158. try:
  159. link = itopic.find('span', {"class": "subject_old"}).find('a').get('href')
  160. except:
  161. link = itopic.find('span',{"class": "subject_new"}).find('a').get('href')
  162. href.append(link)
  163. # Finding the author of the topic
  164. ps = itopic.find('div', {"class":"author smalltext"}).find('a').text
  165. user = ps.strip()
  166. author.append(cleanString(user))
  167. # Finding the number of replies
  168. columns = itopic.findChildren('td',recursive=False)
  169. replies = columns[3].text
  170. posts.append(cleanString(replies))
  171. # Finding the number of Views
  172. tview = columns[4].text
  173. views.append(cleanString(tview))
  174. # If no information about when the topic was added, just assign "-1" to the variable
  175. addDate.append("-1")
  176. return organizeTopics(forum, nm, board, author, topic, views, posts, href, addDate)
  177. def cryptBB_links_parser(soup):
  178. # Returning all links that should be visited by the Crawler
  179. href = []
  180. listing = soup.find('table', {"class": "tborder clear"}).find('tbody').find_all('tr', {"class": "inline_row"})
  181. for a in listing:
  182. try:
  183. link = a.find('span', {"class": "subject_old"}).find('a').get('href')
  184. except:
  185. link = a.find('span', {"class": "subject_new"}).find('a').get('href')
  186. href.append(link)
  187. return href