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.

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