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.

203 lines
7.9 KiB

  1. __author__ = 'DarkWeb'
  2. # Here, we are importing the auxiliary functions to clean or convert data
  3. from typing import List
  4. from Forums.Utilities.utilities import *
  5. from datetime import date
  6. from datetime import timedelta
  7. import re
  8. import string
  9. # Here, we are importing BeautifulSoup to search through the HTML tree
  10. from bs4 import BeautifulSoup
  11. # This is the method to parse the Description Pages (one page to each topic in the Listing Pages)
  12. def onniForums_description_parser(soup: BeautifulSoup) -> tuple:
  13. topicName: str = "-1" # 0 *topic name
  14. users : List[str] = [] # 1 *all users of each post
  15. statuses : List[str] = [] # 2 all user's authority in each post such as (adm, member, dangerous)
  16. reputations : List[str] = [] # 3 all user's karma in each post (usually found as a number)
  17. interests : List[str] = [] # 4 all user's interest in each post
  18. signs : List[str] = [] # 5 all user's signature in each post (usually a standard message after the content of the post)
  19. posts : List[str] = [] # 6 all messages of each post
  20. feedbacks : List[str] = [] # 7 all feedbacks of each vendor (this was found in just one Forum and with a number format)
  21. addDates : List[datetime] = [] # 8 all dates of each post
  22. # Getting the topicName
  23. topicName = soup.find("table", {"class": "tborder tfixed clear"}) \
  24. .find("td", {"class": "thead"}) \
  25. .find_all("div")[-1].text
  26. topicName = cleanString(topicName.strip())
  27. topics_array = soup.find_all("div", {"class": "post"})
  28. for topic in topics_array:
  29. # Extracting and cleaning author information
  30. author_information: BeautifulSoup = topic.find("div", {"class": "author_information"})
  31. username: str = author_information.find("span", {"class": "largetext"}).text
  32. username_cleaned = cleanString(username.strip())
  33. users.append(username_cleaned)
  34. user_status: str = author_information.find("span", {"class": "smalltext"}).text
  35. # Banned users often have weird text issues in HTML
  36. # So we detect banned users and give them a unique string
  37. if user_status.find("Banned") > 0: user_status_cleaned = "Banned"
  38. elif user_status.find("Unregistered") > 0: user_status_cleaned = "Unregistered"
  39. else: user_status_cleaned = cleanString(user_status.strip()) # Remove excessive spaces in string
  40. # Add cleaned data into array
  41. statuses.append(user_status_cleaned)
  42. if user_status_cleaned in ['Unregistered', 'Banned']: reputations.append(-1)
  43. else:
  44. author_statistics: BeautifulSoup = topic.find("div", {"class": "author_statistics"})
  45. reputation: str = author_statistics.find_all("div", {"class": "float_right"})[-1].text
  46. reputation_cleaned = cleanString(reputation.strip())
  47. reputations.append(reputation_cleaned)
  48. # Append a "-1" to `interests` and `signs` array since they don't exist on this forum
  49. interests.append("-1")
  50. signs.append("-1")
  51. post_content: str = topic.find("div", {"class": "post_body scaleimages"}).text
  52. # Clean post content of excessive spaces and characters
  53. post_content_cleaned = post_content.replace("[You must reply to view this hidden content]", "")
  54. post_content_cleaned = cleanString(post_content_cleaned.strip())
  55. posts.append(post_content_cleaned)
  56. # Append a "-1" to `feedbacks` array since they don't exists on this forum
  57. feedbacks.append("-1")
  58. date_posted: str = topic.find("span", {"class": "post_date"}).text
  59. date_posted_cleaned = cleanString(date_posted.split(",")[0])
  60. today = datetime.now()
  61. if date_posted_cleaned == 'Yesterday':
  62. date_object = today - timedelta(days=1)
  63. elif date_posted_cleaned.find('hour') > 0:
  64. hours_ago = int(date_posted_cleaned.split(' ')[0])
  65. date_object = today - timedelta(hours=hours_ago)
  66. elif date_posted_cleaned.find('minute') > 0:
  67. minutes_ago = int(date_posted_cleaned.split(' ')[0])
  68. date_object = today - timedelta(minutes=minutes_ago)
  69. else:
  70. date_object = datetime.strptime(date_posted_cleaned, "%m-%d-%Y")
  71. addDates.append(date_object)
  72. # TESTING PURPOSES - DO NOT REMOVE
  73. # Populate the final variable (this should be a list with all fields scraped)
  74. row = (topicName, users, statuses, reputations, interests, signs, posts, feedbacks, addDates)
  75. # Sending the results
  76. return row
  77. def onniForums_listing_parser(soup: BeautifulSoup):
  78. boardName = "-1" # board name (the previous level of the topic in the Forum categorization tree.
  79. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  80. forum = "OnniForums"
  81. nm = 0 # this variable should receive the number of topics
  82. topic : List[str] = [] # all topics
  83. user : List[str] = [] # all users of each topic
  84. post : List[int] = [] # number of posts of each topic
  85. view : List[int] = [] # number of views of each topic
  86. addDate : List[str] = [] # when the topic was created (difficult to find)
  87. href : List[str] = [] # this variable should receive all cleaned urls (we will use this to do the merge between
  88. # Listing and Description pages)
  89. # Finding the board (should be just one)
  90. board_metadata: BeautifulSoup = soup.find("table",{"class" : "tborder clear"})
  91. boardName = board_metadata.find_all("div")[1].text
  92. boardName = cleanString(boardName.strip())
  93. thread_arrays = board_metadata.find_all("tr", {"class":"inline_row"}) # gets the information of posts
  94. nm = len(thread_arrays)
  95. for thread in thread_arrays: #getting the information from the posts and sorting them into the arrays defined above
  96. body = thread.find("span",{"class": "subject_new"})
  97. try:
  98. post_subject: str = body.text #getting the topic
  99. except AttributeError:
  100. body = thread.find("span",{"class": "subject_old"})
  101. post_subject: str = body.text
  102. post_subject_cleaned = cleanString(post_subject.strip())
  103. topic.append(post_subject_cleaned)
  104. reply_count = thread.find_all("td", {"align": "center"})[2].text
  105. post.append(reply_count)
  106. views = thread.find_all("td", {"align": "center"})[3].text
  107. view.append(views)
  108. # dates_added: str = thread.find("span",{"class" : "thread_start_datetime smalltext"}).text
  109. # dates_added_cleaned = dates_added.split(',')[0]
  110. # addDate.append(dates_added_cleaned)
  111. author = thread.find("span",{"class" : "author smalltext"}).text
  112. author_cleaned = cleanString(author.strip())
  113. user.append(author_cleaned)
  114. thread_link = body.find('a').get('href')
  115. href.append(thread_link)
  116. return organizeTopics(
  117. forum=forum,
  118. nm=nm,
  119. board=boardName,
  120. author=user,
  121. topic=topic,
  122. views=view,
  123. posts=post,
  124. href=href,
  125. addDate=addDate
  126. )
  127. # This is the method to parse the Listing Pages (one page with many posts)
  128. def onniForums_links_parser(soup: BeautifulSoup):
  129. href = []
  130. listing = soup.find_all('tr', {'class': 'inline_row'})
  131. for thread in listing:
  132. try:
  133. link = thread.find('span', {"class": "subject_old"}).find('a').get('href')
  134. except:
  135. link = thread.find('span', {"class": "subject_new"}).find('a').get('href')
  136. href.append(link)
  137. return href