com.googlecode.flickrjandroid.FlickrException Java Examples

The following examples show how to use com.googlecode.flickrjandroid.FlickrException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OAuthInterface.java    From flickr-uploader with GNU General Public License v2.0 6 votes vote down vote up
public User testLogin() throws FlickrException, IOException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_TEST_LOGIN));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));
	OAuthUtils.addOAuthToken(parameters);
	Response response = this.oauthTransport.postJSON(this.sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}

	JSONObject jObj = response.getData();
	JSONObject userObj = jObj.getJSONObject("user");
	String id = userObj.getString("id");
	String name = userObj.getJSONObject("username").getString("_content");
	User user = new User();
	user.setId(id);
	user.setUsername(name);
	return user;
}
 
Example #2
Source File: Uploader.java    From flickr-uploader with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Upload a photo from an InputStream.
 * 
 * 
 * @param imageName
 * @param in
 * @param photoId
 * @return photoId for sync mode or ticketId for async mode
 * @throws IOException
 * @throws FlickrException
 * @throws SAXException
 * @deprecated This is not working at moment!
 */
public String replace(String imageName, InputStream in, String photoId, boolean async) throws IOException, FlickrException, SAXException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, this.apiKey));

	parameters.add(new Parameter("async", async ? "1" : "0"));
	parameters.add(new Parameter("photo_id", photoId));

	parameters.add(new ImageParameter(imageName, in));
	OAuthUtils.addOAuthToken(parameters);

	UploaderResponse response = (UploaderResponse) transport.replace(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
	String id = "";
	if (async) {
		id = response.getTicketId();
	} else {
		id = response.getPhotoId();
	}
	return id;
}
 
Example #3
Source File: Uploader.java    From flickr-uploader with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Upload a photo from an InputStream.
 * 
 * @param imageName
 * @param in
 * @param photoId
 * @return photoId for sync mode or ticketId for async mode
 * @throws IOException
 * @throws FlickrException
 * @throws SAXException
 * @deprecated This is not working at moment!
 */
public String replace(String imageName, byte[] data, String photoId, boolean async) throws IOException, FlickrException, SAXException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, this.apiKey));

	parameters.add(new Parameter("async", async ? "1" : "0"));
	parameters.add(new Parameter("photo_id", photoId));

	parameters.add(new ImageParameter(imageName, data));
	;
	OAuthUtils.addOAuthToken(parameters);

	UploaderResponse response = (UploaderResponse) transport.replace(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
	String id = "";
	if (async) {
		id = response.getTicketId();
	} else {
		id = response.getPhotoId();
	}
	return id;
}
 
Example #4
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new photoset.
 * 
 * @param title
 *            The photoset title
 * @param description
 *            The photoset description
 * @param primaryPhotoId
 *            The primary photo id
 * @return The new Photset
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public Photoset create(String title, String description, String primaryPhotoId) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_CREATE));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));

	parameters.add(new Parameter("title", title));
	parameters.add(new Parameter("description", description));
	parameters.add(new Parameter("primary_photo_id", primaryPhotoId));
	OAuthUtils.addOAuthToken(parameters);

	Response response = transportAPI.postJSON(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
	JSONObject photosetElement = response.getData().getJSONObject("photoset");
	Photoset photoset = new Photoset();
	photoset.setId(photosetElement.getString("id"));
	photoset.setUrl(photosetElement.getString("url"));
	return photoset;
}
 
Example #5
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Modify the meta-data for a photoset.
 * 
 * @param photosetId
 *            The photoset ID
 * @param title
 *            A new title
 * @param description
 *            A new description (can be null)
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public void editMeta(String photosetId, String title, String description) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_EDIT_META));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));

	parameters.add(new Parameter("photoset_id", photosetId));
	parameters.add(new Parameter("title", title));
	if (description != null) {
		parameters.add(new Parameter("description", description));
	}
	OAuthUtils.addOAuthToken(parameters);

	Response response = transportAPI.postJSON(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
}
 
Example #6
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Edit which photos are in the photoset.
 * 
 * @param photosetId
 *            The photoset ID
 * @param primaryPhotoId
 *            The primary photo Id
 * @param photoIds
 *            The photo IDs for the photos in the set
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public void editPhotos(String photosetId, String primaryPhotoId, String[] photoIds) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_EDIT_PHOTOS));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));

	parameters.add(new Parameter("photoset_id", photosetId));
	parameters.add(new Parameter("primary_photo_id", primaryPhotoId));
	parameters.add(new Parameter("photo_ids", StringUtilities.join(photoIds, ",")));
	OAuthUtils.addOAuthToken(parameters);

	Response response = transportAPI.postJSON(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
}
 
Example #7
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the order in which sets are returned for the user.
 * 
 * This method requires authentication with 'write' permission.
 * 
 * @param photosetIds
 *            An array of Ids
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public void orderSets(String[] photosetIds) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_ORDER_SETS));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));

	parameters.add(new Parameter("photoset_ids", StringUtilities.join(photosetIds, ",")));
	OAuthUtils.addOAuthToken(parameters);

	Response response = transportAPI.postJSON(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
}
 
Example #8
Source File: UploadService.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isRetryable(Throwable e) {
	if (e == null) {
		return false;
	} else if (e instanceof FlickrException) {
		return false;
	} else if (e instanceof FileNotFoundException) {
		return false;
	} else if (e instanceof RuntimeException && e.getCause() != null) {
		return isRetryable(e.getCause());
	}
	return true;
}
 
Example #9
Source File: OAuthUtils.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
public static void sign(String requestMethod, String url, String apiSharedSecret, List<Parameter>  parameters) throws FlickrException {
	OAuth oauth = RequestContext.getRequestContext().getOAuth();
	
	String tokenSecret = oauth != null && oauth.getToken() != null 
	? oauth.getToken().getOauthTokenSecret() : "";
	// generate the oauth_signature
	String signature = OAuthUtils.getSignature(
			requestMethod, 
			url, 
			parameters,
			apiSharedSecret, tokenSecret);
	// This method call must be signed.
	parameters.add(new Parameter("oauth_signature", signature));
}
 
Example #10
Source File: Photo.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
private StringBuffer getOriginalBaseImageUrl() throws FlickrException, NullPointerException {
	StringBuffer buffer = new StringBuffer();
	buffer.append(_getBaseImageUrl());
	if (getOriginalSecret().length() > 8) {
		buffer.append(getOriginalSecret());
	} else {
		throw new FlickrException("0", "OriginalUrl not available because of missing originalsecret.");
	}
	return buffer;
}
 
Example #11
Source File: Photo.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the original image URL.
 * 
 * @return The original image URL
 */
public String getOriginalUrl() throws FlickrException {
	if (originalSize == null) {
		if (originalFormat != null) {
			return getOriginalBaseImageUrl() + "_o." + originalFormat;
		}
		return getOriginalBaseImageUrl() + DEFAULT_ORIGINAL_IMAGE_SUFFIX;
	} else {
		return originalSize.getSource();
	}
}
 
Example #12
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
public void setPrimaryPhoto(String photosetId, String photoId) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_SET_PRIMARY_PHOTO));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));

	parameters.add(new Parameter("photoset_id", photosetId));
	parameters.add(new Parameter("photo_id", photoId));
	OAuthUtils.addOAuthToken(parameters);

	Response response = transportAPI.postJSON(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
}
 
Example #13
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove a photo from the set.
 * 
 * @param photosetId
 *            The photoset ID
 * @param photoId
 *            The photo ID
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public void removePhoto(String photosetId, String photoId) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_REMOVE_PHOTO));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));

	parameters.add(new Parameter("photoset_id", photosetId));
	parameters.add(new Parameter("photo_id", photoId));
	OAuthUtils.addOAuthToken(parameters);

	Response response = transportAPI.postJSON(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
}
 
Example #14
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the order in which sets are returned for the user.
 * 
 * This method requires authentication with 'write' permission.
 * 
 * @param photosetIds
 *            An array of Ids
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public void reorderPhotos(String photosetId, List<String> photoIds) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_REORDER_PHOTOS));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));

	parameters.add(new Parameter("photoset_id", photosetId));
	parameters.add(new Parameter("photo_ids", StringUtilities.join(photoIds, ",")));
	OAuthUtils.addOAuthToken(parameters);

	Response response = transportAPI.postJSON(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
}
 
Example #15
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the information for a specified photoset.
 * 
 * This method does not require authentication.
 * 
 * @param photosetId
 *            The photoset ID
 * @return The Photoset
 * @throws FlickrException
 * @throws IOException
 * @throws JSONException
 */
public Photoset getInfo(String photosetId) throws FlickrException, IOException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_GET_INFO));
	parameters.add(new Parameter("api_key", apiKey));

	parameters.add(new Parameter("photoset_id", photosetId));

	Response response = transportAPI.get(transportAPI.getPath(), parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
	JSONObject photosetElement = response.getData().getJSONObject("photoset");
	Photoset photoset = new Photoset();
	photoset.setId(photosetElement.getString("id"));

	User owner = new User();
	owner.setId(photosetElement.getString("owner"));
	photoset.setOwner(owner);

	Photo primaryPhoto = new Photo();
	primaryPhoto.setId(photosetElement.getString("primary"));
	primaryPhoto.setSecret(photosetElement.getString("secret")); // TODO verify that this is the secret for the photo
	primaryPhoto.setServer(photosetElement.getString("server")); // TODO verify that this is the server for the photo
	primaryPhoto.setFarm(photosetElement.getString("farm"));
	photoset.setPrimaryPhoto(primaryPhoto);

	// TODO remove secret/server/farm from photoset?
	// It's rather related to the primaryPhoto, then to the photoset itself.
	photoset.setSecret(photosetElement.getString("secret"));
	photoset.setServer(photosetElement.getString("server"));
	photoset.setFarm(photosetElement.getString("farm"));
	photoset.setPhotoCount(photosetElement.getString("photos"));

	photoset.setTitle(JSONUtils.getChildValue(photosetElement, "title"));
	photoset.setDescription(JSONUtils.getChildValue(photosetElement, "description"));
	photoset.setPrimaryPhoto(primaryPhoto);

	return photoset;
}
 
Example #16
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Delete the specified photoset.
 * 
 * @param photosetId
 *            The photoset ID
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public void delete(String photosetId) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_DELETE));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));

	parameters.add(new Parameter("photoset_id", photosetId));
	OAuthUtils.addOAuthToken(parameters);

	Response response = transportAPI.postJSON(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
}
 
Example #17
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add a photo to the end of the photoset.
 * <p/>
 * Note: requires authentication with the new authentication API with 'write' permission.
 * 
 * @param photosetId
 *            The photoset ID
 * @param photoId
 *            The photo ID
 * @throws JSONException
 */
public void addPhoto(String photosetId, String photoId) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_ADD_PHOTO));
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));

	parameters.add(new Parameter("photoset_id", photosetId));
	parameters.add(new Parameter("photo_id", photoId));
	OAuthUtils.addOAuthToken(parameters);

	Response response = transportAPI.postJSON(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
}
 
Example #18
Source File: FlickrHelper.java    From glimmr with Apache License 2.0 5 votes vote down vote up
/**
 * Check if exception e is the cause of Flickr being down and show some toast.
 * @param context
 * @return true if flickr is down
 */
public boolean handleFlickrUnavailable(Context context, Exception e) {
    if (e != null && e instanceof FlickrException) {
        if (((FlickrException) e).getErrorCode().equals(
                Constants.ERR_CODE_FLICKR_UNAVAILABLE)) {
            e.printStackTrace();
            Log.w(TAG, "Flickr seems down at the moment");
            Toast.makeText(context, context.getString(R.string.flickr_unavailable),
                    Toast.LENGTH_LONG).show();
            return true;
        }
    }
    return false;
}
 
Example #19
Source File: ExifInfoFragment.java    From glimmr with Apache License 2.0 4 votes vote down vote up
public void onExifInfoReady(List<Exif> exifInfo, Exception exc) {
    mActivity.setProgressBarIndeterminateVisibility(Boolean.FALSE);

    if (FlickrHelper.getInstance().handleFlickrUnavailable(mActivity, exc)) {
        return;
    }

    if (FlickrHelper.getInstance().handleFlickrUnavailable(mActivity, exc)) {
        return;
    }

    /* Something went wrong, show message and return */
    if (exc != null) {
        mTextViewErrorMessage.setVisibility(View.VISIBLE);
        if (exc instanceof FlickrException) {
            String errCode = ((FlickrException) exc).getErrorCode();
            if (BuildConfig.DEBUG) Log.d(getLogTag(), "errCode: " + errCode);
            if (errCode != null && ERR_PERMISSION_DENIED.equals(errCode)) {
                mTextViewErrorMessage.setText(
                        mActivity.getString(R.string.no_exif_permission));
            } else {
                mTextViewErrorMessage.setText(
                        mActivity.getString(R.string.no_connection));
            }
        } else {
            mTextViewErrorMessage.setText(
                    mActivity.getString(R.string.no_connection));
        }
        return;
    }

    /* Populate table with exif info */
    for (Exif e : exifInfo) {
        /* Convert camel case key to space delimited:
         * http://stackoverflow.com/a/2560017/663370 */
        String rawTag = e.getTag();
        String tagConverted = rawTag.replaceAll(
                String.format("%s|%s|%s",
                    "(?<=[A-Z])(?=[A-Z][a-z])",
                    "(?<=[^A-Z])(?=[A-Z])",
                    "(?<=[A-Za-z])(?=[^A-Za-z])"), " "
                );
        addKeyValueRow(tagConverted, e.getRaw());
    }
}
 
Example #20
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get a collection of Photo objects for the specified Photoset.
 * 
 * This method does not require authentication.
 * 
 * @see com.gmail.yuyang226.flickr.photos.Extras
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_NO_FILTER
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_PUBLIC
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_FRIENDS
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_FRIENDS_FAMILY
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_FAMILY
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_FRIENDS
 * @param photosetId
 *            The photoset ID
 * @param extras
 *            Set of extra-fields
 * @param privacy_filter
 *            filter value for authenticated calls
 * @param perPage
 *            The number of photos per page
 * @param page
 *            The page offset
 * @return PhotoList The Collection of Photo objects
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public PhotoList getPhotos(String photosetId, Set<String> extras, int privacy_filter, int perPage, int page) throws IOException, FlickrException, JSONException {
	PhotoList photos = new PhotoList();
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_GET_PHOTOS));
	boolean signed = OAuthUtils.hasSigned();
	if (signed) {
		parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));
	} else {
		parameters.add(new Parameter("api_key", apiKey));
	}

	parameters.add(new Parameter("photoset_id", photosetId));

	if (perPage > 0) {
		parameters.add(new Parameter("per_page", Integer.valueOf(perPage)));
	}

	if (page > 0) {
		parameters.add(new Parameter("page", Integer.valueOf(page)));
	}

	if (privacy_filter > 0) {
		parameters.add(new Parameter("privacy_filter", "" + privacy_filter));
	}

	if (extras != null && !extras.isEmpty()) {
		parameters.add(new Parameter(Extras.KEY_EXTRAS, StringUtilities.join(extras, ",")));
	}
	if (signed) {
		OAuthUtils.addOAuthToken(parameters);
	}

	Response response = signed ? transportAPI.postJSON(sharedSecret, parameters) : transportAPI.get(transportAPI.getPath(), parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}

	JSONObject photoset = response.getData().getJSONObject("photoset");
	JSONArray photoElements = photoset.optJSONArray("photo");
	photos.setPage(photoset.getString("page"));
	photos.setPages(photoset.getString("pages"));
	photos.setPerPage(photoset.getString("per_page"));
	photos.setTotal(photoset.getString("total"));

	for (int i = 0; photoElements != null && i < photoElements.length(); i++) {
		JSONObject photoElement = photoElements.getJSONObject(i);
		photos.add(PhotoUtils.createPhoto(photoElement));
	}

	return photos;
}
 
Example #21
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get a list of all photosets for the specified user.
 * 
 * This method does not require authentication. But to get a Photoset into the list, that contains just private photos, the call needs to be authenticated.
 * 
 * @param userId
 *            The User id
 * @return The Photosets collection
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public Photosets getList(String userId) throws IOException, FlickrException, JSONException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter("method", METHOD_GET_LIST));

	boolean signed = OAuthUtils.hasSigned();
	if (signed) {
		parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, apiKey));
	} else {
		parameters.add(new Parameter("api_key", apiKey));
	}

	if (userId != null) {
		parameters.add(new Parameter("user_id", userId));
	}

	if (signed) {
		OAuthUtils.addOAuthToken(parameters);
	}

	Response response = signed ? transportAPI.postJSON(sharedSecret, parameters) : transportAPI.get(transportAPI.getPath(), parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
	Photosets photosetsObject = new Photosets();
	JSONObject photosetsElement = response.getData().getJSONObject("photosets");
	List<Photoset> photosets = new ArrayList<Photoset>();
	JSONArray photosetElements = photosetsElement.optJSONArray("photoset");
	for (int i = 0; photosetElements != null && i < photosetElements.length(); i++) {
		JSONObject photosetElement = photosetElements.getJSONObject(i);
		Photoset photoset = new Photoset();
		photoset.setId(photosetElement.getString("id"));

		if (photosetElement.has("owner")) {
			User owner = new User();
			owner.setId(photosetElement.getString("owner"));
			photoset.setOwner(owner);
		}

		Photo primaryPhoto = new Photo();
		primaryPhoto.setId(photosetElement.getString("primary"));
		primaryPhoto.setSecret(photosetElement.getString("secret")); // TODO verify that this is the secret for the photo
		primaryPhoto.setServer(photosetElement.getString("server")); // TODO verify that this is the server for the photo
		primaryPhoto.setFarm(photosetElement.getString("farm"));
		photoset.setPrimaryPhoto(primaryPhoto);

		photoset.setSecret(photosetElement.getString("secret"));
		photoset.setServer(photosetElement.getString("server"));
		photoset.setFarm(photosetElement.getString("farm"));
		photoset.setPhotoCount(photosetElement.getString("photos"));

		photoset.setTitle(JSONUtils.getChildValue(photosetElement, "title"));
		photoset.setDescription(JSONUtils.getChildValue(photosetElement, "description"));

		photosets.add(photoset);
	}

	photosetsObject.setPhotosets(photosets);
	return photosetsObject;
}
 
Example #22
Source File: Uploader.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
/**
	 * Upload a photo from an InputStream.
	 * 
	 * @param in
	 * @param metaData
	 * @return photoId for sync mode or ticketId for async mode
	 * @throws IOException
	 * @throws FlickrException
	 * @throws SAXException
	 */
	public String upload(String imageName, File file, UploadMetaData metaData, Media media) throws IOException, FlickrException, SAXException {
		List<Parameter> parameters = new ArrayList<Parameter>();
		parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, this.apiKey));

		String title = metaData.getTitle();
		if (title != null)
			parameters.add(new Parameter("title", title));

		String description = metaData.getDescription();
		if (description != null)
			parameters.add(new Parameter("description", description));

		Collection<String> tags = metaData.getTags();
		if (tags != null) {
			parameters.add(new Parameter("tags", StringUtilities.join(tags, " ")));
		}

		parameters.add(new Parameter("is_public", metaData.isPublicFlag() ? "1" : "0"));
		parameters.add(new Parameter("is_family", metaData.isFamilyFlag() ? "1" : "0"));
		parameters.add(new Parameter("is_friend", metaData.isFriendFlag() ? "1" : "0"));
		parameters.add(new Parameter("async", metaData.isAsync() ? "1" : "0"));
		if (metaData.getSafetyLevel() != null) {
			parameters.add(new Parameter("safety_level", metaData.getSafetyLevel()));
		}
		if (metaData.getContentType() != null) {
			parameters.add(new Parameter("content_type", metaData.getContentType()));
		}

		parameters.add(new ImageParameter(imageName, file));
		OAuthUtils.addOAuthToken(parameters);
		OAuthUtils.addOAuthParams(sharedSecret, Uploader.URL_UPLOAD, parameters);
		UploaderResponse response = (UploaderResponse) ((REST) transport).sendUpload(Uploader.UPLOAD_PATH, parameters, media);

//		UploaderResponse response = (UploaderResponse) ((REST) transport).upload(sharedSecret, parameters, progressListener);
		if (response.isError()) {
			throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
		}
		String id = "";
		if (metaData.isAsync()) {
			id = response.getTicketId();
		} else {
			id = response.getPhotoId();
		}
		return id;
	}
 
Example #23
Source File: OAuthUtils.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
public static void addOAuthParams(String apiSharedSecret, String url, List<Parameter> parameters) 
throws FlickrException {
	addBasicOAuthParams(parameters);
	signPost(apiSharedSecret, url, parameters);
}
 
Example #24
Source File: OAuthUtils.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
public static void signGet(String apiSharedSecret, String url, List<Parameter>  parameters) throws FlickrException {
	sign(OAuthUtils.REQUEST_METHOD_GET, url, apiSharedSecret, parameters);
}
 
Example #25
Source File: OAuthUtils.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
public static void signPost(String apiSharedSecret, String url, List<Parameter>  parameters) throws FlickrException {
	sign(OAuthUtils.REQUEST_METHOD_POST, url, apiSharedSecret, parameters);
}
 
Example #26
Source File: Uploader.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Upload a photo from a byte-array.
 * 
 * @param data
 *            The photo data as a byte array
 * @param metaData
 *            The meta data
 * @return photoId for sync mode or ticketId for async mode
 * @throws FlickrException
 * @throws IOException
 * @throws SAXException
 */
@Deprecated
public String upload(String imageName, byte[] data, UploadMetaData metaData) throws FlickrException, IOException, SAXException {
	List<Parameter> parameters = new ArrayList<Parameter>();
	parameters.add(new Parameter(OAuthInterface.PARAM_OAUTH_CONSUMER_KEY, this.apiKey));
	String title = metaData.getTitle();
	if (title != null)
		parameters.add(new Parameter("title", title));

	String description = metaData.getDescription();
	if (description != null)
		parameters.add(new Parameter("description", description));

	Collection<String> tags = metaData.getTags();
	if (tags != null)
		parameters.add(new Parameter("tags", StringUtilities.join(tags, " ")));

	parameters.add(new Parameter("is_public", metaData.isPublicFlag() ? "1" : "0"));
	parameters.add(new Parameter("is_family", metaData.isFamilyFlag() ? "1" : "0"));
	parameters.add(new Parameter("is_friend", metaData.isFriendFlag() ? "1" : "0"));

	parameters.add(new ImageParameter(imageName, data));

	if (metaData.isHidden() != null) {
		parameters.add(new Parameter("hidden", metaData.isHidden().booleanValue() ? "1" : "0"));
	}

	if (metaData.getSafetyLevel() != null) {
		parameters.add(new Parameter("safety_level", metaData.getSafetyLevel()));
	}

	parameters.add(new Parameter("async", metaData.isAsync() ? "1" : "0"));

	if (metaData.getContentType() != null) {
		parameters.add(new Parameter("content_type", metaData.getContentType()));
	}
	OAuthUtils.addOAuthToken(parameters);

	UploaderResponse response = (UploaderResponse) transport.upload(sharedSecret, parameters);
	if (response.isError()) {
		throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
	}
	String id = "";
	if (metaData.isAsync()) {
		id = response.getTicketId();
	} else {
		id = response.getPhotoId();
	}
	return id;
}
 
Example #27
Source File: OAuthUtils.java    From flickr-uploader with GNU General Public License v2.0 4 votes vote down vote up
public static String getSignature(String url, List<Parameter> parameters
		, String apiSecret, String tokenSecret)
throws FlickrException {
	return getSignature(REQUEST_METHOD_GET, url, parameters, apiSecret, tokenSecret);
}
 
Example #28
Source File: Photo.java    From flickr-uploader with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Get an InputStream for the original image. Callers must close the stream upon completion.
 * 
 * @deprecated
 * @see PhotosInterface#getImageAsStream(Photo, int)
 * @return The InputStream
 * @throws IOException
 */
public InputStream getOriginalAsStream() throws IOException, FlickrException {
	if (originalFormat != null) {
		return getOriginalImageAsStream("_o." + originalFormat);
	}
	return getOriginalImageAsStream(DEFAULT_ORIGINAL_IMAGE_SUFFIX);
}
 
Example #29
Source File: PhotosetsInterface.java    From flickr-uploader with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Convenience method.
 * 
 * Calls getPhotos() with Extras.MIN_EXTRAS and Flickr.PRIVACY_LEVEL_NO_FILTER.
 * 
 * This method does not require authentication.
 * 
 * @see com.gmail.yuyang226.flickr.photos.Extras
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_NO_FILTER
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_PUBLIC
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_FRIENDS
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_FRIENDS_FAMILY
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_FAMILY
 * @see com.gmail.yuyang226.flickr.Flickr#PRIVACY_LEVEL_FRIENDS
 * @param photosetId
 *            The photoset ID
 * @param perPage
 *            The number of photos per page
 * @param page
 *            The page offset
 * @return PhotoList The Collection of Photo objects
 * @throws IOException
 * @throws FlickrException
 * @throws JSONException
 */
public PhotoList getPhotos(String photosetId, int perPage, int page) throws IOException, FlickrException, JSONException {
	return getPhotos(photosetId, Extras.MIN_EXTRAS, Flickr.PRIVACY_LEVEL_NO_FILTER, perPage, page);
}
 
Example #30
Source File: Photo.java    From flickr-uploader with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 
 * @deprecated
 * @see PhotosInterface#getImageAsStream(Photo, int)
 * @param suffix
 * @return InoutStream
 * @throws IOException
 * @throws FlickrException
 */
private InputStream getOriginalImageAsStream(String suffix) throws IOException, FlickrException {
	StringBuffer buffer = getOriginalBaseImageUrl();
	buffer.append(suffix);
	return _getImageAsStream(buffer.toString());
}