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.

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