Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. /*
  2. * USAGE:
  3. * fetchComments()
  4. */
  5. var copyToClipboard = str => {
  6. const el = document.createElement('textarea')
  7. el.value = str
  8. document.body.appendChild(el)
  9. el.select()
  10. document.execCommand('copy')
  11. document.body.removeChild(el)
  12. }
  13.  
  14. var copyResults = (str, len) => {
  15. if (str) {
  16. window.localStorage.setItem('tw-scrapper-results', str)
  17. const $el = `<div style="background-color: black; color: white; position: fixed; width: 100%; left: 0; bottom: 0; z-index: 10000; text-align: center; padding: 1em;" id="tw-scrapper-container">${len} comentários extraídos <button onClick="copyResults()">Copiar</button></div>`
  18. const node = new DOMParser().parseFromString($el, 'text/html').body.childNodes[0]
  19. document.body.appendChild(node)
  20. } else {
  21. const results = window.localStorage.getItem('tw-scrapper-results')
  22. copyToClipboard(results)
  23. alert('Comentários copiados')
  24. document.querySelector('#tw-scrapper-container').parentElement.removeChild(document.querySelector('#tw-scrapper-container'))
  25. }
  26. }
  27. function fetchComments(length) {
  28.  
  29. let baseSelector = window.document
  30.  
  31. const prepareCSV = (data, header) => {
  32. data.unshift(header)
  33. const dataCSV = data.map(c => {
  34. return c.map(v => {
  35. return v
  36. ? v
  37. .replace(/[\n\s]+/g, ' ')
  38. .replace(/^\s+/, '')
  39. .replace(/\s+$/, '')
  40. : ''
  41. }).join('\t')
  42. }).join('\n')
  43. return dataCSV
  44. }
  45.  
  46. const scrapComments = () => {
  47. console.log('Raspando comentários...')
  48. const $comments = Array.from(baseSelector.querySelectorAll('.stream-items .tweet'))
  49. const header = ['timeString', 'name', 'username', 'text', 'likes', 'replies', 'retweets']
  50. const comments = $comments.map((el) => {
  51. const name = el.querySelector('.fullname').innerText
  52. const username = el.querySelector('.username b').innerText
  53. const timeEl = el.querySelector('.tweet-timestamp')
  54. const timeString = timeEl.getAttribute('data-original-title') || timeEl.getAttribute('title')
  55. const timeObj = new Date(parseInt(el.querySelector('.tweet-timestamp span').getAttribute('data-time-ms'), 10))
  56. const text = el.querySelector('.tweet-text').innerText
  57. const likes = el.querySelector('.ProfileTweet-action--favorite .ProfileTweet-actionCountForPresentation').innerText || '0'
  58. const replies = el.querySelector('.ProfileTweet-action--reply .ProfileTweet-actionCountForPresentation').innerText || '0'
  59. const retweets = el.querySelector('.ProfileTweet-action--retweet .ProfileTweet-actionCountForPresentation').innerText || '0'
  60. return [ timeString, name, username, text, likes, replies, retweets ]
  61. })
  62. const csvComments = prepareCSV(comments, header)
  63. if (comments.length) {
  64. copyToClipboard(csvComments)
  65. copyResults(csvComments, comments.length)
  66. } else {
  67. alert('Nenhum comentário encontrado')
  68. }
  69. }
  70.  
  71. const expandComments = () => {
  72. }
  73.  
  74. // Initialize
  75. return scrapComments()
  76. /* if (length) {
  77. return expandComments()
  78. .then(scrapComments)
  79. } else {
  80. return scrapComments()
  81. } */
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement