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.

138 lines
5.1 KiB

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 altenens_description_parser(soup):
  11. topic = "-1" # 0 *topic name
  12. user = [] # 1 *all users of each post
  13. status = [] # 2 all user's authority in each post such as (adm, member, dangerous)
  14. reputation = [] # 3 all user's karma in each post (usually found as a number)
  15. interest = [] # 4 all user's interest in each post
  16. sign = [] # 5 all user's signature in each post (usually a standard message after the content of the post)
  17. post = [] # 6 all messages of each post
  18. feedback = [] # 7 all feedbacks of each vendor (this was found in just one Forum and with a number format)
  19. addDate = [] # 8 all dates of each post
  20. topic = soup.find("h1", {"class": "p-title-value"}).text
  21. topic = cleanString(topic.strip())
  22. iposts = soup.find('div', {"class": "block-body js-replyNewMessageContainer"}).find_all('article')
  23. for ipost in iposts:
  24. author = ipost.find('h4', {"class": "message-name"}).text
  25. user.append(cleanString(author.strip()))
  26. stat = ipost.find('h5', {"class": "userTitle message-userTitle"}).text
  27. status.append(cleanString(stat.strip()))
  28. bar = ipost.find('div', {"class": "xtr-progress-bar"})
  29. if bar is not None:
  30. rep = bar.find('p').get('data-value')
  31. else:
  32. rep = "-1"
  33. reputation.append(cleanString(rep))
  34. interest.append("-1")
  35. signature = ipost.find('aside', {"class": "message-signature"})
  36. if signature is not None:
  37. signature = signature.text.strip()
  38. else:
  39. signature = "-1"
  40. sign.append(cleanString(signature))
  41. inner = ipost.find('div', {"class": "bbWrapper"}).find(text=True, recursive=False)
  42. post.append(cleanString(inner.strip()))
  43. feedback.append("-1")
  44. dt = ipost.find('time', {"class": "u-dt"})
  45. date_time_obj = datetime.strptime(dt, '%m-%d-%Y, %I:%M %p')
  46. addDate.append(date_time_obj)
  47. # Populate the final variable (this should be a list with all fields scraped)
  48. row = (topic, user, status, reputation, interest, sign, post, feedback, addDate)
  49. # Sending the results
  50. return row
  51. # This is the method to parse the Listing Pages (one page with many posts)
  52. def altenens_listing_parser(soup):
  53. nm = 0 # *this variable should receive the number of topics
  54. forum = "Altenens" # 0 *forum name
  55. board = "-1" # 1 *board name (the previous level of the topic in the Forum categorization tree.
  56. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  57. author = [] # 2 *all authors of each topic
  58. topic = [] # 3 *all topics
  59. views = [] # 4 number of views of each topic
  60. posts = [] # 5 number of posts of each topic
  61. href = [] # 6 this variable should receive all cleaned urls (we will use this to do the marge between
  62. # Listing and Description pages)
  63. addDate = [] # 7 when the topic was created (difficult to find)
  64. board = soup.find('h1', {"class": "p-title-value"}).text
  65. board = cleanString(board.strip())
  66. itopics = soup.find_all('div', {"class": "structItem-cell structItem-cell--main"})
  67. nm = len(itopics)
  68. for itopic in itopics:
  69. topics = itopic.find('div', {"class": "structItem-title"}).text
  70. topic.append(cleanString(topics.strip()))
  71. link = itopic.find('a').get('href')
  72. href.append(link)
  73. user = itopic.find('div', {"class": "structItem-parts"}).find('a').text
  74. author.append(cleanString(user.strip()))
  75. dt = itopic.find('li', {"class": "structItem-startDate"}).get('datetime')
  76. date_time_obj = datetime.strptime(dt, '%m-%d-%Y, %I:%M %p')
  77. addDate.append(date_time_obj)
  78. itopics = soup.find_all('div', {"class": "structItem-cell structItem-cell--meta"})
  79. for itopic in itopics:
  80. nposts = itopic.find('dl', {"class": "pairs pairs--justified"}).text
  81. nposts = nposts.replace('K', '000')
  82. posts.append(cleanString(nposts))
  83. nviews = itopic.find('dl', {"class": "pairs pairs--justified structItem-minor"}).text
  84. nviews = nviews.replace('K', '000')
  85. views.append(cleanString(nviews))
  86. return organizeTopics(forum, nm, board, author, topic, views, posts, href, addDate)
  87. def altenens_links_parser(soup):
  88. # Returning all links that should be visited by the Crawler
  89. href = []
  90. listing = soup.find_all('div', {"class": "structItem-cell structItem-cell--main"})
  91. for a in listing:
  92. link = a.find('a', {"class": ""}).get('href')
  93. href.append(link)
  94. return href