package de.tilman.picasa;
import java.net.URL;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.CommentEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.PhotoFeed;
import com.google.gdata.data.photos.UserFeed;

public class PicasaTest {

	public static void main(String[] args) {

		/*
		 * http://code.google.com/apis/gdata/javadoc/
		 * http://code.google.com/apis/picasaweb/docs/2.0/developers_guide_java.html#GettingStarted
		 * http://code.google.com/apis/gdata/articles/eclipse.html
		 */

		try {

			PicasawebService service = new PicasawebService("exampleCo-exampleApp-1");
			service.setUserCredentials("twalther@gmx.net", args[0]);

			URL feedUrl = new URL("https://picasaweb.google.com/data/feed/api/user/default?kind=album");

			UserFeed userFeed = service.getFeed(feedUrl, UserFeed.class);

			for (AlbumEntry album : userFeed.getAlbumEntries()) {
				System.out.println(album.getTitle().getPlainText() + ", " + album.getId());
			}

			feedUrl = new URL("https://picasaweb.google.com/data/feed/api/user/default/albumid/5582087736992348017");

			AlbumFeed albumFeed = service.getFeed(feedUrl, AlbumFeed.class);

			for (PhotoEntry photo : albumFeed.getPhotoEntries()) {

				System.out.println("\nTitle: " + photo.getTitle().getPlainText());
				System.out.println("Date: " + photo.getTimestamp());
				System.out.println("Description: " + photo.getDescription().getPlainText());
				System.out.println("ID: " + photo.getId());

				if (photo.getCommentCount() > 0) {
					PhotoFeed photoFeed = photo.getFeed();
					for (CommentEntry comment : photoFeed.getCommentEntries()) {
						System.out.println(comment.getPlainTextContent());
					}
					
					photo.setDescription(new PlainTextConstruct("Hier wurden Kommentare gefunden und automatisch eine Bildbeschreibung gesetzt"));
					System.out.println("New Description: " + photo.getDescription().getPlainText());
					photo.update();
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
