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.

226 lines
8.0 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. datetime_append = datetime.strptime(date_time_cleaned, "%Y-%m-%d %H:%M:%S GMT")
  61. addDate.append(datetime_append)
  62. # Finding the post
  63. user_post = ipost.find("div", {"class": "content-c"}).text
  64. post.append(cleanString(user_post))
  65. # Finding the user's signature
  66. sign.append("-1")
  67. # As no information about user's feedback was found, just assign "-1" to the variable
  68. feedback.append("-1")
  69. # Populate the final variable (this should be a list with all fields scraped)
  70. # print(topic)
  71. # print(user)
  72. # print(status)
  73. # print(reputation)
  74. # print(interest)
  75. # print(sign)
  76. # print(post)
  77. # print(feedback)
  78. # print(addDate)
  79. # print(len(user))
  80. # print(len(status))
  81. # print(len(reputation))
  82. # print(len(interest))
  83. # print(len(sign))
  84. # print(len(feedback))
  85. # print(len(addDate))
  86. row = (topic, user, status, reputation, interest, sign, post, feedback, addDate)
  87. # Sending the results
  88. return row
  89. # This is the method to parse the Listing Pages (one page with many posts)
  90. def libre_listing_parser(soup):
  91. nm = 0 # *this variable should receive the number of topics
  92. forum = "Libre" # 0 *forum name
  93. board = "-1" # 1 *board name (the previous level of the topic in the Forum categorization tree.
  94. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  95. author = [] # 2 *all authors of each topic
  96. topic = [] # 3 *all topics
  97. views = [] # 4 number of views of each topic
  98. posts = [] # 5 number of posts of each topic
  99. href = [] # 6 this variable should receive all cleaned urls (we will use this to do the marge between
  100. # Listing and Description pages)
  101. addDate = [] # 7 when the topic was created (difficult to find)
  102. # Finding the board (should be just one)
  103. board = soup.find('div', {"class": "title"}).find("h1").text
  104. board = cleanString(board.strip())
  105. # Finding the repeated tag that corresponds to the listing of topics
  106. itopics = soup.find("div", {"class", "space-y-2 mt-4"}).find_all('div', {"class": "flex box"})
  107. nm = 0
  108. for itopic in itopics:
  109. nm += 1
  110. # For each topic found, the structure to get the rest of the information can be of two types. Testing all of them
  111. # to don't miss any topic
  112. # Adding the topic to the topic list
  113. topic_string = itopic.find("a", {"class": "link text-xl text-zinc-300"}).text
  114. cleaned_topic_string = cleanString(topic_string.strip())
  115. topic.append(cleaned_topic_string)
  116. # Adding the url to the list of urls
  117. link_to_clean = itopic.find("a", {"class": "link text-xl text-zinc-300"}).get("href")
  118. href.append(link_to_clean)
  119. # Finding the author of the topic
  120. username_not_cleaned = itopic.find('div', {"class": "flex-grow p-2 text-justify"}).find('a').text
  121. username_cleaned = username_not_cleaned.split("/")[-1]
  122. author.append(cleanString(username_cleaned))
  123. # Finding the number of views
  124. num_views = itopic.find_all("div", {"class": "flex items-center"})[0].find("p").text
  125. views.append(cleanString(num_views))
  126. # Finding the number of replies
  127. num_replies = itopic.find_all("div", {"class": "flex items-center"})[1].find("p").text
  128. posts.append(cleanString(num_replies))
  129. # If no information about when the topic was added, just assign "-1" to the variable
  130. date_time_concatenated = itopic.find("p", {"class": "text-sm text-zinc-400 italic"}).text
  131. date_time_cleaned = date_time_concatenated.replace(username_not_cleaned, "")
  132. # creating the datetime object
  133. date_time_array = date_time_cleaned[3:]
  134. datetime_append = datetime.strptime(date_time_array, "%Y-%m-%d %H:%M:%S GMT")
  135. addDate.append(datetime_append)
  136. # print(forum)
  137. # print(nm)
  138. # print(board)
  139. # print(author)
  140. # print(topic)
  141. # print(views)
  142. # print(href)
  143. # print(addDate)
  144. # print(len(author))
  145. # print(len(topic))
  146. # print(len(views))
  147. # print(len(href))
  148. # print(len(addDate))
  149. return organizeTopics(
  150. forum=forum,
  151. nm=nm,
  152. board=board,
  153. author=author,
  154. topic=topic,
  155. views=views,
  156. posts=posts,
  157. href=href,
  158. addDate=addDate
  159. )
  160. def libre_links_parser(soup):
  161. # Returning all links that should be visited by the Crawler
  162. href = []
  163. listing = soup.find_all('div', {"class": "flex-grow p-2 text-justify"})
  164. for a in listing:
  165. link = a.find('div', {'class': 'flex space-x-2 items-center'}).find('a').get('href')
  166. href.append(link)
  167. return href