Inital commit

This commit is contained in:
Tilman
2011-10-26 09:44:02 +02:00
commit 5971f4b417
1813 changed files with 737837 additions and 0 deletions
@@ -0,0 +1,249 @@
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.photos;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.Link;
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.GphotoEntry;
import com.google.gdata.data.photos.GphotoFeed;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.TagEntry;
import com.google.gdata.data.photos.UserFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* This is a simple client that provides high-level operations on the Picasa Web
* Albums GData API. It can also be used as a command-line application to test
* out some of the features of the API.
*
*
*/
public class PicasawebClient {
private static final String API_PREFIX
= "https://picasaweb.google.com/data/feed/api/user/";
private final PicasawebService service;
/**
* Constructs a new un-authenticated client.
*/
public PicasawebClient(PicasawebService service) {
this(service, null, null);
}
/**
* Constructs a new client with the given username and password.
*/
public PicasawebClient(PicasawebService service, String uname,
String passwd) {
this.service = service;
if (uname != null && passwd != null) {
try {
service.setUserCredentials(uname, passwd);
} catch (AuthenticationException e) {
throw new IllegalArgumentException(
"Illegal username/password combination.");
}
}
}
/**
* Retrieves the albums for the given user.
*/
public List<AlbumEntry> getAlbums(String username) throws IOException,
ServiceException {
String albumUrl = API_PREFIX + username;
UserFeed userFeed = getFeed(albumUrl, UserFeed.class);
List<GphotoEntry> entries = userFeed.getEntries();
List<AlbumEntry> albums = new ArrayList<AlbumEntry>();
for (GphotoEntry entry : entries) {
GphotoEntry adapted = entry.getAdaptedEntry();
if (adapted instanceof AlbumEntry) {
albums.add((AlbumEntry) adapted);
}
}
return albums;
}
/**
* Retrieves the albums for the currently logged-in user. This is equivalent
* to calling {@link #getAlbums(String)} with "default" as the username.
*/
public List<AlbumEntry> getAlbums() throws IOException, ServiceException {
return getAlbums("default");
}
/**
* Retrieves the tags for the given user. These are tags aggregated across
* the entire account.
*/
public List<TagEntry> getTags(String uname) throws IOException,
ServiceException {
String tagUrl = API_PREFIX + uname + "?kind=tag";
UserFeed userFeed = getFeed(tagUrl, UserFeed.class);
List<GphotoEntry> entries = userFeed.getEntries();
List<TagEntry> tags = new ArrayList<TagEntry>();
for (GphotoEntry entry : entries) {
GphotoEntry adapted = entry.getAdaptedEntry();
if (adapted instanceof TagEntry) {
tags.add((TagEntry) adapted);
}
}
return tags;
}
/**
* Retrieves the tags for the currently logged-in user. This is equivalent
* to calling {@link #getTags(String)} with "default" as the username.
*/
public List<TagEntry> getTags() throws IOException, ServiceException {
return getTags("default");
}
/**
* Retrieves the photos for the given album.
*/
public List<PhotoEntry> getPhotos(AlbumEntry album) throws IOException,
ServiceException {
String feedHref = getLinkByRel(album.getLinks(), Link.Rel.FEED);
AlbumFeed albumFeed = getFeed(feedHref, AlbumFeed.class);
List<GphotoEntry> entries = albumFeed.getEntries();
List<PhotoEntry> photos = new ArrayList<PhotoEntry>();
for (GphotoEntry entry : entries) {
GphotoEntry adapted = entry.getAdaptedEntry();
if (adapted instanceof PhotoEntry) {
photos.add((PhotoEntry) adapted);
}
}
return photos;
}
/**
* Retrieves the comments for the given photo.
*/
public List<CommentEntry> getComments(PhotoEntry photo) throws IOException,
ServiceException {
String feedHref = getLinkByRel(photo.getLinks(), Link.Rel.FEED);
AlbumFeed albumFeed = getFeed(feedHref, AlbumFeed.class);
List<GphotoEntry> entries = albumFeed.getEntries();
List<CommentEntry> comments = new ArrayList<CommentEntry>();
for (GphotoEntry entry : entries) {
GphotoEntry adapted = entry.getAdaptedEntry();
if (adapted instanceof CommentEntry) {
comments.add((CommentEntry) adapted);
}
}
return comments;
}
/**
* Retrieves the tags for the given taggable entry. This is valid on user,
* album, and photo entries only.
*/
public List<TagEntry> getTags(GphotoEntry<?> parent) throws IOException,
ServiceException {
String feedHref = getLinkByRel(parent.getLinks(), Link.Rel.FEED);
feedHref = addKindParameter(feedHref, "tag");
AlbumFeed albumFeed = getFeed(feedHref, AlbumFeed.class);
List<GphotoEntry> entries = albumFeed.getEntries();
List<TagEntry> tags = new ArrayList<TagEntry>();
for (GphotoEntry entry : entries) {
GphotoEntry adapted = entry.getAdaptedEntry();
if (adapted instanceof TagEntry) {
tags.add((TagEntry) adapted);
}
}
return tags;
}
/**
* Album-specific insert method to insert into the gallery of the current
* user, this bypasses the need to have a top-level entry object for parent.
*/
public AlbumEntry insertAlbum(AlbumEntry album)
throws IOException, ServiceException {
String feedUrl = API_PREFIX + "default";
return service.insert(new URL(feedUrl), album);
}
/**
* Insert an entry into another entry. Because our entries are a hierarchy,
* this lets you insert a photo into an album even if you only have the
* album entry and not the album feed, making it quicker to traverse the
* hierarchy.
*/
public <T extends GphotoEntry> T insert(GphotoEntry<?> parent, T entry)
throws IOException, ServiceException {
String feedUrl = getLinkByRel(parent.getLinks(), Link.Rel.FEED);
return service.insert(new URL(feedUrl), entry);
}
/**
* Helper function to allow retrieval of a feed by string url, which will
* create the URL object for you. Most of the Link objects have a string
* href which must be converted into a URL by hand, this does the conversion.
*/
public <T extends GphotoFeed> T getFeed(String feedHref,
Class<T> feedClass) throws IOException, ServiceException {
System.out.println("Get Feed URL: " + feedHref);
return service.getFeed(new URL(feedHref), feedClass);
}
/**
* Helper function to add a kind parameter to a url.
*/
public String addKindParameter(String url, String kind) {
if (url.contains("?")) {
return url + "&kind=" + kind;
} else {
return url + "?kind=" + kind;
}
}
/**
* Helper function to get a link by a rel value.
*/
public String getLinkByRel(List<Link> links, String relValue) {
for (Link link : links) {
if (relValue.equals(link.getRel())) {
return link.getHref();
}
}
throw new IllegalArgumentException("Missing " + relValue + " link.");
}
}
@@ -0,0 +1,454 @@
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.photos;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.OtherContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.CommentEntry;
import com.google.gdata.data.photos.GphotoEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.TagEntry;
import com.google.gdata.util.ContentType;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;
/**
* A Command-line application using the PicasawebClient to make calls to the
* Picasa Web Album GData API.
*
*
*/
public class PicasawebCommandLine extends PicasawebClient {
// Input stream for reading user input.
private static final BufferedReader IN
= new BufferedReader(new InputStreamReader(System.in));
/**
* Constructs the command line application.
*/
public PicasawebCommandLine(PicasawebService service, String uname,
String passwd) {
super(service, uname, passwd);
}
/**
* Main loop, runs the client application against the standard service, and
* asks the user for their username and password.
*/
public static void main(String[] args) throws Exception {
try {
PicasawebService service = new PicasawebService("exampleClient");
String uname, passwd;
if (args.length == 2) {
uname = args[0];
passwd = args[1];
} else if (args.length != 0) {
println("Syntax: PicasawebCommandLine <username> <password>");
return;
} else {
uname = getString("Username");
passwd = getString("Password");
}
new PicasawebCommandLine(service, uname, passwd).mainLoop();
} catch (ExitException ee) {
println("Exiting...");
}
}
private void mainLoop() throws Exception {
while (true) {
println("Main Menu:");
println("0) Exit Program");
println("1) Show albums");
println("2) Show user tags");
int choice = getInt("Action");
switch (choice) {
case -1:
continue;
case 0:
return;
case 1:
showAlbums();
break;
case 2:
showUserTags();
break;
default:
println("Invalid choice " + choice);
break;
}
}
}
private void showAlbums() throws Exception {
List<AlbumEntry> albums = getAlbums();
int count = 1;
if (albums.size() == 0) {
println("No albums found.");
} else {
for (AlbumEntry entry : albums) {
println("Album " + count++ + ") " + entry.getTitle().getPlainText());
println(entry.getDescription().getPlainText());
}
}
while (true) {
println("Album Menu:");
println("0) Exit to main menu");
println("1) Show an album's photos");
println("2) Show an album's tags");
println("3) Create a new album");
println("4) Delete an album");
int choice = getInt("Action");
switch (choice) {
case -1:
continue;
case 0:
return;
case 1:
showAlbumPhotos(albums);
break;
case 2:
showAlbumTags(albums);
break;
case 3:
createAlbum();
showAlbums();
return;
case 4:
deleteEntry(albums, "album");
showAlbums();
return;
default:
println("Invalid choice " + choice);
break;
}
}
}
private void showUserTags() throws Exception {
printTags(getTags());
}
private void showAlbumTags(List<AlbumEntry> albums) throws Exception {
AlbumEntry albumEntry;
try {
albumEntry = getEntry(albums, "album");
} catch (ExitException ee) {
return;
}
printTags(getTags(albumEntry));
}
private void createAlbum() throws Exception {
AlbumEntry album = new AlbumEntry();
String title = getString("Title");
album.setTitle(new PlainTextConstruct(title));
String description = getString("Description");
album.setDescription(new PlainTextConstruct(description));
String access = "";
while (!access.equals("public") && !access.equals("private")) {
access = getString("Access (public | private)");
}
album.setAccess(access);
String location = getString("Location");
album.setLocation(location);
album.setDate(new Date());
insertAlbum(album);
}
private void createComment(PhotoEntry photoEntry) throws Exception {
CommentEntry comment = new CommentEntry();
String gphotoId = photoEntry.getGphotoId();
comment.setPhotoId(Long.parseLong(gphotoId));
comment.setTitle(new PlainTextConstruct("Inserted Comment"));
String commentStr = getString("Comment");
comment.setContent(new PlainTextConstruct(commentStr));
insert(photoEntry, comment);
}
private void showAlbumPhotos(List<AlbumEntry> albums) throws Exception {
AlbumEntry albumEntry;
try {
albumEntry = getEntry(albums, "album");
} catch (ExitException ee) {
return;
}
List<PhotoEntry> photos = getPhotos(albumEntry);
int count = 1;
if (photos.size() == 0) {
println("No photos found.");
} else {
for (PhotoEntry entry : photos) {
println("Photo " + count++ + ") " + entry.getTitle().getPlainText());
println(entry.getDescription().getPlainText());
}
}
while (true) {
println("Photo Menu:");
println("0) Exit to album menu");
println("1) Show photo comments");
println("2) Show photo tags");
println("3) Create a new photo");
println("4) Delete a photo");
int choice = getInt("Action");
switch (choice) {
case -1:
continue;
case 0:
return;
case 1:
showPhotoComments(albumEntry, photos);
break;
case 2:
showPhotoTags(albumEntry, photos);
break;
case 3:
createPhoto(albumEntry);
break;
case 4:
deleteEntry(photos, "photo");
break;
default:
println("Invalid choice " + choice);
break;
}
}
}
private void createPhoto(AlbumEntry albumEntry) throws Exception {
PhotoEntry photo = new PhotoEntry();
String title = getString("Title");
photo.setTitle(new PlainTextConstruct(title));
String description = getString("Description");
photo.setDescription(new PlainTextConstruct(description));
photo.setTimestamp(new Date());
OtherContent content = new OtherContent();
File file = null;
while (file == null || !file.canRead()) {
file = new File(getString("Photo location"));
}
content.setBytes(getBytes(file));
content.setMimeType(new ContentType("image/jpeg"));
photo.setContent(content);
insert(albumEntry, photo);
}
private byte[] getBytes(File file) throws FileNotFoundException, IOException {
byte[] result = new byte[(int) file.length()];
new FileInputStream(file).read(result);
return result;
}
private void showPhotoTags(AlbumEntry albumEntry, List<PhotoEntry> photos)
throws Exception {
PhotoEntry photoEntry;
try {
photoEntry = getEntry(photos, "photo");
} catch (ExitException ee) {
return;
}
List<TagEntry> photoTags = getTags(photoEntry);
printTags(photoTags);
while (true) {
println("Tag Menu:");
println("0) Exit to photo menu");
println("1) Add a tag");
println("2) Delete a tag");
int choice = getInt("Action");
switch (choice) {
case -1:
continue;
case 0:
return;
case 1:
createTag(photoEntry);
showPhotoTags(albumEntry, photos);
return;
case 2:
deleteEntry(photoTags, "tag");
showPhotoTags(albumEntry, photos);
return;
default:
println("Invalid choice " + choice);
break;
}
}
}
private void printTags(List<TagEntry> tags) throws Exception {
int count = 1;
if (tags.size() == 0) {
println("No tags found.");
} else {
for (TagEntry tag : tags) {
String tagWeight = tag.getWeight() == null
? "" : " Weight: " + tag.getWeight();
println("Tag " + count++ + ") " + tag.getTitle().getPlainText()
+ tagWeight);
}
}
}
private void createTag(PhotoEntry photoEntry) throws Exception {
TagEntry tag = new TagEntry();
String title = getString("Tag");
tag.setTitle(new PlainTextConstruct(title));
insert(photoEntry, tag);
}
private void showPhotoComments(AlbumEntry albumEntry, List<PhotoEntry> photos)
throws Exception {
PhotoEntry photoEntry;
try {
photoEntry = getEntry(photos, "photo");
} catch (ExitException ee) {
return;
}
List<CommentEntry> comments = getComments(photoEntry);
int count = 1;
if (comments.size() == 0) {
println("No comments found.");
} else {
for (CommentEntry comment : comments) {
println("Comment " + count++ + ") " + comment.getTitle().getPlainText());
println(comment.getPlainTextContent());
}
}
while (true) {
println("Comment Menu:");
println("0) Exit to photo menu");
println("1) Add a comment");
println("2) Delete a comment");
int choice = getInt("Action");
switch (choice) {
case -1:
continue;
case 0:
return;
case 1:
createComment(photoEntry);
showPhotoComments(albumEntry, photos);
return;
case 2:
deleteEntry(comments, "comment");
showPhotoComments(albumEntry, photos);
return;
default:
println("Invalid choice " + choice);
break;
}
}
}
private void deleteEntry(List<? extends GphotoEntry> entries, String name)
throws Exception {
GphotoEntry entry;
try {
entry = getEntry(entries, name);
} catch (ExitException ee) {
return;
}
entry.delete();
}
private <T extends GphotoEntry> T getEntry(List<T> entries, String name)
throws Exception {
while (true) {
int index = getInt("which " + name) - 1;
if (index >= 0 && index < entries.size()) {
return entries.get(index);
} else {
println("Invalid index " + index);
}
}
}
private static int getInt(String name) throws ExitException, IOException {
while (true) {
String input = getString(name);
try {
return Integer.parseInt(input);
} catch (NumberFormatException nfe) {
println("Invalid number " + input);
}
}
}
private static String getString(String name) throws ExitException,
IOException {
print("Please enter ");
print(name);
println(":");
System.out.flush();
String result = IN.readLine();
result = result.trim();
if (result.length() == 0) {
return "-1";
}
return result;
}
private static void print(String str) {
System.out.print(str);
}
private static void println(String str) {
System.out.println(str);
}
private static class ExitException extends Exception {
// Empty, just used to exit quickly.
}
}
@@ -0,0 +1,210 @@
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.photos;
import com.google.gdata.client.Query;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.GphotoEntry;
import com.google.gdata.util.ServiceException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
/**
* Demo of partial response and partial patch to change location on photo
* albums.
*
*
*/
public class PicasawebPartialDemo {
/** Picasaweb feed url prefix. */
private static final String API_PREFIX
= "https://picasaweb.google.com/data/feed/api/user/";
/** Input stream for reading user input. */
private static final BufferedReader IN
= new BufferedReader(new InputStreamReader(System.in));
/** Output stream to write program output. */
private static final PrintStream OUT = System.out;
/** Picasaweb service to talk to the server */
private PicasawebService service;
/** Constructor */
private PicasawebPartialDemo(PicasawebService service) {
this.service = service;
}
/**
* Displays a menu of the main activities a user can perform.
*/
private void printMenu() {
OUT.println("\n");
OUT.println("Choose one of the following demo options:");
OUT.println("\t1) Retrieve my albums with location");
OUT.println("\t2) Update location for an album");
OUT.println("\t0) Exit");
OUT.println("\nEnter Number (0-2): ");
}
/**
* Demonstrates use of partial query to retrieve album title and location
* information for user's albums.
*/
private void printAlbumLocation(String uname)
throws IOException, ServiceException {
String albumsUrl = API_PREFIX + uname;
String fields = "entry(title,gphoto:id,gphoto:location)";
Query albumQuery = new Query(new URL(albumsUrl));
albumQuery.setFields(fields);
AlbumFeed feed = service.query(albumQuery, AlbumFeed.class);
for (GphotoEntry entry : feed.getEntries()) {
if (entry instanceof AlbumEntry) {
AlbumEntry albumEntry = (AlbumEntry) entry;
OUT.println(albumEntry.getGphotoId() + ":"
+ albumEntry.getTitle().getPlainText()
+ " (" + albumEntry.getLocation() + ")");
}
}
}
/**
* Demonstrates update operation using partial patch to update location
* string for specified album.
*/
private void updateAlbumLocation(String uname)
throws IOException, ServiceException {
OUT.println("Enter album id to update:");
String albumId = IN.readLine();
// Get the current album entry
String albumEntryUrl = API_PREFIX + uname + "/" + albumId;
String fields = "@gd:etag,gphoto:location";
Query patchQuery = new Query(new URL(albumEntryUrl));
patchQuery.setFields(fields);
AlbumEntry entry = service.getEntry(patchQuery.getUrl(), AlbumEntry.class);
OUT.println("Current location: " + entry.getLocation());
// Update the location in the album entry
OUT.println("Specify new location: ");
String newLocation = IN.readLine();
entry.setLocation(newLocation);
entry.setSelectedFields("gphoto:location");
AlbumEntry updated = service.patch(new URL(albumEntryUrl), fields, entry);
OUT.println("Location set to: " + updated.getLocation());
}
/** Program entry point. */
public static void main(String args[]) throws IOException, ServiceException {
// parse user input
String uname, passwd;
if (args.length == 2) {
uname = args[0];
passwd = args[1];
} else if (args.length != 0) {
OUT.println(
"Syntax: PicasawebCommandLine <username> <password>");
return;
} else {
uname = getString("Username");
passwd = getString("Password");
}
PicasawebService service = new PicasawebService("gdata-PhotosPartialDemo");
service.setUserCredentials(uname, passwd);
PicasawebPartialDemo demo = new PicasawebPartialDemo(service);
while (true) {
try {
demo.printMenu();
int choice = readInt();
switch (choice) {
case 1:
// Prints out the user's uploaded videos
demo.printAlbumLocation(uname);
break;
case 2:
demo.updateAlbumLocation(uname);
break;
case 0:
System.exit(1);
break;
}
} catch (IOException e) {
// Communications error
System.err.println(
"There was a problem communicating with the service.");
e.printStackTrace();
} catch (ServiceException e) {
// Server side error
System.err.println("The server had a problem handling your request.");
e.printStackTrace();
}
}
}
/** Reads a user string input. */
private static String getString(String name) throws IOException {
OUT.print("Please enter ");
OUT.print(name);
OUT.println(":");
OUT.flush();
String result = IN.readLine();
result = result.trim();
if (result.length() == 0) {
return "-1";
}
return result;
}
/**
* Reads a line of text from the standard input.
*
* @throws IOException If unable to read a line from the standard input.
* @return A line of text read from the standard input.
*/
private static String readLine() throws IOException {
return IN.readLine();
}
/**
* Reads a line of text from the standard input and returns the parsed
* integer representation.
*
* @throws IOException If unable to read a line from the standard input.
* @return An integer read from the standard input.
*/
private static int readInt() throws IOException {
String input = readLine();
try {
return Integer.parseInt(input);
} catch (NumberFormatException nfe) {
return 0;
}
}
}
Binary file not shown.