-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotes.gs
43 lines (40 loc) · 1.3 KB
/
quotes.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var url = "https://graph.facebook.com/v2.5/CytatyNauczycieliZVLoWGdansku/feed?access_token="+access_token+"&limit=100";
var quotesSheet = SpreadsheetApp.openById("12EDue1V28Tr1AUtiJkmpd9WKP7A5cABCWgkil6VZw30").getSheets()[0];
function update() {
var resp = UrlFetchApp.fetch(encodeURI(url));
if(resp.getResponseCode()!=200) {
Logger.log("Couldn't connect to facebook graph api");
return;
}
var quotes = JSON.parse(resp.getContentText());
for(var quoteIndex in quotes.data) {
var quote = quotes.data[quoteIndex];
if(!isQuotePresent(quote.id)) {
quotesSheet.appendRow([quote.message, quote.created_time, quote.id, false]);
} else {
//If quote was found that means it's all up to date.
return;
}
}
}
function isQuotePresent(id) {
var values = quotesSheet.getRange(1, 1, quotesSheet.getLastRow(),3).getValues();
for(var i=values.length-1;i>0;i--) {
if(values[i][2] == id) {
return true;
}
}
return false;
}
function getRandomQuote() {
var row = [null,null,null,true];
while(row[3]) {
row = quotesSheet.getRange(Math.floor(Math.random()*(quotesSheet.getLastRow()-2))+2, 1, 1, 4).getValues()[0];
}
var response = {update: new Date().getTime(), quote: {
message: row[0],
time: row[1],
id: row[2]
}};
return JSON.stringify(response);
}