Search in sources :

Example 1 with OAuthRequest

use of io.jawg.osmcontributor.flickr.oauth.OAuthRequest in project osm-contributor by jawg.

the class PhotoActivity method setPhotoTag.

private void setPhotoTag(final String photoId) throws UnsupportedEncodingException {
    // Create the machine tag for Flickr to associate picture to this Poi
    Poi currentPoi = application.getOsmTemplateComponent().getPoiManager().queryForId(poiId);
    StringBuilder flickrOsmTagBuilder = new StringBuilder(FLICKR_DEFAULT_TAG);
    if (currentPoi.getBackendId() != null && currentPoi.getBackendId().length() > 0) {
        flickrOsmTagBuilder.append(",osm:");
        flickrOsmTagBuilder.append((currentPoi.getWay()) ? "way" : "node");
        flickrOsmTagBuilder.append("=");
        flickrOsmTagBuilder.append(currentPoi.getBackendId());
    } else {
    // TODO Handle pictures added on new POIs which haven't an OSM ID yet
    // Such POIs might be sent to OSM first, and when an ID is defined, update the Flickr picture
    }
    String flickrOsmTag = URLEncoder.encode(flickrOsmTagBuilder.toString(), UTF_8);
    OAuthRequest oauthRequest = flickrOAuth.getOAuthRequest();
    if (oauthRequest == null) {
        oauthRequest = new OAuthRequest(application.getFlickr().getApiKey(), application.getFlickr().getSharedSecret());
        oauthRequest.setOAuthToken(configManager.getFlickrToken());
        oauthRequest.setOAuthTokenSecret(configManager.getFlickrTokenSecret());
        flickrOAuth.setOAuthRequest(oauthRequest);
    }
    oauthRequest.setRequestUrl(FLICKR_API_SERVICES);
    oauthRequest.initParam(OAuthParams.getOAuthParams().put(OAuthParams.OAUTH_TOKEN, oauthRequest.getOAuthToken()).put("method", FLICKR_METHOD_ADDTAGS).put("photo_id", photoId).put("tags", flickrOsmTag).toMap());
    oauthRequest.signRequest(Verb.POST);
    // Filter params for Authorization header
    Map<String, String> oauthParams = new TreeMap<String, String>();
    for (Map.Entry<String, String> entry : oauthRequest.getParams().entrySet()) {
        if (entry.getKey().startsWith(PARAM_OAUTH_PREFIX)) {
            oauthParams.put(entry.getKey(), entry.getValue());
        }
    }
    flickrAddTagClient = FlickrPhotoUtils.getAdapter(oauthParams).create(FlickrAddTagClient.class);
    flickrAddTagClient.addTags(new StringBuilder("method=").append(FLICKR_METHOD_ADDTAGS).append("&photo_id=").append(photoId).append("&tags=").append(flickrOsmTag).toString()).enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.isSuccessful()) {
                String responseAsString = response.body();
                Log.d(TAG, responseAsString);
                progressDialog.dismiss();
                photoFile.delete();
                Toast.makeText(PhotoActivity.this, R.string.picture_sent_success, Toast.LENGTH_LONG).show();
                if (ImageAdapter.getPhotoUrlsCachedThumbs(poiId) == null || ImageAdapter.getPhotoUrlsCachedThumbs(poiId).isEmpty()) {
                    finish();
                }
            } else {
                onFailure(call, new Exception());
            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            if (nbTry < MAX_RETRY_UPLOAD) {
                nbTry++;
                try {
                    setPhotoTag(photoId);
                } catch (UnsupportedEncodingException e) {
                    Timber.e(e, e.getMessage());
                }
            } else {
                nbTry = 0;
                Toast.makeText(PhotoActivity.this, R.string.poi_association_failure, Toast.LENGTH_LONG).show();
                progressDialog.dismiss();
            }
        }
    });
}
Also used : OAuthRequest(io.jawg.osmcontributor.flickr.oauth.OAuthRequest) FlickrAddTagClient(io.jawg.osmcontributor.flickr.rest.FlickrAddTagClient) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Poi(io.jawg.osmcontributor.model.entities.Poi) TreeMap(java.util.TreeMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 2 with OAuthRequest

use of io.jawg.osmcontributor.flickr.oauth.OAuthRequest in project osm-contributor by jawg.

the class PhotoActivity method setPhotoLocation.

private void setPhotoLocation(final String photoId) {
    if (flickrPhotoClient == null) {
        flickrPhotoClient = FlickrPhotoUtils.getAdapter().create(FlickrPhotoClient.class);
    }
    OAuthRequest oauthRequest = new OAuthRequest(configManager.getFlickrApiKey(), configManager.getFlickrApiKeySecret());
    oauthRequest.setRequestUrl(FLICKR_API_SERVICES);
    oauthRequest.setOAuthToken(configManager.getFlickrToken());
    oauthRequest.setOAuthTokenSecret(configManager.getFlickrTokenSecret());
    oauthRequest.initParam(OAuthParams.getOAuthParams().put(OAuthParams.OAUTH_TOKEN, configManager.getFlickrToken()).put("method", "flickr.photos.geo.setLocation").put("photo_id", photoId).put("lat", String.valueOf(latitude)).put("lon", String.valueOf(longitude)).toMap());
    oauthRequest.signRequest(Verb.GET);
    flickrPhotoClient.setProperties(oauthRequest.getParams()).enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.isSuccessful()) {
                try {
                    setPhotoTag(photoId);
                } catch (UnsupportedEncodingException e) {
                    onFailure(call, new RuntimeException(e));
                }
            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Toast.makeText(PhotoActivity.this, R.string.picture_sent_failure, Toast.LENGTH_LONG).show();
            progressDialog.dismiss();
        }
    });
}
Also used : OAuthRequest(io.jawg.osmcontributor.flickr.oauth.OAuthRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FlickrPhotoClient(io.jawg.osmcontributor.flickr.rest.FlickrPhotoClient)

Example 3 with OAuthRequest

use of io.jawg.osmcontributor.flickr.oauth.OAuthRequest in project osm-contributor by jawg.

the class PhotoActivity method onUploadFinishedEvent.

public void onUploadFinishedEvent(final String photoId) {
    // Retrieve picture URL
    if (flickrPhotoClient == null) {
        flickrPhotoClient = FlickrPhotoUtils.getAdapter().create(FlickrPhotoClient.class);
    }
    OAuthRequest oauthRequest = new OAuthRequest(configManager.getFlickrApiKey(), configManager.getFlickrApiKeySecret());
    oauthRequest.setRequestUrl(FLICKR_API_SERVICES);
    oauthRequest.setOAuthToken(configManager.getFlickrToken());
    oauthRequest.setOAuthTokenSecret(configManager.getFlickrTokenSecret());
    oauthRequest.initParam(OAuthParams.getOAuthParams().put(OAuthParams.OAUTH_TOKEN, configManager.getFlickrToken()).put("method", "flickr.photos.getInfo").put("photo_id", photoId).toMap());
    oauthRequest.signRequest(Verb.GET);
    flickrPhotoClient.setProperties(oauthRequest.getParams()).enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.isSuccessful()) {
                defineImageOnPoi(ResponseConverter.convertImageUrl(response.body()));
                setPhotoLocation(photoId);
            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Toast.makeText(PhotoActivity.this, R.string.flickr_communication_failure, Toast.LENGTH_LONG).show();
            progressDialog.dismiss();
        }
    });
}
Also used : OAuthRequest(io.jawg.osmcontributor.flickr.oauth.OAuthRequest) FlickrPhotoClient(io.jawg.osmcontributor.flickr.rest.FlickrPhotoClient)

Example 4 with OAuthRequest

use of io.jawg.osmcontributor.flickr.oauth.OAuthRequest in project osm-contributor by jawg.

the class PhotoActivity method uploadPhoto.

private void uploadPhoto() {
    OAuthRequest oAuthRequest = flickrOAuth.getOAuthRequest();
    if (oAuthRequest == null) {
        oAuthRequest = new OAuthRequest(application.getFlickr().getApiKey(), application.getFlickr().getSharedSecret());
        oAuthRequest.setOAuthToken(configManager.getFlickrToken());
        oAuthRequest.setOAuthTokenSecret(configManager.getFlickrTokenSecret());
        flickrOAuth.setOAuthRequest(oAuthRequest);
    }
    oAuthRequest.setRequestUrl(FLICKR_API_UPLOAD);
    oAuthRequest.initParam(OAuthParams.getOAuthParams().put(OAuthParams.OAUTH_TOKEN, oAuthRequest.getOAuthToken()).toMap());
    oAuthRequest.signRequest(Verb.POST);
    progressDialog.show();
    if (flickrUploadClient == null) {
        flickrUploadClient = FlickrUploadUtils.getRestAdapter(oAuthRequest.getParams()).create(FlickrUploadClient.class);
    }
    RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(Uri.fromFile(photoFile))), photoFile);
    flickrUploadClient.upload(MultipartBody.Part.createFormData("photo", photoFile.getName(), requestFile)).enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.isSuccessful()) {
                onUploadFinishedEvent(ResponseConverter.convertImageId(response.body()));
            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            if (nbTry < MAX_RETRY_UPLOAD) {
                nbTry++;
                uploadPhoto();
            } else {
                nbTry = 0;
                Toast.makeText(PhotoActivity.this, R.string.flickr_communication_failure, Toast.LENGTH_LONG).show();
                progressDialog.dismiss();
            }
        }
    });
}
Also used : OAuthRequest(io.jawg.osmcontributor.flickr.oauth.OAuthRequest) FlickrUploadClient(io.jawg.osmcontributor.flickr.rest.FlickrUploadClient) RequestBody(okhttp3.RequestBody)

Aggregations

OAuthRequest (io.jawg.osmcontributor.flickr.oauth.OAuthRequest)4 FlickrPhotoClient (io.jawg.osmcontributor.flickr.rest.FlickrPhotoClient)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 FlickrAddTagClient (io.jawg.osmcontributor.flickr.rest.FlickrAddTagClient)1 FlickrUploadClient (io.jawg.osmcontributor.flickr.rest.FlickrUploadClient)1 Poi (io.jawg.osmcontributor.model.entities.Poi)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 RequestBody (okhttp3.RequestBody)1