Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Mobypicture, Twipl, Posterous Support.
Browse files Browse the repository at this point in the history
  • Loading branch information
withgod committed Jan 24, 2011
1 parent 1d014d1 commit 539ab5c
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public ImageUploaderFactory(Configuration conf) {
defaultMediaProvider = TWITPIC;
} else if ("yfrog".equals(mediaProvider)) {
defaultMediaProvider = YFROG;
} else if ("mobypicture".equals(mediaProvider)) {
defaultMediaProvider = MOBYPICTURE;
} else if ("twipl".equals(mediaProvider)) {
defaultMediaProvider = TWIPL;
} else if ("posterous".equals(mediaProvider)) {
defaultMediaProvider = POSTEROUS;
} else {
throw new IllegalArgumentException("unsupported media provider:" + mediaProvider);
}
Expand Down Expand Up @@ -126,6 +132,12 @@ public ImageUpload getInstance(MediaProvider mediaProvider, Authorization author
return new TwitpicUpload(conf, apiKey, oauth);
} else if (mediaProvider == YFROG) {
return new YFrogUpload(conf, oauth);
} else if (mediaProvider == MOBYPICTURE) {
return new MobypictureUpload(conf, apiKey, oauth);
} else if (mediaProvider == TWIPL) {
return new TwiplUpload(conf, apiKey, oauth);
} else if (mediaProvider == POSTEROUS) {
return new PosterousUpload(conf, oauth);
} else {
throw new AssertionError("Unknown provider");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class MediaProvider implements java.io.Serializable {
public static MediaProvider TWITGOO= new MediaProvider("TWITGOO");
public static MediaProvider TWITPIC = new MediaProvider("TWITPIC");
public static MediaProvider YFROG = new MediaProvider("YFROG");
public static MediaProvider MOBYPICTURE = new MediaProvider("MOBYPICTURE");
public static MediaProvider TWIPL= new MediaProvider("TWIPL");
public static MediaProvider POSTEROUS = new MediaProvider("POSTEROUS");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright (c) 2007-2011, Yusuke Yamamoto
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Yusuke Yamamoto nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package twitter4j.media;

import twitter4j.TwitterException;
import twitter4j.conf.Configuration;
import twitter4j.http.OAuthAuthorization;
import twitter4j.internal.http.HttpParameter;
import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;

/**
* @see <a href="http://developers.mobypicture.com/documentation/">mobyapi documentation</a>
* @author withgod - noname at withgod.jp
* @since Twitter4J 2.1.12
*/
class MobypictureUpload extends AbstractImageUploadImpl {

public MobypictureUpload(Configuration conf, String apiKey, OAuthAuthorization oauth) {
super(conf, apiKey, oauth);
}

@Override
protected String postUpload() throws TwitterException {
int statusCode = httpResponse.getStatusCode();
if (statusCode != 200)
throw new TwitterException("Mobypic image upload returned invalid status code", httpResponse);

String response = httpResponse.asString();

try {
JSONObject json = new JSONObject(response);
if (!json.isNull("media")) {
return json.getJSONObject("media").getString("mediaurl");
}
} catch (JSONException e) {
throw new TwitterException("Invalid Mobypic response: " + response, e);
}

throw new TwitterException("Unknown Mobypic response", httpResponse);
}

@Override
protected void preUpload() throws TwitterException {
uploadUrl = "https://api.mobypicture.com/2.0/upload.json";
String verifyCredentialsAuthorizationHeader = generateVerifyCredentialsAuthorizationHeader(TWITTER_VERIFY_CREDENTIALS_JSON);

headers.put("X-Auth-Service-Provider", TWITTER_VERIFY_CREDENTIALS_JSON);
headers.put("X-Verify-Credentials-Authorization", verifyCredentialsAuthorizationHeader);

if (null == apiKey) {
throw new IllegalStateException("No API Key for Mobypic specified. put media.providerAPIKey in twitter4j.properties.");
}
HttpParameter[] params = {
new HttpParameter("key", apiKey),
this.image};
if (message != null) {
params = appendHttpParameters(new HttpParameter[]{
this.message
}, params);
}
this.postParameter = params;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright (c) 2007-2011, Yusuke Yamamoto
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Yusuke Yamamoto nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package twitter4j.media;

import twitter4j.TwitterException;
import twitter4j.conf.Configuration;
import twitter4j.http.OAuthAuthorization;
import twitter4j.internal.http.HttpParameter;
import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;

/**
* @see <a href="http://apidocs.posterous.com/pages/twitter">Posterous API Documentation</a>
* @author withgod - noname at withgod.jp
* @since Twitter4J 2.1.12
*/
class PosterousUpload extends AbstractImageUploadImpl {

public PosterousUpload(Configuration conf, OAuthAuthorization oauth) {
super(conf, oauth);
}


@Override
protected String postUpload() throws TwitterException {
int statusCode = httpResponse.getStatusCode();
if (statusCode != 200)
throw new TwitterException("Posterous image upload returned invalid status code", httpResponse);

String response = httpResponse.asString();

try {
JSONObject json = new JSONObject(response);
if (!json.isNull("url"))
return json.getString("url");
} catch (JSONException e) {
throw new TwitterException("Invalid Posterous response: " + response, e);
}

throw new TwitterException("Unknown Posterous response", httpResponse);
}

@Override
protected void preUpload() throws TwitterException {
uploadUrl = "http://posterous.com/api2/upload.json";
String verifyCredentialsAuthorizationHeader = generateVerifyCredentialsAuthorizationHeader(TWITTER_VERIFY_CREDENTIALS_JSON);

headers.put("X-Auth-Service-Provider", TWITTER_VERIFY_CREDENTIALS_JSON);
headers.put("X-Verify-Credentials-Authorization", verifyCredentialsAuthorizationHeader);

HttpParameter[] params = {this.image};
if (message != null) {
params = appendHttpParameters(new HttpParameter[]{
this.message
}, params);
}
this.postParameter = params;
}
}
100 changes: 100 additions & 0 deletions twitter4j-media-support/src/main/java/twitter4j/media/TwiplUpload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
Copyright (c) 2007-2011, Yusuke Yamamoto
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Yusuke Yamamoto nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package twitter4j.media;

import twitter4j.TwitterException;
import twitter4j.conf.Configuration;
import twitter4j.http.OAuthAuthorization;
import twitter4j.internal.http.HttpParameter;

/**
* @see <a href="http://www.twipl.net/api/doc">Twipl Open API</a>
* @author withgod - noname at withgod.jp
* @since Twitter4J 2.1.12
*/
class TwiplUpload extends AbstractImageUploadImpl {

public TwiplUpload(Configuration conf, String apiKey, OAuthAuthorization oauth) {
super(conf, apiKey, oauth);
}


@Override
protected String postUpload() throws TwitterException {
int statusCode = httpResponse.getStatusCode();
if (statusCode != 200)
throw new TwitterException("Twipl image upload returned invalid status code", httpResponse);

String response = httpResponse.asString();
if(-1 != response.indexOf("<response status=\"ok\">")){
String h = "<mediaurl>";
int i = response.indexOf(h);
if(i != -1){
int j = response.indexOf("</mediaurl>", i + h.length());
if(j != -1){
return response.substring(i + h.length(), j);
}
}
} else if(-1 != response.indexOf("<rsp status=\"fail\">")){
String h = "msg=\"";
int i = response.indexOf(h);
if(i != -1){
int j = response.indexOf("\"", i + h.length());
if(j != -1){
String msg = response.substring(i + h.length(), j);
throw new TwitterException ("Invalid Twitgoo response: " + msg);
}
}
}

throw new TwitterException("Unknown Twipl response", httpResponse);
}

@Override
protected void preUpload() throws TwitterException {
uploadUrl = "http://api.twipl.net/2/upload.xml";
String verifyCredentialsAuthorizationHeader = generateVerifyCredentialsAuthorizationHeader(TWITTER_VERIFY_CREDENTIALS_XML);

headers.put("X-OAUTH-AUTHORIZATION ", verifyCredentialsAuthorizationHeader);
headers.put("X-OAUTH-SP-URL ", TWITTER_VERIFY_CREDENTIALS_XML);

if (null == apiKey) {
throw new IllegalStateException("No API Key for Twipl specified. put media.providerAPIKey in twitter4j.properties.");
}
String fname = this.image.getFile().getName();
HttpParameter[] params = {
new HttpParameter("key", apiKey),
new HttpParameter("media1", fname, this.image.getFileBody())
};
if (message != null) {
params = appendHttpParameters(new HttpParameter[]{
this.message
}, params);
}
this.postParameter = params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public void testProviders() throws Exception {
factory = new ImageUploaderFactory(conf);
conf = new ConfigurationBuilder().setMediaProvider(MediaProvider.YFROG.getName()).build();
factory = new ImageUploaderFactory(conf);
conf = new ConfigurationBuilder().setMediaProvider(MediaProvider.MOBYPICTURE.getName()).build();
factory = new ImageUploaderFactory(conf);
conf = new ConfigurationBuilder().setMediaProvider(MediaProvider.TWIPL.getName()).build();
factory = new ImageUploaderFactory(conf);
conf = new ConfigurationBuilder().setMediaProvider(MediaProvider.POSTEROUS.getName()).build();
factory = new ImageUploaderFactory(conf);
}

public void testNonexistingFileUpload() throws Exception {
Expand Down Expand Up @@ -160,6 +166,42 @@ public void testTwippleUpload() throws Exception {
}
}

public void testMobypictureUpload() throws Exception {
InputStream is = getClass().getResourceAsStream("/" + fileName);
try {
ImageUploaderFactory factory = new ImageUploaderFactory(getConfiguration("IOUxMoqc8Snms9nU"));
ImageUpload upload = factory.getInstance(MediaProvider.MOBYPICTURE);
String url = upload.upload(fileName, is);
assertTrue(url.length() > 0);
} finally {
is.close();
}
}

public void testTwiplUpload() throws Exception {
InputStream is = getClass().getResourceAsStream("/" + fileName);
try {
ImageUploaderFactory factory = new ImageUploaderFactory(getConfiguration("56fd1892dcf34c14beb7e2eecfc65c81"));
ImageUpload upload = factory.getInstance(MediaProvider.TWIPL);
String url = upload.upload(fileName, is);
assertTrue(url.length() > 0);
} finally {
is.close();
}
}

public void testPosterousUpload() throws Exception {
InputStream is = getClass().getResourceAsStream("/" + fileName);
try {
ImageUploaderFactory factory = new ImageUploaderFactory(getConfiguration(null));
ImageUpload upload = factory.getInstance(MediaProvider.POSTEROUS);
String url = upload.upload(fileName, is);
assertTrue(url.length() > 0);
} finally {
is.close();
}
}

public void testFromConfigurationUpload() throws Exception {
InputStream is = getClass().getResourceAsStream("/" + fileName);
try {
Expand Down

0 comments on commit 539ab5c

Please sign in to comment.