public

Ubiquity plugin - Create toodledo tasks via twitter

  • Download Gist
Ubiquity Command - toodledo via twitter.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
Cu.import("resource://ubiquity/modules/oauth.js");
 
// http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0update
// "Statuses over 140 characters will cause a 403 error from the API"
const TWITTER_STATUS_MAXLEN = 140;
 
 
CmdUtils.CreateCommand({
name: ["toodledo", "todo", "task", "do", "to do"],
icon: "http://toodledo.com/favicon.ico",
arguments: [{role: 'object', nountype: noun_arb_text, 'label':'task'},
{role: 'location', nountype: noun_arb_text, 'label':'context'},
{role: 'goal', nountype: noun_arb_text, label:'project'}
],
description: "Create a task in toodledo via your twitter account",
help: "You'll need a <a href=\"http://twitter.com\">Twitter account</a>, obviously. If you're not already logged in" +
" you'll be asked to log in.",
preview: function(previewBlock, args) {
// these are converted in the Twitter database anyway, and counted as 4 characters
var statusText = args.object.text
.replace("<", "&lt;")
.replace(">", "&gt;");
var previewTemplate = "Create a new task in toodledo: <br /><b>${status}</b> ${context} ${project} <br /><br />Characters remaining: <b>${chars}</b>";
var truncateTemplate = "<br />The last <b>${truncate}</b> characters will be truncated!";
var previewData = {
status: statusText,
chars: TWITTER_STATUS_MAXLEN - statusText.length
};
previewData['context'] = previewData['project'] = '';
if (args.location && args.location.text){
previewData['context']='<i>@'+args.location.text+'<i>';
}
if (args.goal && args.goal.text){
previewData['project']='<i>*'+args.goal.text+'</i>';
}
 
var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
 
if(previewData.chars < 0) {
var truncateData = {
truncate: 0 - previewData.chars
};
 
previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
}
 
previewBlock.innerHTML = previewHTML;
},
execute: function twitter_execute({object: {text}, alias, location, goal}) {
var me = this;
if (!text) return me._show(_("requires a status to be entered"));
var statusText = text;
//statusText = 'd toodledo '+statusText;
if (location && location.text){
statusText =statusText +' @'+ location.text;
}
if (goal && goal.text){
statusText =statusText + ' *'+ goal.text;
}
 
var login = Bin.twitterLastLogin() || {};
me._auth(login, function twitter_tweet(username, key, secret) {
me._post({
url: "http://api.twitter.com/1/direct_messages/new.json",
data: {'text': statusText = statusText.slice(0, TWITTER_STATUS_MAXLEN), 'user': 'toodledo'},
dataType: "json",
success: function twitter_success(res) {
me._show(
text, username
);
},
error: function twitter_error(xhr) {
me._show(_("error - status not updated") + " / " +
xhr.status + " " + xhr.statusText,
username);
},
}, {token: key, tokenSecret: secret});
Bin.twitterLastLogin({username: username, password: "dummy"});
});
},
 
_show: function twitter_show(text, user, cb) {
var title = this.name;
if (user) title += " \u2013 " + user;
displayMessage({icon: this.icon, title: title, text: text, onclick: cb});
},
_post: function twitter_post(settings, accessor) {
settings.type = "POST";
accessor.consumerKey = "C6h2HUUjmOcqXTtPRYqAVg";
accessor.consumerSecret = "AYNHPfkpm5lL3uPKXRCuzGFYItA8EOWlrkajyEBOd6s";
return $.ajax(OAuth.completeAjaxSettings(settings, accessor));
},
_auth: function twitter_auth({username, password}, cb) {
username = username && username.toLowerCase();
for each (let saved in CmdUtils.retrieveLogins("TwitterOAuth")) {
if (saved.username !== username) continue;
let [key, secret] = saved.password.split(" ");
return cb.call(this, username, key, secret);
}
const APIURL = "https://api.twitter.com/oauth/access_token";
if (!username || !password) {
let un = {value: username || ""};
let pw = {value: password || ""};
let ok = Utils.PromptService.promptUsernameAndPassword(
context.chromeWindow, this.name, APIURL, un, pw, null, {});
if (!ok || !un.value || !pw.value) return;
username = un.value.toLowerCase();
password = pw.value;
}
var me = this;
this._post({
url: APIURL,
data: {
x_auth_mode: "client_auth",
x_auth_username: username,
x_auth_password: password,
},
success: function twitter_xAuth_success(res) {
var {oauth_token, oauth_token_secret} = Utils.urlToParams(res);
CmdUtils.savePassword({
name: "TwitterOAuth",
username: username,
password: oauth_token + " " + oauth_token_secret,
});
cb.call(me, username, oauth_token, oauth_token_secret);
},
error: function twitter_xAuth_error(xhr) {
var status = xhr.status + " " + xhr.statusText;
Cu.reportError("Twitter xAuth for " + username + ": " + status);
me._show(status, username);
},
}, {});
},
 
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.