Skip to content

Instantly share code, notes, and snippets.

@printminion
Last active December 10, 2015 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save printminion/4503237 to your computer and use it in GitHub Desktop.
Save printminion/4503237 to your computer and use it in GitHub Desktop.
Google Plus Profile parser written in Google Apps Script
/**
* Script for parsing Google+ Profile Data
* @author Misha M.-Kupriyanov <m.kupriyanov@gmail.com>
* @license
*/
/**
* Test run method
*/
function fetchTest() {
var profile = getProfileById('104512463398531242371');
Logger.log('testRun:' + profile);
}
/**
* Get public Google+ profile data. Cached for 25 minutes
* @param {integer} Google+ Profile Id
* @return {array} profile object
*/
function getProfileById(userId) {
Logger.log('getProfile:' + userId);
if (!userId) {
throw 'Missed parameter: google+ id';
}
var html = null;
var url = 'https://plus.google.com/' + userId + '/about?hl=en';
var data = new Object();
try {
// var cache = CacheService.getPublicCache();
// var cacheKey = 123; //shortKeyHash_(userId);
// var html = cache.get(cacheKey);
// if (html == null) {
var response = UrlFetchApp.fetch(url);
var html = response.getContentText();
// cache.put(cacheKey, html, 1500); // cache for 25 minutes
// }
} catch(e) {
return;
}
var profile = {id: userId
, fullname: null
, name: null
, surname: null
, followers: null
, following: null
, status: ''
};
try {
//<span>11,686 have him in circles</span>
var filterBegin = "({key: '5', isError: false , data: ";
var filterEnd = '});</script>';
var response = html;
var begin = response.indexOf(filterBegin) + filterBegin.length;
response = response.substring(begin);
var end = response.indexOf(filterEnd);
response = response.substring(0, end);
//Logger.log('response:' + response);
data['followers'] = 'failed to parse';
data['following'] = 'failed to parse';
var gjson = toJSON_(response);
var obj = Utilities.jsonParse(gjson);
//Logger.log('obj:' + obj);
//extend parsed data
data = parseProfileData_(data, obj)
profile.fullname = data.fullname;
profile.name = data.name;
profile.surname = data.surname;
profile.followers = data.followers;
profile.following = data.following;
profile.status = data.status;
} catch(e) {
Logger.log('exception:' + e);
return profile;
}
return profile;
}
function parseProfileData_(data, profile_gjson) {
var following, followers = 'hidden';
data['id'] = 'hidden';
data['fullname'] = 'hidden';
data['name'] = 'hidden';
data['surname'] = 'hidden';
data['followers'] = 'hidden';
data['following'] = 'hidden';
try {
data['fullname'] = profile_gjson[2][4][3];
} catch(e) {}
try {
data['name'] = profile_gjson[2][4][1];
} catch(e) {}
try {
data['surname'] = profile_gjson[2][4][2];
} catch(e) {}
try {
following = profile_gjson[3][0][0];
} catch(e) {}
try {
followers = profile_gjson[3][2][0];
} catch(e) {}
try {
data['id'] = profile_gjson[0];
data['followers'] = followers;
data['following'] = following;
data['status'] = 'success';
} catch(e) {
Logger.log('exception:' + e);
data['status'] = 'parse error';
}
return data
}
function shortKeyHash_(input) {
return Utilities.base64Encode( Utilities.computeDigest( Utilities.DigestAlgorithm.SHA_1, input));
}
/**
* convert Google JSON format to Normal JSON
*/
function toJSON_(str) {
str = str.replace(")]}'\n", "");
str = str.replace(",,", ",null,");
str = str.replace(",,", ",null,");
str = str.replace("[,", "[null,");
var match = str.match(/{([^"]+):/);
while (match) {
str = str.replace(match[0], '{"' + match[1] + '":');
match = str.match(/{([^"]+):/);
}
str = str.replace("[]", "null");
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment