package com.volongoto.quickbp;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import com.google.gdata.client.photos.*;
import com.google.gdata.client.GoogleService;
import com.google.gdata.client.GoogleService.InvalidCredentialsException;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;
import com.google.gdata.util.ServiceException;
public final class PicasawebServiceHandler {
PicasawebServiceHandler(String username) {
this.username = username;
feedUrl = null;
serviceHandler = new GoogleServiceHandler();
service = new PicasawebService("NinjaBlogger");
// Initialize feedUrl
try {
feedUrl = new URL(METAFEED_URL_BASE+this.username+"?kind=album");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public boolean authenticate(String username, String password) throws InvalidCredentialsException {
return serviceHandler.authenticate(service, username, password);
}
public boolean handleCaptcha(
String username,
String password,
GoogleService.CaptchaRequiredException cre
) throws InvalidCredentialsException {
return serviceHandler.handleCaptcha(service, username, password, cre);
}
public UserFeed getMetaFeed() {
UserFeed metaFeed = null;
try {
metaFeed = service.getFeed(feedUrl, UserFeed.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return metaFeed;
}
public URL getFeedUrl() {
return feedUrl;
}
public PhotoEntry uploadPhoto(AlbumEntry album, File photo, String mediaType) {
String albumId = album.getId().split("/albumid/")[1];
URL albumPostUrl = null;
try {
albumPostUrl = new URL(ALBUM_POST_URL_BASE+username+"/albumid/"+albumId);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PhotoEntry myPhoto = new PhotoEntry();
myPhoto.setClient("NinjaBlogger");
MediaFileSource myMedia = new MediaFileSource(photo, mediaType);
myPhoto.setMediaSource(myMedia);
PhotoEntry returnedPhoto = null;
try {
returnedPhoto = service.insert(albumPostUrl, myPhoto);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnedPhoto;
}
public AlbumEntry insert(AlbumEntry albumEntry) {
AlbumEntry insertedAlbumEntry = null;
try {
insertedAlbumEntry = service.insert(this.getFeedUrl(), albumEntry);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return insertedAlbumEntry;
}
public AlbumEntry createAlbum(String blogTitle) {
AlbumEntry myAlbum = new AlbumEntry();
myAlbum.setTitle(new PlainTextConstruct(blogTitle));
AlbumEntry insertedAlbumEntry = this.insert(myAlbum);
return insertedAlbumEntry;
}
public void printAlbumList(UserFeed picasaAlbumList) {
for (AlbumEntry myAlbum : picasaAlbumList.getAlbumEntries()) {
System.out.println("Album Name: " + myAlbum.getTitle().getPlainText());
System.out.println(myAlbum.getId());
}
}
public AlbumEntry findCorrespondingPicasaAlbum(
String blogName,
UserFeed picasaAlbumList
) {
AlbumEntry foundAlbum = null;
for (AlbumEntry myAlbum : picasaAlbumList.getAlbumEntries()) {
if(myAlbum.getTitle().getPlainText().compareTo(blogName) == 0) {
foundAlbum = myAlbum;
System.out.println("Album Name: " + myAlbum.getTitle().getPlainText());
}
}
return foundAlbum;
}
private static final String METAFEED_URL_BASE = "http://picasaweb.google.com/data/feed/api/user/";
private static final String ALBUM_POST_URL_BASE = "http://picasaweb.google.com/data/feed/api/user/";
private URL feedUrl;
private String username;
private GoogleServiceHandler serviceHandler;
private PicasawebService service;
}