daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Jul 12th, 2015 186 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function doGet(e) {
  2.   return HtmlService.createHtmlOutput('<html><style>body{ font-size:16px; } #help { font-size:10px; } #help:hover { font-size:19px; } t</style> <body> <div id="frm"> <form action="#" method="get"> Multireddit URL<br />'
  3.                                       + '<input type="text" name="URL" id="URL" value="" /> <div id="help">This can be found by going to <a href="https://www.reddit.com/subreddits/">this page</a> and right-clicking in the sidebar on the text "multireddit of your subscriptions". Select copy, and paste into this textbox.</div><br />'
  4.                                       + '<input type="button" name="submit" value="Submit" '
  5.                                       + 'onClick="begin()"> '
  6.                                       + '</form> </div> <script> function begin() {try { google.script.run.withSuccessHandler(complete).start(document.getElementById("URL").value);} catch (e){document.write("<h1>It appears as though Voat is down, please try again later</h1>");}} function complete(table) { document.write(table);}</script></body> </html>')
  7.       .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  8. }
  9.  
  10.  
  11.  
  12.  
  13. JSON_SRC = "https://voat.co/api/top200subverses";
  14. NAME_TXT = "Name: ";
  15. AFTER_NAME_TXT = ",Description: ";
  16. SUBSCRIBER_TXT = ",Subscribers: ";
  17. AFTER_SUBSCRIBER_TXT = ",Created: "
  18. AFTER_TXT = "reddit.com/r/";
  19. ALT = [["reddit", "voat"], ["voat","reddit"], ["porn", "pix"], ["porn", "pics"], ["_", ""], ["soccer", "football"], ["eli5", "explainlikeimfive"]];
  20.  
  21. function getSubInfo() {
  22.   //retrieve JSON data as utf-8
  23.   var complete = true;
  24.  
  25.   do {
  26.     try {
  27.       var data = UrlFetchApp.fetch(JSON_SRC).getBlob().getAs("application/json").getDataAsString("UTF-8");
  28.       complete = true;
  29.     } catch (e) {return false;}
  30.   } while(!complete);
  31.  
  32.   var doc = JSON.parse(data);
  33.   var content = data.substr(2,data.length-2).split('","');
  34.  
  35.   //parse JSON as an array
  36.   var info = [];
  37.   for (i = 0; i < content.length; i++) {
  38.     var text = content[i];
  39.     var name = text.substr(text.indexOf(NAME_TXT)+NAME_TXT.length, text.indexOf(AFTER_NAME_TXT)-(text.indexOf(NAME_TXT)+NAME_TXT.length));
  40.     var subscribers = Number(text.substr(text.lastIndexOf(SUBSCRIBER_TXT)+SUBSCRIBER_TXT.length, text.lastIndexOf(AFTER_SUBSCRIBER_TXT)-(text.lastIndexOf(SUBSCRIBER_TXT)+SUBSCRIBER_TXT.length)));
  41.    
  42.     if (subscribers > 0) {
  43.       info.push([name.toLowerCase(),subscribers]);
  44.     }
  45.   }//end for
  46.  
  47.   return info;
  48. }//end getSubInfo
  49.  
  50. //super simple helper method
  51. function getReddits(text) {
  52.   var reddits = text.substr(text.indexOf(AFTER_TXT)+AFTER_TXT.length, text.length).toLowerCase().split("+");
  53.   return reddits;
  54. }//end getReddits
  55.  
  56. //another stupid simple helper method
  57. function srch(reddits, subInfo) {
  58.   for (j = 0; j < reddits.length; j++) {
  59.     if (subInfo == reddits[j]) return true;
  60.   }//end for
  61.   return false;
  62. }//end search
  63.  
  64. function start(url, sheet) {
  65.   var subverses = [[]];
  66.   var subInfo = getSubInfo();
  67.   var reddits = getReddits(url);
  68.   var l = reddits.length;
  69.  
  70.   Logger.log("Point 1");
  71.  
  72.   if (subInfo == false) return "<h1>Voat Appears to Be Down, Please Try Later</h1>";
  73.  
  74.   //search reddits and add any that could be slightly different
  75.   for (i = 0, l = reddits.length; i < l; i++) {
  76.     for (j = 0; j < ALT.length; j++) {
  77.       if (reddits[i].search(ALT[j][0]) != -1) reddits.push(reddits[i].replace(ALT[j][0],ALT[j][1]));
  78.     }//end for
  79.   }//end for
  80.  
  81.   Logger.log("Point 2 : reddits.length=" + reddits.length + " srch(reddits, subInfo[2][0]) = " + srch(reddits,[["chloegracemoretz"]]));
  82.  
  83.   //search
  84.   for (i = 0; i < subInfo.length; i++) {
  85.     if (srch(reddits, subInfo[i][0])) subverses.push(subInfo[i]);
  86.     Logger.log(subverses.toString() + " : i=" + i + " : subInfo.length=" + subInfo.length);
  87.   }//end for
  88.  
  89.   Logger.log("Point 3");
  90.  
  91.   var table = "<table>"
  92.  
  93.   for (i = 1; i < subverses.length; i++) {
  94.     table = table + "<tr><td><a target='_blank' href='https://www.voat.co/v/" + subverses[i][0] + "'>/v/" + subverses[i][0] + ": " + subverses[i][1] + " subscribers." + "</a></td></tr>";
  95.   }//end for
  96.  
  97.   table = table + "</table><a onclick=\""
  98.  
  99.   for (i = 1; i < subverses.length; i++) {
  100.     table = table + "window.open('https://www.voat.co/v/" + subverses[i][0] + "'); ";
  101.   }//end for
  102.  
  103.   table = table + "\" href=''>Click here to open all in new tabs.</a> Note: you may have to disable your pop-up blocker for this.";
  104.  
  105.   table = table + "<h1>Finished</h1>";
  106.  
  107.   return table;
  108. }//end of start
  109.  
  110. function test() {
  111.   //get the voat stats spreadsheet by name
  112.   var file = DriveApp.getFilesByName("TESTER");
  113.   var sheet, flagged;
  114.   //if there isn't one, create it
  115.   if (!file.hasNext()) {
  116.     sheet = SpreadsheetApp.create("TESTER").getSheets()[0];
  117.   } else {
  118.     sheet = SpreadsheetApp.open(file.next()).getSheets()[0];
  119.   }
  120.  
  121.   sheet.getRange(1,sheet.getLastColumn()+1).setValue("text" + start("https://www.reddit.com/r/Blackout2015+news+boxxy+chloegracemoretz+fatpeoplehate+fatpeoplehateGW+HAESSuccessStories+koe+RagenChastain+RedditALTernatives+TPPartnership+Voat",sheet));
  122. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top