Created
March 1, 2012 15:29
-
-
Save lleixat/1950525 to your computer and use it in GitHub Desktop.
Google Plus Toolkit
This file contains hidden or 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 Plus Toolkit | |
| Kevin Wang (kevixw'At'gmail.com) | |
| Copyright (c) 2012 . All rights reserved. | |
| Based on : | |
| 1. Google+ Stream API => An unofficial API to get Google+ streams. | |
| https://gist.github.com/1554435 | |
| Copyright (C) 2012 Jingqin Lynn | |
| 2. An Unofficial Google+ Circle and Followers API for Google Chrome | |
| 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/>. | |
| */ | |
| /* | |
| Google Plus Toolkit * alpha | |
| build 1/7/2012 by Kevin | |
| This library is used to enhance the official / unofficial JS API | |
| Sample: | |
| var gpt = new GPT(GSON); | |
| GPT.when('new', function () { alert('A new activity has reached'); }); | |
| */ | |
| var GPT = (function () { | |
| // global private start -------- | |
| // global fields | |
| var $$stream = [], // stream flow | |
| $$people = [], // people's profile collection | |
| $$comment = [], // comment collection | |
| $$circle = [], // circle | |
| $$schedule = {}; // schedule -> 'when' | |
| // shallow copy | |
| function __extend($target, $options) { | |
| for (name in $options) | |
| $target[name] = $options[name]; | |
| return $target; | |
| } | |
| // global private end --------- | |
| // a special object type | |
| var $$when = function ($arg) { | |
| var _self = this; | |
| __extend(_self, { | |
| // triger when the condition is meet | |
| triger : function ($arg) { | |
| } | |
| }); | |
| // build new 'when' | |
| function _build ($arg) { | |
| // register the event in $$schedule | |
| return _self; | |
| } | |
| return _build($arg); | |
| } | |
| // constructor | |
| var $$c = function ($arg) { | |
| var $data_index=null, // the index of current selected item's data | |
| $self=this; // pointer of this | |
| // instance public members | |
| __extend(this, { | |
| // these two variables is required for every kind of information | |
| kind : null, | |
| id : null, | |
| // get specific data item from current selected activity on comment | |
| get : function ($arg) { | |
| if ($data_index) | |
| return $$stream[$data_index]; | |
| else | |
| return null; | |
| }, | |
| // select specific item | |
| select : function ($arg) { | |
| //return $$c.select($arg); | |
| } | |
| }); | |
| // instance private members | |
| return $$c.build($arg); | |
| }; | |
| return __extend($$c, { | |
| // public stastic | |
| // current is stream flowing | |
| // 0 - freezon, 1 - flowing | |
| status : 0, | |
| // start the stream querying | |
| flow : function ($arg) { | |
| $$c.status = 1; | |
| }, | |
| // stop the stream querying | |
| freeze : function ($arg) { | |
| $$c.status = 0; | |
| }, | |
| // triger the $func when $cmd | |
| when : function ($arg) { | |
| return new $$when($arg); | |
| }, | |
| // build a new Google Plus Stream object | |
| build : function ($arg) { | |
| var t = typeof($arg); | |
| // return an empty when the $arg is null or '' | |
| if (!$arg && $arg != 0) { | |
| return new $$c(); | |
| } | |
| else if ($arg instanceof $$c) { | |
| // if this a Google Plus Stream instance | |
| return $arg; | |
| } | |
| else if (t=='string') { | |
| // if this is a GSON, prase it | |
| if (/^\[((('[^']*')|null)*,)*\]$/.test($arg)) | |
| $$c.parseGSON($arg); | |
| } | |
| }, | |
| // list all activities from stream | |
| list : function ($arg) { | |
| }, | |
| // get specific data from stream | |
| get : function ($arg) { | |
| }, | |
| // put / post information to stream | |
| put : function ($arg) { | |
| }, | |
| // select specific data from stream | |
| select : function ($arg, $from) { | |
| // return an empty object when the specific is not exist | |
| if (!$$c.isExist($arg)) | |
| return $$c.build(null); | |
| if ($from) | |
| return $$c.build($from).select($arg); | |
| }, | |
| isExist : function ($arg) { | |
| return true; | |
| }, | |
| // parse Google's irregular JSON | |
| // do not push the JSON into stream when $preventPush is true | |
| parseGSON : function ($raw, $preventPush) { | |
| var jsonString = $raw.substring(4).replace(/\[,/g, '[null,'); | |
| jsonString = jsonString.replace(/,\]/g, ',null]'); | |
| jsonString = jsonString.replace(/,,/g, ',null,'); | |
| jsonString = jsonString.replace(/,,/g, ',null,'); | |
| var json = JSON.parse(jsonString); | |
| if ($preventPush) | |
| $$c.parseJSON(json); | |
| return json; | |
| }, | |
| // read stream from JSON | |
| // return parsed stream | |
| parseJSON : function ($json) { | |
| //return $json; | |
| } | |
| }); | |
| })(); | |
| _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