Exemple #1
0
func (imp) getUserInfo(ctx context.Context) (*userInfo, error) {
	u, err := picago.GetUser(ctxutil.Client(ctx), "default")
	if err != nil {
		return nil, err
	}
	return &userInfo{ID: u.ID, Name: u.Name}, nil
}
func TestGetUserId(t *testing.T) {
	userID := "11047045264"
	responder := httputil.FileResponder("testdata/users-me-res.xml")
	cl := &http.Client{
		Transport: httputil.NewFakeTransport(map[string]func() *http.Response{
			"https://picasaweb.google.com/data/feed/api/user/default/contacts?kind=user":        responder,
			"https://picasaweb.google.com/data/feed/api/user/" + userID + "/contacts?kind=user": responder,
		})}
	inf, err := picago.GetUser(cl, "default")
	if err != nil {
		t.Fatal(err)
	}
	want := picago.User{
		ID:        userID,
		URI:       "https://picasaweb.google.com/" + userID,
		Name:      "Tamás Gulácsi",
		Thumbnail: "https://lh4.googleusercontent.com/-qqove344/AAAAAAAAAAI/AAAAAAABcbg/TXl3f2K9dzI/s64-c/11047045264.jpg",
	}
	if inf != want {
		t.Errorf("user info = %+v; want %+v", inf, want)
	}
}
Exemple #3
0
// See https://developers.google.com/accounts/docs/OAuth2InstalledApp .
func main() {
	flagID := flag.String("id", os.Getenv("CLIENT_ID"), "application client ID")
	flagSecret := flag.String("secret", os.Getenv("CLIENT_SECRET"), "application client secret")
	flagCode := flag.String("code", os.Getenv("AUTH_CODE"), "authorization code")
	flagTokenCache := flag.String("cache", "token-cache.json", "token cache filename")
	flagDir := flag.String("dir", "", "directory to download images to")
	flagDebugDir := flag.String("debug", "", "set to a valid path to save the response XMLs there")

	flag.Parse()
	picago.DebugDir = *flagDebugDir
	userid := flag.Arg(0)

	client, err := picago.NewClient(*flagID, *flagSecret, *flagCode, *flagTokenCache)
	if err != nil {
		log.Fatalf("error with authorization: %v", err)
	}
	user, err := picago.GetUser(client, "")
	log.Printf("user=%#v err=%v", user, err)

	albums, err := picago.GetAlbums(client, userid)
	if err != nil {
		log.Fatalf("error listing albums: %v", err)
	}
	log.Printf("user %s has %d albums.", userid, len(albums))

	download := *flagDir != ""
	dir, fn := "", ""
	for _, album := range albums {
		albumJ, err := json.Marshal(album)
		if err != nil {
			log.Fatalf("error marshaling %#v: %v", album, err)
		}
		if download {
			dir = filepath.Join(*flagDir, album.Name)
			if err = os.MkdirAll(dir, 0750); err != nil {
				log.Fatalf("cannot create directory %s: %v", dir, err)
			}
			fn = filepath.Join(dir, "album-"+album.Name+".json")
			if err = ioutil.WriteFile(fn, albumJ, 0750); err != nil {
				log.Fatalf("error writing %s: %v", fn, err)
			}
		}
		log.Printf("downloading album %s.", albumJ)
		photos, err := picago.GetPhotos(client, userid, album.ID)
		if err != nil {
			log.Printf("error listing photos of %s: %v", album.ID, err)
			continue
		}
		log.Printf("album %s contains %d photos.", album.ID, len(photos))
		for _, photo := range photos {
			photoJ, err := json.Marshal(photo)
			if err != nil {
				log.Fatalf("error marshaling %#v: %v", photo, err)
			}
			log.Printf("Photo: %s", photoJ)

			if !download {
				continue
			}
			fn = filepath.Join(dir, photo.Filename)
			if err = ioutil.WriteFile(fn+".json", photoJ, 0750); err != nil {
				log.Fatalf("error writing %s.json: %v", fn, err)
			}
			if err = downloadTo(fn, client, photo.URL); err != nil {
				log.Fatalf("downloading %s: %v", photo.URL, err)
			}
		}
	}
}