Advertisement
framp

User extraction from youtube comments

Sep 19th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Just go to http://www.youtube.com/all_comments?v=I6gYpSCJ99Y.
  3. On that page copy this bookmarklet on the url bar:
  4. javascript:(function(){var script=document.createElement("script");script.src="http://blackboard.altervista.org/extractUserFromYoutubeComments.js";document.head.appendChild(script)})();
  5.  
  6. Alternatively, open a developer console on the page (CTRL+SHIFT+J on Chrome, CTRL+SHIFT+K on Firefox) and write:
  7. (function(){var script=document.createElement("script");script.src="http://blackboard.altervista.org/extractUserFromYoutubeComments.js";document.head.appendChild(script)})();
  8.  
  9. This should work on every youtube all comments page.
  10. The script get how many votes each user has and then give them one random number for each vote.
  11. The user with the highest random number wins.
  12.  
  13. You can read the code below or at the location specified in the bookmarklet.
  14. */
  15. var logic = function(){
  16.     var authors = [],
  17.         votes = {},
  18.         comments = [],
  19.         op = $('.grayText:contains("From:")').next().text().toLowerCase();
  20.  
  21.     //getting all the authors
  22.     $('.author>a').each(function(){
  23.         var author = $(this).text().toLowerCase();
  24.         //the author of the comment with id x is saved in authors[x]
  25.         authors.push(author);
  26.         //give each unique user (but the original poster) a vote
  27.         if (author!==op){
  28.             votes[author] = 1;
  29.         }
  30.     });
  31.  
  32.     //getting all the comments
  33.     $('.comment-text>p').each(function(i){
  34.         var comment = $(this).text().toLowerCase();
  35.         //for every comment, check if the one of the users is referenced
  36.         //(and check if someone is referencing himself)
  37.         for (user in votes){
  38.             if (comment.indexOf(user)!==-1 && user!==authors[i-1]){
  39.                 votes[user]++;
  40.             }
  41.         }
  42.     });
  43.    
  44.     //each user has a random number from 0 to 1
  45.     //the highest one wins
  46.     //each additional vote give one more chance
  47.     //i.e.: 2 votes => 2 random numbers
  48.    
  49.     var globalMax = 0;
  50.     var winningUser;
  51.     for(user in votes){
  52.         var max = 0;
  53.         for (i=0; i<votes[user]; i++){
  54.             var number = Math.random();
  55.             if (number>max)
  56.                 max = number;
  57.         }
  58.         if (max>globalMax){
  59.             globalMax = max;
  60.             winningUser = user;
  61.         }
  62.     }
  63.     alert(winningUser);
  64. };
  65.  
  66. //Loading jQuery
  67. var script = document.createElement('script');
  68. script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js";
  69. document.head.appendChild(script);
  70.  
  71. var done = false;
  72. script.onload = script.onreadystatechange = function() {
  73.     if (!done && (!this.readyState ||
  74.             this.readyState === "loaded" || this.readyState === "complete") ) {
  75.         done = true;
  76.         logic();
  77.         script.onload = script.onreadystatechange = null;
  78.     }
  79. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement