Skip to content

Commit

Permalink
Use less UrlFetch quota
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainVialard committed Jan 2, 2019
1 parent 64d7244 commit 83cd8aa
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions Apps Script/Code.js
@@ -1,11 +1,12 @@
function getAllPosts() {
var firebaseBaseUrl = "https://apps-script-community-archive.firebaseio.com/";
var communityId = "102471985047225101769";
var searchQuery = "community:" + communityId;
// you can use as the searchQuery most advanced search features listed here:
// https://support.google.com/plus/answer/1669519
// but "from:me" is not working, you need your Google+ profile ID, eg: "from:116263732197316259248 NOT in:community"
var EXPORT_NAME = "My G+ Export";
var FIREBASE_DB_URL = "https://apps-script-community-archive.firebaseio.com/";
var COMMUNITY_ID = "102471985047225101769";
var SEARCH_QUERY = "community:" + COMMUNITY_ID;
// you can use as the SEARCH_QUERY most advanced search features listed here:
// https://support.google.com/plus/answer/1669519
// but "from:me" is not working, you need your Google+ profile ID, eg: "from:116263732197316259248 NOT in:community"

function getAllPosts() {
var today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd");
var startTime = Date.now();

Expand All @@ -27,6 +28,8 @@ function getAllPosts() {
console.log("Trigger created");
// Save when we started the backup, this will be useful to keep syncing new posts once the whole content has been retrieved
scriptProperties.setProperty("lastSyncDate", today);
// Save export name in Firebase
renameExport();
}

var properties = scriptProperties.getProperties();
Expand All @@ -36,22 +39,22 @@ function getAllPosts() {
var onlySyncNewPosts = properties["onlySyncNewPosts"] || false;
if (onlySyncNewPosts) {
// full export done, simply retrieve the latest posts
searchQuery+= " after:" + lastSyncDate;
SEARCH_QUERY+= " after:" + lastSyncDate;
}

var token = ScriptApp.getOAuthToken();
var fb = FirebaseApp.getDatabaseByUrl(firebaseBaseUrl, token);
var fb = FirebaseApp.getDatabaseByUrl(FIREBASE_DB_URL, token);

do {
var activityFeed = Plus.Activities.search(searchQuery, {maxResults: 20, pageToken: nextPageToken});
var activityFeed = Plus.Activities.search(SEARCH_QUERY, {maxResults: 20, pageToken: nextPageToken});
var posts = activityFeed.items;
nextPageToken = activityFeed.nextPageToken;
if (!posts.length) nextPageToken = null;
var postData = {};

for (var i in posts) {
nbOfPostsRetrieved++;
var postId = posts[i].id;
var postData = {};
postData["posts/" + postId] = posts[i];

// retrieve comments
Expand Down Expand Up @@ -106,10 +109,9 @@ function getAllPosts() {
}
}
*/

// Firebase multi-location updates
fb.updateData('', postData);
}
// Firebase multi-location updates - write up to 20 posts in 1 call, along with comments and plusoners
fb.updateData('', postData);

ErrorHandler.expBackoff(function(){
scriptProperties.setProperties({
Expand All @@ -134,11 +136,11 @@ function getAllPosts() {
var nbOfPosts = Object.keys(posts).length;
var currentUserEmailAddress = Session.getEffectiveUser();
var subject = "Google+ exporter to Firebase - migration completed!";
var body = nbOfPosts + " posts have been exported, matching the following G+ search query: '" + searchQuery + "'<br>";
var linkToSearchResultsInGooglePlus = "https://plus.google.com/s/" + encodeURIComponent(searchQuery) + "/top";
var body = nbOfPosts + " posts have been exported, matching the following G+ search query: '" + SEARCH_QUERY + "'<br>";
var linkToSearchResultsInGooglePlus = "https://plus.google.com/s/" + encodeURIComponent(SEARCH_QUERY) + "/top";
body+= linkToSearchResultsInGooglePlus+ "<br><br>";
body+= "If you have completed the tutorial, all those posts should be available here:<br>";
body+= firebaseBaseUrl.replace("firebaseio.com", "firebaseapp.com");
body+= FIREBASE_DB_URL.replace("firebaseio.com", "firebaseapp.com");
body+= "<br><br>New posts will also be automatically exported every day.";
MailApp.sendEmail(currentUserEmailAddress, subject, body, {htmlBody: body});
}
Expand All @@ -147,4 +149,19 @@ function getAllPosts() {
scriptProperties.setProperty("lastSyncDate", today);
}
}
}

// Change the name of the export, displayed in the web app header
function renameExport() {
var token = ScriptApp.getOAuthToken();
var fb = FirebaseApp.getDatabaseByUrl(FIREBASE_DB_URL, token);
fb.setData('siteTitle', EXPORT_NAME);
}

// throw in the Apps Script editor UI the total number of posts recorded in Firebase
function listNbOfPostsStoredInFirebase() {
var token = ScriptApp.getOAuthToken();
var fb = FirebaseApp.getDatabaseByUrl(FIREBASE_DB_URL, token);
var posts = fb.getData('posts', {shallow: true});
throw "Currently " + Object.keys(posts).length + " posts stored in Firebase.";
}

0 comments on commit 83cd8aa

Please sign in to comment.