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.

142 lines
5.3 KiB

  1. __author__ = 'Helium'
  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 procrax_description_parser(soup: Tag):
  11. # Fields to be parsed
  12. topic = "-1" # topic name
  13. user = [] # all users of each post
  14. addDate = [] # all dated of each post
  15. feedback = [] # all feedbacks of each vendor (this was found in just one Forum and with a number format)
  16. status = [] # all user's authority in each post such as (adm, member, dangerous)
  17. reputation = [] # all user's karma in each post (usually found as a number)
  18. sign = [] # all user's signature in each post (usually a standard message after the content of the post)
  19. post = [] # all messages of each post
  20. interest = [] # all user's interest in each post
  21. # Finding the topic (should be just one coming from the Listing Page)
  22. li = soup.find("h1", {"class": "p-title-value"})
  23. topic = li.text
  24. thread: ResultSet[Tag] = soup.find("div", {"class": "block-body js-replyNewMessageContainer"}).find_all("article", {"data-author": True})
  25. for ipost in thread:
  26. username = ipost.find("h4", {"class": "message-name"}).text
  27. user.append(cleanString(username.strip()))
  28. date_posted = ipost.find("ul", {"class": "message-attribution-main listInline"}).find("time").get("datetime")
  29. datetime_obj = datetime.strptime(date_posted, "%Y-%m-%dT%H:%M:%S%z")
  30. addDate.append(datetime_obj)
  31. feedback.append("-1")
  32. user_status = ipost.find("h5", {"class": "userTitle message-userTitle"}).text
  33. status.append(cleanString(user_status.strip()))
  34. user_lvl = ipost.find("div", {"class": "afAwardLevel"}).text
  35. reputation.append(cleanString(user_lvl.strip()))
  36. sign.append("-1")
  37. user_post = ipost.find("article", {"class": "message-body js-selectToQuote"}).text
  38. post.append(cleanString(user_post.strip()))
  39. interest.append("-1")
  40. # Populate the final variable (this should be a list with all fields scraped)
  41. row = (topic, user, status, reputation, interest, sign, post, feedback, addDate)
  42. # Sending the results
  43. return row
  44. # This is the method to parse the Listing Pages (one page with many posts)
  45. def procrax_listing_parser(soup: Tag):
  46. board = "-1" # board name (the previous level of the topic in the Forum categorization tree.
  47. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  48. nm = 0 # this variable should receive the number of topics
  49. topic = [] # all topics
  50. author = [] # all authors of each topic
  51. views = [] # number of views of each topic
  52. posts = [] # number of posts of each topic
  53. addDate = [] # when the topic was created (difficult to find)
  54. href = [] # this variable should receive all cleaned urls (we will use this to do the marge between
  55. # Listing and Description pages)
  56. # Finding the board (should be just one)
  57. li = soup.find("h1", {"class": "p-title-value"})
  58. board = cleanString(li.text.strip())
  59. threads_list: ResultSet[Tag] = soup.find("div", {"class": "structItemContainer-group js-threadList"}).find_all("div", {"data-author": True})
  60. nm = len(threads_list)
  61. for thread in threads_list:
  62. thread_title = thread.find("div", {"class": "structItem-title"}).text
  63. topic.append(cleanString(thread_title.strip()))
  64. thread_author = thread.get("data-author")
  65. author.append(cleanString(thread_author))
  66. thread_views = thread.find("dl", {"class": "pairs pairs--justified structItem-minor"}).find('dd').text
  67. views.append(cleanString(thread_views.strip()))
  68. thread_replies = thread.find("dl", {"class": "pairs pairs--justified"}).find('dd').text
  69. # All threads contain one topic post and reply posts
  70. thread_total_posts = str(1 + int(thread_replies))
  71. posts.append(thread_total_posts)
  72. thread_date = thread.find("li", {"class": "structItem-startDate"}).find("time").get("datetime")
  73. datetime_obj = datetime.strptime(thread_date, "%Y-%m-%dT%H:%M:%S%z")
  74. addDate.append(datetime_obj)
  75. thread_link: str = thread.find("div", {"class": "structItem-title"}).find('a').get('href')
  76. href.append(thread_link)
  77. return organizeTopics(
  78. forum="Procrax",
  79. nm=nm,
  80. board=board,
  81. author=author,
  82. topic=topic,
  83. views=views,
  84. posts=posts,
  85. addDate=addDate,
  86. href=href
  87. )
  88. def procrax_links_parser(soup):
  89. # Returning all links that should be visited by the Crawler
  90. href = []
  91. listing = soup.find_all('div', {"class": "structItem-title"})
  92. for a in listing:
  93. link = a.find('a', {'class': ''}).get('href')
  94. href.append(link)
  95. return href