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.

162 lines
6.2 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. image_user = [] # 9 all user avatars of each post
  21. image_post = [] # 10 all first images of each post
  22. topic = soup.find("h1", {"class": "p-title-value"}).text
  23. topic = cleanString(topic.strip())
  24. body = soup.find('div', {"class": "block-container lbContainer"})
  25. iposts = body.find_all('article', {"class": "message message--post js-post js-inlineModContainer"})
  26. for ipost in iposts:
  27. author = ipost.find('h4', {"class": "message-name"}).text
  28. user.append(cleanString(author.strip()))
  29. stat = ipost.find('h5', {"class": "userTitle message-userTitle"}).text
  30. status.append(cleanString(stat.strip()))
  31. bar = ipost.find('div', {"class": "xtr-progress-bar"})
  32. if bar is not None:
  33. rep = bar.find('p').get('data-value')
  34. else:
  35. rep = "-1"
  36. reputation.append(cleanString(rep))
  37. interest.append("-1")
  38. signature = ipost.find('aside', {"class": "message-signature"})
  39. if signature is not None:
  40. signature = signature.text.strip()
  41. else:
  42. signature = "-1"
  43. sign.append(cleanString(signature))
  44. inner = ipost.find('div', {"class": "bbWrapper"}).find(text=True, recursive=False)
  45. if inner is not None:
  46. inner = inner.strip()
  47. else:
  48. inner = "" # cannot use -1 because the post is hidden unless you reply
  49. post.append(cleanString(inner))
  50. feedback.append("-1")
  51. dt = ipost.find('time', {"class": "u-dt"}).get('datetime')
  52. date_time_obj = datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S%z')
  53. addDate.append(date_time_obj)
  54. img = ipost.find('div', {"class": "message-avatar-wrapper"}).find('img')
  55. if img is not None:
  56. img = img.get('src').split('base64,')[-1]
  57. else:
  58. img = "-1"
  59. image_user.append(img)
  60. image_post.append("-1")
  61. # Populate the final variable (this should be a list with all fields scraped)
  62. row = (topic, user, status, reputation, interest, sign, post, feedback, addDate, image_user, image_post)
  63. # Sending the results
  64. return row
  65. # This is the method to parse the Listing Pages (one page with many posts)
  66. def altenens_listing_parser(soup):
  67. nm = 0 # *this variable should receive the number of topics
  68. forum = "Altenens" # 0 *forum name
  69. board = "-1" # 1 *board name (the previous level of the topic in the Forum categorization tree.
  70. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  71. author = [] # 2 *all authors of each topic
  72. topic = [] # 3 *all topics
  73. views = [] # 4 number of views of each topic
  74. posts = [] # 5 number of posts of each topic
  75. href = [] # 6 this variable should receive all cleaned urls (we will use this to do the marge between
  76. # Listing and Description pages)
  77. addDate = [] # 7 when the topic was created (difficult to find)
  78. image_author = [] # 8 all author avatars used in each topic
  79. board = soup.find('h1', {"class": "p-title-value"}).text
  80. board = cleanString(board.strip())
  81. regex = re.compile('structItem structItem--thread.*')
  82. itopics = soup.find_all('div', {"class": regex})
  83. nm = len(itopics)
  84. for itopic in itopics:
  85. topics = itopic.find('div', {"class": "structItem-title"}).text
  86. topic.append(cleanString(topics.strip()))
  87. author_icon = itopic.find('a', {"class": "avatar avatar--s"})
  88. if author_icon != None:
  89. author_icon = author_icon.find('img')
  90. author_icon = author_icon.get('src')
  91. author_icon = author_icon.split('base64,')[-1]
  92. else:
  93. author_icon = "-1"
  94. image_author.append(author_icon)
  95. link = itopic.find('div', {"class": "structItem-title"}).find('a').get('href')
  96. href.append(link)
  97. user = itopic.find('ul', {"class": "structItem-parts"}).find('a').text
  98. author.append(cleanString(user.strip()))
  99. dt = itopic.find('time', {"class": "u-dt"}).get('datetime')
  100. date_time_obj = datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S%z')
  101. addDate.append(date_time_obj)
  102. nposts = itopic.find('dl', {"class": "pairs pairs--justified"}).text
  103. nposts = nposts.replace('Replies', '')
  104. nposts = nposts.replace('K', '000')
  105. posts.append(cleanString(nposts))
  106. nviews = itopic.find('dl', {"class": "pairs pairs--justified structItem-minor"}).text
  107. nviews = nviews.replace('Views', '')
  108. nviews = nviews.replace('K', '000')
  109. views.append(cleanString(nviews))
  110. return organizeTopics(forum, nm, board, author, topic, views, posts, href, addDate, image_author)
  111. def altenens_links_parser(soup):
  112. # Returning all links that should be visited by the Crawler
  113. href = []
  114. listing = soup.find_all('div', {"class": "structItem-cell structItem-cell--main"})
  115. for a in listing:
  116. link = a.find('a', {"class": ""}).get('href')
  117. href.append(link)
  118. return href