Created
March 14, 2014 21:32
-
-
Save tiffani/9557440 to your computer and use it in GitHub Desktop.
A way of transforming Google Form responses into Tumblr posts!
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
/* | |
Integrate with the Tumblr API (v2) to transform Google Form responses | |
into Tumblr posts! | |
Tumblr API on posting: http://www.tumblr.com/docs/en/api/v2#posting | |
To get started, you'll need to register this as an app in your Tumblr account. | |
Do this at http://www.tumblr.com/oauth/register. After that you'll get your | |
OAuth consumer and secret keys. Plug those into TUMBLR_KEY and TUMBLR_SECRET | |
below. | |
After an initial authorization step, you can post from Google Forms to Tumblr! | |
TO edit this script: open the Google Form editor and in the menu options, click | |
Tools > Script Editor. | |
Also remember to plug in the URL for your Tumblr account below. | |
tab. | |
*/ | |
function postToTumblr(ideaData) { | |
var TUMBLR_KEY = "your-tumblr-key"; | |
var TUMBLR_SECRET = "your-super-secret-tumblr-key"; | |
var oauthConfig = UrlFetchApp.addOAuthService("tumblr"); | |
oauthConfig.setAccessTokenUrl("http://www.tumblr.com/oauth/access_token"); | |
oauthConfig.setRequestTokenUrl("http://www.tumblr.com/oauth/request_token"); | |
oauthConfig.setAuthorizationUrl("http://www.tumblr.com/oauth/authorize"); | |
oauthConfig.setConsumerKey(TUMBLR_KEY); | |
oauthConfig.setConsumerSecret(TUMBLR_SECRET); | |
var responseCounter = FormApp.getActiveForm().getResponses().length + 1; | |
var postTitle = "Idea #" + responseCounter; | |
var postBody = ideaData.description; | |
/* If there's a larger, more interesting story around the idea, that gets posted, too! */ | |
if(ideaData.story) { | |
postBody = postBody.concat("<br /><br /><p><strong>A more detailed description...</strong><br />", ideaData.story, "</p>"); | |
} | |
/* If whoever proposed the idea has been entered on the form, we'll include | |
that in the Tumblr post! */ | |
if(ideaData.proposer) { | |
postBody = postBody + "<p><strong>Who proposed this?</strong><br />"; | |
if(ideaData.proposerContact != undefined && ideaData.proposerContact.length > 0) { | |
postBody = postBody + "<a href=\"mailto:" + ideaData.proposerContact + "\">" + ideaData.proposer + "</a></p>"; | |
} else { | |
postBody = postBody + ideaData.proposer + "</p>"; | |
} | |
} | |
if(ideaData.similarTools) { | |
postBody = postBody.concat("<p><strong>Are there similar tools?</strong><br />", ideaData.similarTools, "</p>"); | |
} | |
var ideaPost = { | |
"body" : postBody, | |
"tags" : ideaData.tags, | |
"type" : "text", /* Text post by default. */ | |
"title" : postTitle | |
}; | |
if(ideaData.source) { | |
var caption = "".concat("<strong>", postTitle, "</strong>", "<p>", postBody, "</p>"); | |
ideaPost['type'] = 'photo'; | |
ideaPost['caption'] = caption; | |
ideaPost['source'] = ideaData.source; | |
ideaPost['link'] = ideaData.source; | |
} | |
var requestData = { | |
"method": "POST", | |
"oAuthServiceName": "tumblr", | |
"oAuthUseToken": "always", | |
"payload" : ideaPost | |
}; | |
try { | |
/* Change "your-tumblr-url" to whatever that is. */ | |
var response = UrlFetchApp.fetch("http://api.tumblr.com/v2/blog/your-tumblr-url.tumblr.com/post", requestData); | |
Logger.log(response); | |
} catch(exception) { | |
Logger.log(exception); | |
} | |
} | |
function onFormSubmit(e) { | |
var resp = e.response.getItemResponses(); | |
var idea = []; | |
/* What's the general description of this idea? */ | |
idea['description'] = resp[0].getResponse(); | |
/* Tags! These correspond to different government agencies, departments, and constituent areas. | |
These end up getting translated to Tumblr tags on posts. */ | |
idea['tags'] = resp[2].getResponse() + ',' + resp[3].getResponse() + ',' + resp[4].getResponse(); | |
/* Is there a link to an image representing this idea? It goes here. */ | |
if(resp[1].getResponse().length > 0) { | |
idea['source'] = resp[1].getResponse(); | |
} | |
/* Is there a more extended story behind the idea? */ | |
if(resp[5].getResponse().length > 0) { | |
idea['story'] = resp[5].getResponse(); | |
} | |
/* Who proposed this idea? */ | |
if(resp[6].getResponse().length > 0) { | |
idea['proposer'] = resp[6].getResponse(); | |
} | |
/* What's the email address of who proposed the idea? */ | |
if(resp[7].getResponse().length > 0) { | |
idea['proposerContact'] = resp[7].getResponse(); | |
} | |
/* Are there any similar tools out there (including Code for America stuff)? */ | |
if(resp[8].getResponse().length > 0) { | |
idea['similarTools'] = resp[8].getResponse(); | |
} | |
postToTumblr(idea); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment