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.

230 lines
8.1 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, ResultSet, Tag
  9. # This is the method to parse the Description Pages (one page to each topic in the Listing Pages)
  10. def libre_description_parser(soup: Tag):
  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. topic_found = soup.find("a", {"class": "link text-xl text-zinc-300"}).text
  23. topic = cleanString(topic_found.strip())
  24. original_post: Tag = soup.find("div", {"class": "flex items-start"})
  25. original_user = original_post.find("div", {"class": "info-p"}).find("a", {"class": "link"}).text
  26. user.append(cleanString(original_user.replace("/u/", "").strip()))
  27. original_user_statistics: ResultSet[Tag] = original_post.find("div", {"class": "info-p"}).find_all("span")
  28. original_time = original_user_statistics[0].text[2:]
  29. datetime_append = datetime.strptime(original_time, "%Y-%m-%d %H:%M:%S GMT")
  30. addDate.append(datetime_append)
  31. original_karma = original_user_statistics[1].text[2]
  32. reputation.append(cleanString(original_karma.strip()))
  33. original_content = soup.find("div", {"class": "content-p"}).text
  34. post.append(cleanString(original_content.strip()))
  35. status.append("-1")
  36. interest.append("-1")
  37. sign.append("-1")
  38. feedback.append("-1")
  39. # Finding the repeated tag that corresponds to the listing of posts
  40. # try:
  41. posts: ResultSet[Tag] = soup.find_all("div", {"class": "flex items-stretch"})
  42. # For each message (post), get all the fields we are interested to:
  43. for ipost in posts:
  44. # Finding a first level of the HTML page
  45. # Finding the author (user) of the post
  46. user_name = ipost.find("p", {"class": "text-zinc-400 text-justify"}).find("a", {"class": "link"}).text
  47. user.append(cleanString(user_name.replace("/u/", "").strip())) # Remember to clean the problematic characters
  48. status.append("-1")
  49. # Finding the interest of the author
  50. # CryptBB does not have blurb
  51. interest.append("-1")
  52. # Finding the reputation of the user
  53. # CryptBB does have reputation
  54. karma = ipost.find("p", {"class": "text-zinc-400 text-justify"}).text
  55. karma_cleaned = karma.split(" ")[6]
  56. reputation.append(cleanString(karma_cleaned.strip()))
  57. # Getting here another good tag to find the post date, post content and users' signature
  58. date_posted = ipost.find("p", {"class": "text-zinc-400 text-justify"}).text
  59. date_time_cleaned = date_posted.replace(user_name, "")[3:-12]
  60. print(date_time_cleaned)
  61. datetime_append = datetime.strptime(date_time_cleaned, "%Y-%m-%d %H:%M:%S GMT")
  62. addDate.append(datetime_append)
  63. # Finding the post
  64. user_post = ipost.find("div", {"class": "content-c"}).text
  65. post.append(cleanString(user_post))
  66. # Finding the user's signature
  67. sign.append("-1")
  68. # As no information about user's feedback was found, just assign "-1" to the variable
  69. feedback.append("-1")
  70. # Populate the final variable (this should be a list with all fields scraped)
  71. # print(topic)
  72. # print(user)
  73. # print(status)
  74. # print(reputation)
  75. # print(interest)
  76. # print(sign)
  77. # print(post)
  78. # print(feedback)
  79. # print(addDate)
  80. # print(len(user))
  81. # print(len(status))
  82. # print(len(reputation))
  83. # print(len(interest))
  84. # print(len(sign))
  85. # print(len(feedback))
  86. # print(len(addDate))
  87. row = (topic, user, status, reputation, interest, sign, post, feedback, addDate)
  88. # Sending the results
  89. return row
  90. # This is the method to parse the Listing Pages (one page with many posts)
  91. def libre_listing_parser(soup):
  92. nm = 0 # *this variable should receive the number of topics
  93. forum = "Libre" # 0 *forum name
  94. board = "-1" # 1 *board name (the previous level of the topic in the Forum categorization tree.
  95. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  96. author = [] # 2 *all authors of each topic
  97. topic = [] # 3 *all topics
  98. views = [] # 4 number of views of each topic
  99. posts = [] # 5 number of posts of each topic
  100. href = [] # 6 this variable should receive all cleaned urls (we will use this to do the marge between
  101. # Listing and Description pages)
  102. addDate = [] # 7 when the topic was created (difficult to find)
  103. # Finding the board (should be just one)
  104. board = soup.find('div', {"class": "title"}).find("h1").text
  105. board = cleanString(board.strip())
  106. # Finding the repeated tag that corresponds to the listing of topics
  107. itopics = soup.find("div", {"class", "space-y-2 mt-4"}).find_all('div', {"class": "flex box"})
  108. nm = 0
  109. for itopic in itopics:
  110. nm += 1
  111. # For each topic found, the structure to get the rest of the information can be of two types. Testing all of them
  112. # to don't miss any topic
  113. # Adding the topic to the topic list
  114. topic_string = itopic.find("a", {"class": "link text-xl text-zinc-300"}).text
  115. cleaned_topic_string = cleanString(topic_string.strip())
  116. topic.append(cleaned_topic_string)
  117. # Adding the url to the list of urls
  118. link_to_clean = itopic.find("a", {"class": "link text-xl text-zinc-300"}).get("href")
  119. href.append(link_to_clean)
  120. # Finding the author of the topic
  121. username_not_cleaned = itopic.find('div', {"class": "flex-grow p-2 text-justify"}).find('a').text
  122. username_cleaned = username_not_cleaned.split("/")[-1]
  123. author.append(cleanString(username_cleaned))
  124. # Finding the number of views
  125. num_views = itopic.find_all("div", {"class": "flex items-center"})[0].find("p").text
  126. views.append(cleanString(num_views))
  127. # Finding the number of replies
  128. num_replies = itopic.find_all("div", {"class": "flex items-center"})[1].find("p").text
  129. posts.append(cleanString(num_replies))
  130. # If no information about when the topic was added, just assign "-1" to the variable
  131. date_time_concatenated = itopic.find("p", {"class": "text-sm text-zinc-400 italic"}).text
  132. date_time_cleaned = date_time_concatenated.replace(username_not_cleaned, "")
  133. # creating the datetime object
  134. date_time_array = date_time_cleaned[3:]
  135. datetime_append = datetime.strptime(date_time_array, "%Y-%m-%d %H:%M:%S GMT")
  136. addDate.append(datetime_append)
  137. # print(forum)
  138. # print(nm)
  139. # print(board)
  140. # print(author)
  141. # print(topic)
  142. # print(views)
  143. # print(href)
  144. # print(addDate)
  145. # print(len(author))
  146. # print(len(topic))
  147. # print(len(views))
  148. # print(len(href))
  149. # print(len(addDate))
  150. return organizeTopics(
  151. forum=forum,
  152. nm=nm,
  153. board=board,
  154. author=author,
  155. topic=topic,
  156. views=views,
  157. posts=posts,
  158. href=href,
  159. addDate=addDate
  160. )
  161. def libre_links_parser(soup):
  162. # Returning all links that should be visited by the Crawler
  163. href = []
  164. listing = soup.find_all('div', {"class": "flex-grow p-2 text-justify"})
  165. for a in listing:
  166. link = a.find('div', {'class': 'flex space-x-2 items-center'}).find('a').get('href')
  167. href.append(link)
  168. return href