diff --git a/Apps Script/Code.js b/Apps Script/Code.js index ac868a2..65047d8 100644 --- a/Apps Script/Code.js +++ b/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(); @@ -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(); @@ -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 @@ -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({ @@ -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 + "'
"; - 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 + "'
"; + var linkToSearchResultsInGooglePlus = "https://plus.google.com/s/" + encodeURIComponent(SEARCH_QUERY) + "/top"; body+= linkToSearchResultsInGooglePlus+ "

"; body+= "If you have completed the tutorial, all those posts should be available here:
"; - body+= firebaseBaseUrl.replace("firebaseio.com", "firebaseapp.com"); + body+= FIREBASE_DB_URL.replace("firebaseio.com", "firebaseapp.com"); body+= "

New posts will also be automatically exported every day."; MailApp.sendEmail(currentUserEmailAddress, subject, body, {htmlBody: body}); } @@ -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."; } \ No newline at end of file