2009年3月3日 星期二

picasa api

這是一個簡單的picasa工具,google有簡單的使用教學。
這個程式,主要將就的相本刪除,以新的相本替換。
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.net.URL;

import com.google.gdata.client.photos.*;
import com.google.gdata.data.*;
import com.google.gdata.data.media.*;
import com.google.gdata.data.photos.*;

/**
*
* @author iMac
*/
public class picasa {

public static void main(String[] args) {
try {
//連結服務端
PicasawebService myService = new PicasawebService("專案名稱(隨便你)");
myService.setUserCredentials("帳號", "密碼");

//相本代碼
String id = "";

//相本代碼存於picasa.id
File file = new File("picasa.id");
if (file.exists()) {
//讀出相本代碼
BufferedReader buffer = new BufferedReader(new FileReader(file));
id = buffer.readLine();

//刪除picasaweb的相本
URL albumPostUrl = new URL("http://picasaweb.google.com/data/entry/api/user/你的大名/albumid/" + id);
AlbumEntry insertedEntry = myService.getEntry(albumPostUrl, AlbumEntry.class);
insertedEntry.delete();
buffer.close();
}

//移至個人資料夾
URL feedUrl = new URL("http://picasaweb.google.com/data/feed/api/user/你的大名?kind=album");

//建立新相本
AlbumEntry myAlbum = new AlbumEntry();
//相本名稱
myAlbum.setTitle(new PlainTextConstruct("相本名稱"));
//相本描述
myAlbum.setDescription(new PlainTextConstruct("相本描述"));
//相本為公開性質
myAlbum.setAccess("public");
//登入伺服器,建立相本
AlbumEntry insertedEntry = myService.insert(feedUrl, myAlbum);

//截取新相本代碼
String s = insertedEntry.getId();
int index = s.lastIndexOf("/");
id = s.substring(index + 1);

//移至新相本
URL albumPostUrl = new URL("http://picasaweb.google.com/data/feed/api/user/你的大名/albumid/" + id);

//建立新照片
PhotoEntry myPhoto = new PhotoEntry();
//照片名稱
myPhoto.setTitle(new PlainTextConstruct("照片名稱"));
//照片描述
myPhoto.setDescription(new PlainTextConstruct("照片描述"));
myPhoto.setClient("myClientName");
//設定上傳檔案
MediaFileSource myMedia = new MediaFileSource(new File("2.JPG"), "image/jpeg");
myPhoto.setMediaSource(myMedia);
//登入伺服器,上傳
PhotoEntry returnedPhoto = myService.insert(albumPostUrl, myPhoto);

//儲存新相本代碼
BufferedWriter buffer = new BufferedWriter(new FileWriter(file));
buffer.write(id);
buffer.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

沒有留言: