Advertisement
thetenfold

GetFlix3-fixed

Jun 9th, 2013
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // ==UserScript==
  2.     // @name           GetFlix Grabber3
  3.     // @namespace      http://arantius.com/misc/greasemonkey/
  4.     // @description    Grab all the data about your NetFlix ratings, pass it to the GetFlix analyzer.
  5.     // @include        http://www.netflix.com/*
  6.     // @include        http://dvd.netflix.com/*
  7.     // @include        https://movies.netflix.com/*
  8.     // ==/UserScript==
  9.  
  10.     var website=window.location.origin;
  11.  
  12.     // Make sure the page is not in a frame
  13.     console.log (window.self);
  14.     console.log (window.top);
  15.     if(window.self !== window.top) {
  16.             throw "";
  17.     }
  18.      
  19.     // Add GM user script commands if possible
  20.     if(GM_registerMenuCommand) {
  21.             GM_registerMenuCommand('Start GetFlix', startGetFlix);
  22.             GM_registerMenuCommand('Stop GetFlix', stopGetFlix);
  23.     }
  24.  
  25.     var log = GM_log || (window.console ? console.log : function(e) {return true;}),
  26.             error = GM_log || (window.console ? console.error : function(e) {return true;});
  27.      
  28.     var button1=document.createElement('button');
  29.     button1.setAttribute('style', 'margin-left: 6px; vertical-align: middle; font-size: 9pt;');
  30.     button1.appendChild(document.createTextNode('Start'));
  31.     button1.addEventListener('click', startGetFlix, false);
  32.  
  33.     var button2=document.createElement('button');
  34.     button2.setAttribute('style', 'margin-left: 6px; vertical-align: middle; font-size: 9pt;');
  35.     button2.appendChild(document.createTextNode('Stop'));
  36.     button2.addEventListener('click', stopGetFlix, false);
  37.  
  38.     var button3=document.createElement('button');
  39.     button3.setAttribute('style', 'margin-left: 6px; vertical-align: middle; font-size: 9pt;');
  40.     button3.appendChild(document.createTextNode('Close'));
  41.     button3.addEventListener('click', closebox, false);
  42.        
  43.     var menu = document.createElement('li'),
  44.         span = document.createElement("span");
  45.      
  46.     span.appendChild(document.createTextNode('GetFlix2: '));
  47.     span.setAttribute("style", "font-size: 15px; color: #FFFFFF; font-family: Arial; font-weight: bold; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.5); line-height: 1.2; vertical-align: middle;");
  48.     menu.setAttribute("id", "menubox");
  49.     //menu.setAttribute('style', 'background-color: transparent; text-align: center; vertical-align: middle; padding: 22px 2%; float: right;');
  50.     menu.setAttribute('style', 'position: absolute; top: 37px; width: 245px; left: 57%; background-color: transparent; text-align: center;');
  51.     menu.appendChild(span);
  52.     menu.appendChild(button1);
  53.     menu.appendChild(button2);
  54.     menu.appendChild(button3);
  55.      
  56.     document.getElementById("global-header").appendChild(menu);
  57.  
  58.     function makeTextWindow() {
  59.      
  60.             if(document.getElementById("textwindow") !== null) return;
  61.            
  62.             var d = document.createElement("div");
  63.            
  64.             d.setAttribute("style", "background-color: #FFFFFF; position: absolute; top: 175px; width: 80%; left: 10%; z-index: 99999; border: 5px dotted #FF0000;");
  65.             d.setAttribute("id", "textwindow");
  66.                    
  67.             var t='<div style=width="400px";background-color:white;text-align:left;">' +    
  68.                       '<table cellspacing="2" cellpadding="2">' +
  69.                       '<tr style="background-color:white; text-align:center;">' +      
  70.                       '<td style="font-size:14pt"><strong>Netflix <em>Movie\'s You\'ve Rated</em> extractor</strong> </td></tr>' +
  71.                       '<tr><td align="center" id="td-buttons"></td></tr>' +
  72.                       '<tr style="background-color:white" ><td>Press <b>Start</b> to get your ratings. When finished ' +
  73.                       'cut and paste the results into Notepad and save the file as ratings.csv, which you can then import into a spreadsheet for analysis.</td>' +
  74.                       '<td><div style="text-align: right;color:red;font-weight:bold;" id="fetchct">Fetching Page</div></td></tr>';
  75.                        
  76.             t+='<tr><td align="center"><textarea style="color:black;font-size:10pt;" id="resultbox" rows="15" cols="80"></textarea></td></tr></table></div>';
  77.                    
  78.             d.innerHTML = t;        
  79.             document.body.appendChild(d);
  80.     }
  81.      
  82.     function startGetFlix() {
  83.             makeTextWindow();
  84.             // init a single-task queue
  85.             actionQueue=[ ['getRatingsPage', 1] ];
  86.             // and start the queue running!
  87.             runQueue();
  88.     }
  89.    
  90.     var stopRunning=0;
  91.     function stopGetFlix() {
  92.             console.log ("Stop Script");
  93.             actionQueue=[];
  94.             stopRunning=1;
  95.     }
  96.      
  97.     var actionQueue=[];
  98.     function runQueue() {
  99.             if (stopRunning) {
  100.                     console.log ("stopRunning = 1");
  101.                     return;
  102.             }
  103.             var action=actionQueue.shift();
  104.             //console.log("Queue length: "+actionQueue.length+".  Running action "+action[0]);
  105.      
  106.             switch (action[0]) {
  107.             case 'getRatingsPage':
  108.                     getRatingsPage(action[1]);
  109.                     break;
  110.             case 'parseRatings':
  111.                     parseRatingsPage(action[1], action[2]);
  112.                     break;
  113.             case 'saveRating':
  114.                     saveRating(action[1]);
  115.                     break;
  116.             }
  117.     }
  118.    
  119.     function getRatingsPage(pagenum) {
  120.         if (pagenum == 1) {
  121.                var url=website+'/MoviesYouveSeen';
  122.                // reset the results
  123.                var rf = document.getElementById('resultbox');
  124.                rf.value = "";
  125.             } else {
  126.                if (website == "http://dvd.netflix.com") {var url=website+'/MoviesYouveSeen'+'?pageNum='+parseInt(pagenum, 10);}
  127.                if (website == "http://movies.netflix.com") {var url=website+'/MoviesYouveSeen'+'?pn='+parseInt(pagenum, 10);}
  128.             }
  129.             var fc = document.getElementById('fetchct');
  130.             fc.innerHTML = "Fetching Page " + pagenum;
  131.             //console.info('Fetch:', url);
  132.             GM_xmlhttpRequest({
  133.                     'method':'GET',
  134.                     'url':url,
  135.                     onload: function(xhr) {
  136.                             //console.log("getRatingsPage load status= "+xhr.status);
  137.                             actionQueue.push(['parseRatings', pagenum, xhr.responseText]);
  138.                             runQueue();
  139.                     }
  140.             });
  141.     }
  142.      
  143.     function parseRatingsPage(num, text) {
  144.             //Search pattern used to verify you have rated a movie
  145.             var ratingRegex = /You rated this movie[^\d]+(\d)/;
  146.             var ratings = text.indexOf("<tr class=\"agMovie") === -1 ? text.split('addlk') : text.split("<tr class=\"agMovie"),
  147.                 blankArray = ["",""];
  148.             // get rid of the HTML before the first one        
  149.             ratings.shift();
  150.             var MovieRating = "";
  151.             for (var i=0, rating=null; rating=ratings[i]; i++) {
  152.                     //Test to make sure movie is rated so you don't get a rating.match exception
  153.                     if(ratingRegex.test(rating)) {
  154.                             MovieRating=rating.match(ratingRegex)[1];
  155.                             var detail={
  156.                                     'id':rating.match(/\/(\d+)\?trkid=/i)[1],
  157.                                     'title':(rating.match(/"title"><a.*?>(.*?)</) || rating.match(/<span class="title "><a[^>]+>([^<]+)/i))[1],
  158.                                     'year':(rating.match(/"list-titleyear"> \(([0-9]+)\)/i) || blankArray)[1],
  159.                                     'genre':(rating.match(/"list-genre">(.+?)</i) || blankArray)[1],
  160.                                     'rating':MovieRating
  161.                             };
  162.                             actionQueue.push(['saveRating', detail]);
  163.                     } else {
  164.                         var detail1={'title':(rating.match(/"title"><a.*?>(.*?)</) || rating.match(/<span class="title "><a[^>]+>([^<]+)/i))[1] };
  165.                         console.log ("Movie not rated = "+detail1.title+" on page "+num);
  166.                     }
  167.             }
  168.             //Test to see if more ratings pages
  169.             if(/paginationLink-next|go to the next page/i.test(text)) {
  170.                     actionQueue.push(['getRatingsPage', num+1]);
  171.                     runQueue();
  172.             } else { console.log ("Done extracting movie ratings"); }
  173.     }
  174.      
  175.     function saveRating(detail) {
  176.             //replace any commas in movie titles as this is being saved as a comma delimited .csv file
  177.             var newtitle=detail.title.replace(/,/g,'');
  178.             var rf = document.getElementById('resultbox');
  179.             rf.value += (newtitle+","+detail.year+","+detail.genre+","+detail.rating+"\n");          
  180.             runQueue();
  181.     }
  182.      
  183.     function closebox() {
  184.         document.body.removeChild(document.getElementById("textwindow"));  
  185.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement