Permalink
Browse files

Get Google Plus Posts

  • Loading branch information...
1 parent 110b55f commit a945b11c6d144df38e6310e48630ec29e0c57bf8 @timdream committed May 19, 2013
Showing with 176 additions and 2 deletions.
  1. +2 −0 assets/start.js
  2. +1 −0 assets/vars-sample.js
  3. +158 −0 assets/wordcloud-app.js
  4. +2 −0 go2-redirect.html
  5. +13 −2 index.html
View
@@ -13,6 +13,7 @@
sourceDialogView.addPanel(new FilePanelView());
sourceDialogView.addPanel(new WikipediaPanelView());
sourceDialogView.addPanel(new FacebookPanelView());
+ sourceDialogView.addPanel(new GooglePlusPanelView());
sourceDialogView.addPanel(new FeedPanelView());
sourceDialogView.addPanel(new FeedPanelView({
@@ -70,4 +71,5 @@
app.addFetcher(new FeedFetcher());
app.addFetcher(new WikipediaFetcher());
app.addFetcher(new FacebookFetcher());
+ app.addFetcher(new GooglePlusFetcher());
})();
@@ -1 +1,2 @@
var FACEBOOK_APP_ID = '';
+var GOOGLE_CLIENT_ID = '';
@@ -935,6 +935,114 @@ FacebookPanelView.prototype.submit = function fbpv_submit() {
'#facebook:' + this.facebookResponse.authResponse.userID);
};
+var GooglePlusPanelView = function GooglePlusPanelView(opts) {
+ this.load(opts, {
+ name: 'googleplus',
+ element: 'wc-panel-googleplus',
+ statusElement: 'wc-panel-googleplus-status',
+ idElement: 'wc-panel-googleplus-id'
+ });
+ this.loaded = false;
+};
+GooglePlusPanelView.prototype = new PanelView();
+GooglePlusPanelView.prototype.LABEL_LOGGED_IN = 'google-ready';
+GooglePlusPanelView.prototype.LABEL_NOT_LOGGED_IN = 'google-start-to-login';
+GooglePlusPanelView.prototype.beforeShow = function gppv_beforeShow() {
+ if (!GOOGLE_CLIENT_ID)
+ throw 'No GOOGLE_CLIENT_ID defined.';
+
+ if (this.loaded)
+ return;
+
+ this.loaded = true;
+
+ var el = document.createElement('script');
+ el.src = './assets/go2/src/google-oauth2.js';
+ el.onload = el.onerror = (function go2load() {
+ el.onload = el.onerror = null;
+
+ if (!window.GO2) {
+ this.loaded = false;
+ return;
+ }
+
+ var redirectUri = window.GO2_REDIRECT_URI ||
+ document.location.href.replace(/\/(index.html)?(#.*)?$/i,
+ '/go2-redirect.html');
+
+ GO2.init({
+ client_id: GOOGLE_CLIENT_ID,
+ scope: this.GOOGLE_API_SCOPE || '',
+ redirect_uri: redirectUri
+ });
+
+ GO2.login(false, true);
+
+ // Update UI for the first time, as we might not
+ // be able to log-in quietly.
+ this.updateUI();
+
+ GO2.onlogin = (function go2_onlogin(token) {
+ this.accessToken = token;
+ this.updateUI();
+
+ if (this.submitted) {
+ this.submitted = false;
+
+ // XXX: There is no way to cancel the login pop-up midway if
+ // the user navigates away from the panel (or the source dialog).
+ // We shall do some checking here to avoid accidently switches the UI.
+ if (this.element.hidden || this.dialog.element.hidden)
+ return;
+
+ this.realSubmit();
+ }
+ }).bind(this);
+
+ GO2.onlogout = (function go2_onlogout() {
+ this.accessToken = '';
+ this.updateUI();
+ }).bind(this);
+ }).bind(this);
+
+ document.documentElement.firstElementChild.appendChild(el);
+};
+GooglePlusPanelView.prototype.isReadyForFetch =
+ function gppv_isReadyForFetch() {
+ return !!this.accessToken;
+ };
+GooglePlusPanelView.prototype.updateUI = function gppv_updateUI() {
+ // XXX: l10n
+ if (this.isReadyForFetch()) {
+ this.statusElement.textContent = this.LABEL_LOGGED_IN;
+ } else {
+ this.statusElement.textContent = this.LABEL_NOT_LOGGED_IN;
+ }
+};
+GooglePlusPanelView.prototype.submit = function gppv_submit() {
+ if (!window.GO2 || !this.loaded)
+ return;
+
+ if (!this.isReadyForFetch()) {
+ this.submitted = true;
+ GO2.login(true, false);
+
+ return;
+ }
+
+ this.realSubmit();
+};
+GooglePlusPanelView.prototype.realSubmit = function gppv_realSubmit() {
+ var id = this.idElement.value;
+ if (!id)
+ id = 'me';
+
+ // Remove everything after the first slash.
+ id = id.replace(/\/.*$/, '');
+
+ this.dialog.submit('#googleplus:' + id);
+};
+
var Fetcher = function Fetcher() { };
Fetcher.prototype.LABEL_VERB = LoadingView.prototype.LABEL_LOADING;
@@ -1165,6 +1273,56 @@ WikipediaFetcher.prototype.handleResponse = function wf_handleResponse(res) {
this.app.handleData(text);
};
+var GooglePlusFetcher = function GooglePlusFetcher(opts) {
+ this.types = ['googleplus'];
+
+ this.params = [
+ ['maxResults', '100'],
+ ['alt', 'json'],
+ ['pp', '1']
+ ];
+};
+GooglePlusFetcher.prototype = new JSONPFetcher();
+GooglePlusFetcher.prototype.GOOGLE_PLUS_API_URL =
+ 'https://www.googleapis.com/plus/v1/people/%source/activities/public';
+GooglePlusFetcher.prototype.POST_REGEXP =
+ /<[^>]+?>|\(.+?\.\.\.\)|\&\w+\;|<script.+?\/script\>/ig;
+GooglePlusFetcher.prototype.getData = function gpf_getData(dataType, data) {
+ var googlePlusPanelView =
+ this.app.views['source-dialog'].panels['googleplus'];
+ var accessToken = googlePlusPanelView.accessToken;
+
+ if (!accessToken) {
+ // XXX: can we login user from here?
+ // User would lost the id kept in hash here.
+ this.app.reset();
+ this.app.views['source-dialog'].showPanel(googlePlusPanelView);
+ return;
+ }
+
+ var params = [].concat(this.params);
+ params.push(['access_token', accessToken]);
+
+ var url = this.GOOGLE_PLUS_API_URL.replace(/%source/, data) + '?' +
+ params.map(function kv(param) {
+ return param[0] + '=' + encodeURIComponent(param[1]);
+ }).join('&');
+
+ this.requestData(url);
+};
+GooglePlusFetcher.prototype.handleResponse = function gpf_handleResponse(res) {
+ if (res.error || !res.items) {
+ this.app.handleData('');
+ return;
+ }
+
+ var text = res.items.map((function gpf_map(item) {
+ return item.object.content.replace(this.POST_REGEXP, '');
+ }).bind(this)).join('');
+
+ this.app.handleData(text);
+};
+
var FacebookFetcher = function FacebookFetcher() {
this.types = ['facebook'];
};
View
@@ -0,0 +1,2 @@
+<script defer type="text/javascript"
+ src="./assets/go2/src/google-oauth2.js"></script>
View
@@ -12,8 +12,6 @@
<script defer type="text/javascript"
src="./assets/vars.js"></script>
<script defer type="text/javascript"
- src="./assets/go2/src/google-oauth2.js"></script>
- <script defer type="text/javascript"
src="./assets/wordfreq/src/wordfreq.js"></script>
<script defer type="text/javascript"
src="./assets/wordcloud2.js/src/wordcloud2.js"></script>
@@ -71,6 +69,9 @@
<a href="#" data-panel="feed">RSS Feed</a></li>
<li hidden>
+ <a href="#" data-panel="googleplus">Google+</a></li>
+
+ <li hidden>
<a href="#" data-panel="cp">Copy &amp; Paste</a></li>
<li hidden>
<a href="#" data-panel="file">File</a></li>
@@ -174,6 +175,16 @@
<p id="wc-panel-facebook-status">...</p>
</form>
+ <form id="wc-panel-googleplus" hidden>
+ <label for="wc-panel-googleplus-id">Profile ID</label>
+ <div class="input-prepend">
+ <span class="add-on">https://plus.google.com/</span>
+ <input type="text" id="wc-panel-googleplus-id" class="input-medium" placeholder="me" />
+ </div>
+ <span class="help-block">Find the ID by copying it from the URL. Leave it empty to access your own profile.</span>
+ <p id="wc-panel-googleplus-status">...</p>
+ </form>
+
</div>
</div>
</div>

0 comments on commit a945b11

Please sign in to comment.