Created
January 3, 2012 10:48
-
-
Save quietlynn/1554435 to your computer and use it in GitHub Desktop.
An unofficial API to get Google+ streams.
This file contains 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
/* | |
Google+ Stream API => An unofficial API to get Google+ streams. | |
Copyright (C) 2012 Jingqin Lynn | |
Based on the "An Unofficial Google+ Circle and Followers API for Google Chrome" poject | |
https://github.com/mohamedmansour/google-plus-extension-jsapi | |
by +Mohamed Mansour <https://github.com/mohamedmansour> | |
Under the license: https://github.com/mohamedmansour/google-plus-extension-jsapi/blob/master/LICENSE.md | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
// GSON = Google's irregular JSON | |
function parseGSON(raw) { | |
var jsonString = raw.substring(4).replace(/\[,/g, '[null,'); | |
jsonString = jsonString.replace(/,\]/g, ',null]'); | |
jsonString = jsonString.replace(/,,/g, ',null,'); | |
jsonString = jsonString.replace(/,,/g, ',null,'); | |
return JSON.parse(jsonString); | |
} | |
_parsePost = function(element) { | |
var item = {}; | |
item.type = element[2].toLowerCase(); | |
item.time = element[5]; | |
if (element[70]) { | |
item.time_edited = parseInt((element[70] + '').substring(0, (item.time + '').length)); | |
} | |
item.url = this._buildProfileURLFromItem(element[21]); | |
item.is_public = (element[32] == '1'); | |
item.owner = {}; | |
item.owner.name = element[3]; | |
item.owner.id = element[16]; | |
item.owner.image = this._fixImage(element[18]); | |
if (element[43]) { // Share? | |
item.share = {}; | |
item.share.name = element[43][0]; | |
item.share.id = element[43][1]; | |
item.share.image = this._fixImage(element[43][4]); | |
item.share.html = element[43][4]; | |
item.share.url = this._buildProfileURLFromItem(element[43][4]); | |
item.html = element[47]; | |
} | |
else { // Normal | |
item.html = element[4]; | |
} | |
// Parse hangout item. | |
if (element[2] == 'Hangout') { | |
var hangoutData = element[82][2][1][0]; | |
var hangoutURL = hangoutData[1]; | |
var hangoutID = hangoutData[0]; | |
var hangoutType = hangoutData[6]; | |
var isActive = hangoutURL == '' ? false : true; | |
// Skip this since it isn't a hangout. It is just youtube content. | |
if (isActive && hangoutID == '' && hangoutType == 2 /*normal*/) { | |
// Perhaps we want to deal with this later. | |
} | |
else { | |
item.owner.status = true; | |
item.data = {}; | |
item.data.url = hangoutURL; | |
item.data.type = hangoutType; | |
item.data.active = isActive; | |
item.data.id = hangoutID; | |
item.data.participants = []; | |
item.data.extra_data = hangoutData[13]; | |
var cachedOnlineUsers = {}; | |
var onlineParticipants = hangoutData[3]; | |
for (var i in onlineParticipants) { | |
var elt = onlineParticipants[i]; | |
var user = this._buildUserFromItem(elt[2], elt[0], elt[1], true); | |
cachedOnlineUsers[user.id] = true; | |
item.data.participants.push(user); | |
} | |
var offlineParticipants = hangoutData[4]; | |
for (var i in offlineParticipants) { | |
var elt = offlineParticipants[i]; | |
var user = this._buildUserFromItem(elt[2], elt[0], elt[1], false); | |
if (!cachedOnlineUsers[user.id]) { | |
item.data.participants.push(user); | |
} | |
} | |
} | |
} | |
return item; | |
}; | |
_fixImage = function(image) { | |
if (image.indexOf('https') == -1) { | |
image = 'https:' + image; | |
} | |
return image; | |
}; | |
_buildProfileURLFromItem = function(url) { | |
if (url.indexOf('https') == -1) { | |
url = 'http://plus.google.com/' + url; | |
} | |
return url; | |
}; | |
var pageTokens = new Array(); | |
pageTokens.push(null); | |
var _pageTokenSet = {}; | |
function getStream(circleId, page, callback, onerror) { | |
var format = "https://plus.google.com/_/stream/getactivities/?sp=%5B1%2C2%2Cnull%2C{circleId}%2Cnull%2C20%2Cnull%2C%22social.google.com%22%2C%5B%5D%5D%26rt%3Dj"; | |
var url = format.replace("{circleId}", circleId || "null") + "&_requid=" + new Date().getTime(); | |
if(page != null) { | |
var token = pageTokens[page]; | |
if(token) url += '&ct=' + token; | |
} | |
chrome._url = url; | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function test() { | |
if (xhr.readyState == XMLHttpRequest.DONE) { | |
if(xhr.status==200) { | |
callback(_parseStream(parseGSON(xhr.responseText))); | |
} | |
else { | |
if(onerror) onerror(xhr); | |
} | |
} | |
}; | |
xhr.open('GET', url, true); | |
xhr.overrideMimeType('application/json; charset=UTF-8'); | |
xhr.send(null); | |
} | |
function _parseStream(o) { | |
var posts = o[0][1][0]; | |
var page = o[0][1][1]; | |
if(!posts) { | |
posts = o[0][1][1]; | |
page = o[1][1][1]; | |
} | |
if(!_pageTokenSet[page]) { | |
_pageTokenSet[page] = true; | |
pageTokens.push(page); | |
} | |
var p = new Array(); | |
for(var i in posts) { | |
var post = _parsePost(posts[i]); | |
p.push(post); | |
} | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment