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.

212 lines
8.4 KiB

  1. # Here, we are importing the auxiliary functions to clean or convert data
  2. from Forums.Utilities.utilities import *
  3. import datetime
  4. import re
  5. # Here, we are importing BeautifulSoup to search through the HTML tree
  6. from bs4 import BeautifulSoup
  7. # This is the method to parse the Description Pages (one page to each topic in the Listing Pages)
  8. def endchan_description_parser(soup):
  9. # Fields to be parsed
  10. topic = "-1" # 0 *topic name
  11. user = [] # 1 *all users of each post
  12. status = [] # 2 all user's authority in each post such as (adm, member, dangerous)
  13. reputation = [] # 3 all user's karma in each post (usually found as a number)
  14. interest = [] # 4 all user's interest in each post
  15. sign = [] # 5 all user's signature in each post (usually a standard message after the content of the post)
  16. post = [] # 6 all messages of each post
  17. feedback = [] # 7 all feedbacks of each vendor (this was found in just one Forum and with a number format)
  18. addDate = [] # 8 all dates of each post
  19. image_user = [] # 9 all user avatars of each post
  20. image_post = [] # 10 all first images of each post
  21. # Finding the topic (should be just one coming from the Listing Page)
  22. entire_post = soup.find('div', {"id": "threadList"}).find('div', {"id": "divThreads"}).find('div',
  23. class_ = re.compile('opCell'))
  24. original_post = entire_post.find('div', {"class": "innerOP"})
  25. post_header = original_post.find('div', {"class": "opHead"})
  26. topic = post_header.find('span', {"class": "labelSubject"}).text
  27. topic = re.sub("\[\w*\]", '', topic)
  28. topic = topic.replace(",","")
  29. topic = topic.replace("\n","")
  30. topic = cleanString(topic.strip())
  31. # the replies are separated from the original post, so have to get original post and then get repeated tags for the replies
  32. #functions to reuse code:
  33. def get_user(area):
  34. name = area.find('a', class_=re.compile('linkName'))
  35. author = name.text.strip()
  36. #user.append(cleanString(author))
  37. def get_post(area):
  38. content = area.find('div', {"class": "divMessage"})
  39. content = content.text.strip()
  40. #post.append(cleanString(content))
  41. def get_date(area):
  42. dt = area.find('span', {"class": "labelCreated"}).text
  43. dt = dt.strip().split()
  44. date_time_obj = datetime.strftime(dt[0], '%m-%d-%Y')
  45. #addDate.append(date_time_obj)
  46. def get_user_img(area):
  47. avatar_img = area.find('img', class_= re.compile('imgFlag'))
  48. if avatar_img is not None:
  49. avatar_img = avatar_img.get('src').split('base64,')[-1]
  50. else:
  51. avatar_img = "-1"
  52. #image_user.append(avatar_img)
  53. def get_first_img(area):
  54. img_cell = area.find('div', class_= re.compile('panelUploads')).find('figure', {"class": "uploadCell"})
  55. if img_cell is not None:
  56. img = img_cell.find('img')
  57. img = img.get('src').split('base64,')[-1]
  58. else:
  59. img = "-1"
  60. #image_post.append(img)
  61. # Endchan does not have status, blurb, reputation, signature or feedback
  62. def set_other_lists():
  63. status.append("-1")
  64. reputation.append("-1")
  65. interest.append("-1")
  66. sign.append("-1")
  67. feedback.append("-1")
  68. # For the original post, get all fields we are interested in
  69. # get user
  70. get_user(post_header)
  71. # get post
  72. get_post(original_post)
  73. # get addDate
  74. get_date(post_header)
  75. # get user image
  76. get_user_img(post_header)
  77. #get first post image
  78. get_first_img(original_post)
  79. #no status, interest, reputation, feedback, or signature
  80. #set_other_lists()
  81. # Finding the repeated tag that corresponds to the listing of posts
  82. post_replies = entire_post.find('div', {"class": "divPosts"}).find_all('div', class_ = re.compile('postCell'))
  83. # For all replies, get all the fields we are interested in
  84. for ipost in post_replies:
  85. post_area = ipost.find('div', {"class": "innerPost"})
  86. # Finding user of the post
  87. get_user(post_area)
  88. # getting post date
  89. get_date(post_area)
  90. # getting the post content
  91. get_post(post_area)
  92. # get first image from post using panel uploads
  93. get_first_img(post_area)
  94. # get author avatar
  95. get_user_img(post_area)
  96. #set_other_lists()
  97. # Populate the final variable (this should be a list with all fields scraped)
  98. #row = (topic, user, status, reputation, interest, sign, post, feedback, addDate, image_user, image_post)
  99. # Sending the results
  100. #return row
  101. # This is the method to parse the Listing Pages (one page with many posts)
  102. def endchan_listing_parser(soup):
  103. nm = 0 # *this variable should receive the number of topics
  104. forum = "Endchan" # 0 *forum name
  105. board = "-1" # 1 *board name (the previous level of the topic in the Forum categorization tree.
  106. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  107. author = [] # 2 *all authors of each topic
  108. topic = [] # 3 *all topics
  109. views = [] # 4 number of views of each topic
  110. posts = [] # 5 number of posts of each topic
  111. href = [] # 6 this variable should receive all cleaned urls (we will use this to do the marge between
  112. # Listing and Description pages)
  113. addDate = [] # 7 when the topic was created (difficult to find)
  114. image_author = [] # 8 all author avatars used in each topic
  115. # Finding the board (should be just one)
  116. header = soup.find("header", {"class": "boardHeader"})
  117. labelName = header.find("p", {"id": "labelName"}).text
  118. board = cleanString(labelName.strip())
  119. # Finding the repeated tag that corresponds to the listing of topics
  120. topics = soup.find('div', {"id": "threadList"}).find('div', {"id": "divThreads"}).find_all('div',
  121. class_ = re.compile('opCell'))
  122. # Counting how many topics
  123. nm = len(topics)
  124. for itopic in topics:
  125. post_header = itopic.find('div', {"class": "innerOP"}).find('div', {"class": "opHead"})
  126. topics = post_header.find('span', {"class": "labelSubject"})
  127. # Adding the topic to the topic list
  128. topics = re.sub("\[\w*\]", '', topics)
  129. topic.append(cleanString(topics))
  130. # get author avatar
  131. avatar_img = post_header.find('img', class_ = re.compile('imgFlag'))
  132. if avatar_img is not None:
  133. avatar_img = avatar_img.get('src').split('base64,')[-1]
  134. else:
  135. avatar_img = "-1"
  136. image_author.append(avatar_img)
  137. # Adding the url to the list of urls
  138. #using linkSelf to get link, because the website is formatted differently.
  139. link = post_header.find('a', {"class": "linkSelf"}).get('href')
  140. href.append(link)
  141. # Finding the author of the topic
  142. name = post_header.find('a', class_= re.compile('linkName'))
  143. user = name.strip()
  144. author.append(cleanString(user))
  145. # Finding the number of replies
  146. replies = itopic.find('div', {"class": "divPosts"}).find_all('div', class_ = re.compile('postCell'))
  147. if replies is not None:
  148. num_replies = str(len(replies))
  149. posts.append(cleanString(num_replies))
  150. else:
  151. posts.append('-1')
  152. # No information on number of Views
  153. views.append('-1')
  154. #get date topic was added
  155. dt = post_header.find('span', {"class": "labelCreated"}).text
  156. dt = dt.strip().split()
  157. date_time_obj = datetime.strftime(dt[0], '%m-%d-%Y')
  158. addDate.append(date_time_obj)
  159. #return organizeTopics(forum, nm, board, author, topic, views, posts, href, addDate, image_author)
  160. def endchan_links_parser(soup):
  161. # Returning all links that should be visited by the Crawler
  162. href = []
  163. listing = soup.find('div', {"id": "threadList"}).find('div', {"id": "divThreads"}).find_all('div',
  164. class_ = re.compile('opCell'))
  165. for a in listing:
  166. link = a.find('a', {"class": "linkSelf"}).get('href')
  167. href.append(link)
  168. return href