Post-to-Tumblr class [complete + uploaded to OpenProcessing]
in
Library and Tool Development
•
2 years ago
24th Aug 2011 - TumblrP5 class now supports image data uploads, along with all other posts except video and audio data uploads. Available for download from
http://openprocessing.org/visuals/?visualID=34381
Hope it's useful to somebody :)
-
At the moment it supports writing every type of post to tumblr, except data uploads. -
Photos/Videos/Audio can be posted from an online URL source -
Uses the API v1, so no Oauth required - just a username and a password
- /*
- ** TUMBLRWRITE CLASS 0.9
- ** Write posts to Tumblr from Processing
- ** Data uploads not yet supported
- **
- ** Henderson[Six] http://hendersonsix.com
- ** CC-BY-SA 2.0
- */
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- class TumblrWrite {
- String userEmail;
- String password;
- String data;
- String encType = "UTF-8"; //encode URLs according to UTF-8 format for tumblr
- //CONSTRUCTOR
- TumblrWrite(String _userEmail, String _password) throws IOException {
- userEmail = _userEmail;
- password = _password;
- //prepare data string for POST request with email + password (required for each post request)
- data = URLEncoder.encode("email", encType) + "=" + URLEncoder.encode(userEmail, encType);
- data += "&" + URLEncoder.encode("password", encType) + "=" + URLEncoder.encode(password, encType);
- }
- /****************** SET-UP POST METHODS *******************/
- //TEXT
- void textPost(String title, String body) throws IOException {
- System.out.println("Posting regular text...");
- data += "&" + URLEncoder.encode("type", encType) + "=" + URLEncoder.encode("regular", encType);
- data += "&" + URLEncoder.encode("title", encType) + "=" + URLEncoder.encode(title, encType);
- data += "&" + URLEncoder.encode("body", encType) + "=" + URLEncoder.encode(body, encType);
- }
- //PHOTO - Embeds a photo from a URL source
- void photoLinkPost(String caption, String url, String clickThroughUrl) throws IOException {
- System.out.println("Posting a photo link...");
- data += "&" + URLEncoder.encode("type", encType) + "=" + URLEncoder.encode("photo", encType);
- data += "&" + URLEncoder.encode("source", encType) + "=" + URLEncoder.encode(url, encType);
- data += "&" + URLEncoder.encode("caption", encType) + "=" + URLEncoder.encode(caption, encType);
- data += "&" + URLEncoder.encode("click-through-url", encType) + "=" + URLEncoder.encode(clickThroughUrl, encType);
- }
- //QUOTE
- void quotePost(String quote, String source) throws IOException {
- System.out.println("Posting a quote...");
- data += "&" + URLEncoder.encode("type", encType) + "=" + URLEncoder.encode("quote", encType);
- data += "&" + URLEncoder.encode("quote", encType) + "=" + URLEncoder.encode(quote, encType);
- data += "&" + URLEncoder.encode("source", encType) + "=" + URLEncoder.encode(source, encType);
- }
- //LINK
- void linkPost(String name, String url, String description) throws IOException {
- System.out.println("Posting a link...");
- data += "&" + URLEncoder.encode("type", encType) + "=" + URLEncoder.encode("link", encType);
- data += "&" + URLEncoder.encode("name", encType) + "=" + URLEncoder.encode(name, encType);
- data += "&" + URLEncoder.encode("url", encType) + "=" + URLEncoder.encode(url, encType);
- data += "&" + URLEncoder.encode("description", encType) + "=" + URLEncoder.encode(description, encType);
- }
- //CONVERSATION
- void conversationPost(String title, String conversation) throws IOException {
- System.out.println("Posting a conversation...");
- data += "&" + URLEncoder.encode("type", encType) + "=" + URLEncoder.encode("conversation", encType);
- data += "&" + URLEncoder.encode("title", encType) + "=" + URLEncoder.encode(title, encType);
- data += "&" + URLEncoder.encode("conversation", encType) + "=" + URLEncoder.encode(conversation, encType);
- }
- //VIDEO - Supply either a youtube URL or html embed code for url parameter
- void videoLinkPost(String title, String url, String caption) throws IOException {
- String.out.println("Posting an embedded video...");
- data += "&" + URLEncoder.encode("type", encType) + "=" + URLEncoder.encode("video", encType);
- data += "&" + URLEncoder.encode("source", encType) + "=" + URLEncoder.encode(url, encType);
- data += "&" + URLEncoder.encode("caption", encType) + "=" + URLEncoder.encode(caption, encType);
- }
- //AUDIO - Supply source url of audio file
- void audioLinkPost(String title, String url, String caption) throws IOException {
- String.out.println("Posting embedded audio...");
- data += "&" + URLEncoder.encode("type", encType) + "=" + URLEncoder.encode("audio", encType);
- data += "&" + URLEncoder.encode("externally-hosted-url", encType) + "=" + URLEncoder.encode(url, encType);
- data += "&" + URLEncoder.encode("caption", encType) + "=" + URLEncoder.encode(caption, encType);
- }
- /****************** COMMON POST METHODS ******************/
- //A short description of the application making the request for tracking and statistics, such as "John's Widget 1.0".
- //Must be 64 or fewer characters.
- void setGenerator(String generator) throws IOException {
- data += "&" + URLEncoder.encode("generator", encType) + "=" + URLEncoder.encode(generator, encType);
- }
- //The post date, if different from now, in the blog's timezone.
- //Most unambiguous formats are accepted, such as '2007-12-01 14:50:02'
- //Dates must not be in the future.
- void setDate(String date) throws IOException {
- data += "&" + URLEncoder.encode("date", encType) + "=" + URLEncoder.encode(date, encType);
- }
- //Whether the post is private.
- //Private posts only appear in the Dashboard or with authenticated links, and do not appear on the blog's main page.
- void setPrivacy(boolean privacy) throws IOException {
- if (privacy==true) {
- data += "&" + URLEncoder.encode("private", encType) + "=" + URLEncoder.encode("1", encType);
- }
- else {
- data += "&" + URLEncoder.encode("private", encType) + "=" + URLEncoder.encode("0", encType);
- }
- }
- // Comma-separated list of post tags.
- void setTags(String tags) throws IOException {
- data += "&" + URLEncoder.encode("tags", encType) + "=" + URLEncoder.encode(tags, encType);
- }
- //html or markdown
- void setFormat(String format) throws IOException {
- data += "&" + URLEncoder.encode("format", encType) + "=" + URLEncoder.encode(format, encType);
- }
- //Post this to a secondary blog on your account, e.g. mygroup.tumblr.com (for public groups only)
- void setGroup(String myGroup) throws IOException {
- data += "&" + URLEncoder.encode("group", encType) + "=" + URLEncoder.encode(myGroup, encType);
- }
- //A custom string to appear in the post's URL: myblog.tumblr.com/post/123456/this-string-right-here.
- //Max 55 characters
- void setSlug(String slug) throws IOException{
- data += "&" + URLEncoder.encode("slug", encType) + "=" + URLEncoder.encode(slug, encType);
- }
- // One of the following values: published, draft, submission, queue, publish-on=YYYY-MM-DDT13:34:00
- void setPostState(String state) throws IOException{
- data += "&" + URLEncoder.encode("state", encType) + "=" + URLEncoder.encode(state, encType);
- }
- //(optional, ignored on edits)
- //One of the following values, if the tumblelog has Twitter integration enabled:
- //no (default), auto (post summary), custom message
- void sendToTwitter(String state) throws IOException{
- data += "&" + URLEncoder.encode("send-to-twitter", encType) + "=" + URLEncoder.encode(state, encType);
- }
- /****************** SEND POST METHOD ******************/
- void sendPost() throws IOException {
- //set up http connection
- URL tumblr = new URL("http://tumblr.com/api/write");
- HttpURLConnection http = (HttpURLConnection) tumblr.openConnection();
- http.setDoOutput(true);
- http.setRequestMethod("POST");
- OutputStreamWriter out = new OutputStreamWriter(http.getOutputStream());
- //connect and write data to tumblr
- http.connect();
- out.write(data);
- out.flush();
- //get status of post request
- //400 - multiple errors
- //403 - incorrect username/password
- //201 - success! created!
- System.out.println(http.getResponseCode());
- System.out.println(http.getResponseMessage());
- //close OutputStreamWriter
- out.close();
- }
- }
2