Last active
June 22, 2016 18:02
-
-
Save dmelcer9/8b53a297830d60d5879cbffece85c974 to your computer and use it in GitHub Desktop.
Parses the comments from a Lego Ideas page and gets the frequency of each number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var LegoURL = "https://ideas.lego.com/comments/project/32812051-934d-4d73-9961-b7a6cc652007/comments?order=newest&from=max" | |
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Refresh Lego Comments') | |
.addItem('Refresh', 'RefreshLegoCount') | |
.addToUi(); | |
} | |
function RefreshLegoCount() { | |
var Responses = JSON.parse(UrlFetchApp.fetch(LegoURL)).results; | |
var nums = []; | |
while(Responses.length>0){ | |
var last_id=""; | |
for(var i = 0; i<Responses.length; i++){ | |
var newNums = splitString(Responses[i].comment); | |
for(var j = 0; j<newNums.length; j++){ | |
if(newNums[j].num<7000) nums.push(newNums[j]); | |
} | |
last_id = Responses[i].id; | |
} | |
var newURL = LegoURL + "&max_id=" + last_id; | |
Responses = JSON.parse(UrlFetchApp.fetch(newURL)).results; | |
} | |
nums.sort(function(a,b){ | |
return a.num-b.num; | |
}); | |
var consolidated = []; | |
var lastNumAdded = 0; | |
for(var i = 0; i<nums.length; i++){ | |
var newNum = nums[i]; | |
if(newNum.num != lastNumAdded){ | |
consolidated.push(newNum); | |
}else{ | |
var oldNum = consolidated.pop(); | |
oldNum.freq += newNum.freq; | |
consolidated.push(oldNum); | |
} | |
lastNumAdded = newNum.num; | |
} | |
consolidated.sort(function(a,b){ | |
return b.freq-a.freq; | |
}); | |
var values = []; | |
for(var x = 0; x<consolidated.length; x++){ | |
values.push([consolidated[x].num,consolidated[x].freq]); | |
} | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
sheet.clear(); | |
sheet.appendRow(["Number", "Frequency", "The sheet has been fixed! All comments are being counted."]); | |
var range = sheet.getRange(2, 1, consolidated.length, 2); | |
range.setValues(values) | |
//SpreadsheetApp.getUi().alert(JSON.stringify(splitString("123as;ldkj98asdf123,98"))); | |
} | |
function splitString(input){ | |
var strs = input.split(/\D{1,}/); | |
var nums = []; | |
for(var i = 0; i<strs.length ; i++){ | |
var num = parseInt(strs[i]); | |
if(num) nums.push(num); | |
} | |
nums.sort(function(a,b){ | |
return a-b; | |
}); | |
var consolidated = []; | |
var lastAdded = 0; | |
for(var i = 0; i<nums.length; i++){ | |
var newNum = nums[i]; | |
if(newNum != lastAdded) consolidated.push( | |
{freq:1, | |
num:newNum}); | |
lastAdded = newNum; | |
} | |
return consolidated; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment