Permalink
Browse files

improved picasaweb exchange: made uploading cert more reliable

  • Loading branch information...
uwedeportivo committed Jun 8, 2011
1 parent b5678ce commit 006651f4220af56325f117795c2f1f90b73c56a8
Showing with 59 additions and 75 deletions.
  1. +54 −72 goclient/kindi/picasa.go
  2. +5 −3 goclient/main.go
View
@@ -37,6 +37,8 @@ import (
"os"
"strings"
"io/ioutil"
+ "time"
+ "strconv"
"goauth2.googlecode.com/hg/oauth"
)
@@ -60,65 +62,67 @@ func jsonPath(object interface{}, path string) interface{} {
return o[keys[len(keys) - 1]]
}
-func fetchKindiAlbumIds(user string) ([]string, os.Error) {
+func fetchKindiAlbumId(user string) (string, os.Error) {
url := "https://picasaweb.google.com/data/feed/api/user/" + user + "?alt=json"
httpResponse, _, err := http.DefaultClient.Get(url)
if err != nil {
- return nil, err
+ return "", err
}
defer httpResponse.Body.Close()
if httpResponse.StatusCode >= 300 {
rb, _ := ioutil.ReadAll(httpResponse.Body)
- fmt.Printf("fetchKindiAlbumIds failed: response body = %s\n", rb)
- return nil, fmt.Errorf("fetchKindiAlbumIds: got status code %d from http.Get(%s)", httpResponse.StatusCode, url)
+ fmt.Printf("fetchKindiAlbumId failed: response body = %s\n", rb)
+ return "", fmt.Errorf("fetchKindiAlbumId: got status code %d from http.Get(%s)", httpResponse.StatusCode, url)
}
var jsonResponse interface{}
err = json.NewDecoder(httpResponse.Body).Decode(&jsonResponse)
if err != nil {
- return nil, err
+ return "", err
}
albumsList := jsonPath(jsonResponse, "feed/entry")
if albumsList == nil {
- return nil, nil
+ return "", nil
}
albums := albumsList.([]interface{})
if len(albums) == 0 {
- return nil, nil
+ return "", nil
}
- rv := make([]string, 0, 10)
+ var albumId string = ""
+ var timestamp uint64 = 0
for i := range albums {
albumTitle := jsonPath(albums[i], "title/$t").(string)
- if albumTitle == "__com.codemanic.kindi__" {
- albumId := jsonPath(albums[i], "gphoto$id/$t").(string)
- rv = append(rv, albumId)
+ if albumTitle == kindiAlbumName {
+ ts, err := strconv.Atoui64(jsonPath(albums[i], "gphoto$timestamp/$t").(string))
+ if err != nil {
+ continue
+ }
+
+ if ts > timestamp {
+ albumId = jsonPath(albums[i], "gphoto$id/$t").(string)
+ timestamp = ts
+ }
}
}
- return rv, nil
+ return albumId, nil
}
func fetchImageURL(user string) (string, os.Error) {
- albumIds, err := fetchKindiAlbumIds(user)
+ albumId, err := fetchKindiAlbumId(user)
if err != nil {
return "", err
}
- if len(albumIds) != 1 {
- return "", nil
- }
-
- albumId := albumIds[0]
-
if len(albumId) == 0 {
return "", nil
}
@@ -132,6 +136,10 @@ func fetchImageURL(user string) (string, os.Error) {
defer httpResponse.Body.Close()
+ if httpResponse.StatusCode == 404 {
+ return "", nil
+ }
+
if httpResponse.StatusCode >= 300 {
rb, _ := ioutil.ReadAll(httpResponse.Body)
fmt.Printf("fetchImageURL failed: response body = %s\n", rb)
@@ -178,6 +186,10 @@ func fetchCertBytes(user string) ([]byte, os.Error) {
defer httpResponse.Body.Close()
+ if httpResponse.StatusCode == 404 {
+ return nil, nil
+ }
+
if httpResponse.StatusCode >= 300 {
rb, _ := ioutil.ReadAll(httpResponse.Body)
fmt.Printf("fetchCertBytes: response body = %s\n", rb)
@@ -256,78 +268,48 @@ func uploadCertPNG(path string) os.Error {
}
func createKindiAlbum(httpClient *http.Client) (string, os.Error) {
- albumIds, err := fetchKindiAlbumIds(myGmail)
+ albumCreateReader := bytes.NewBuffer([]byte(fmt.Sprintf(albumCreateBodyTemplate, time.Seconds() * 1000)))
+ url := "https://picasaweb.google.com/data/feed/api/user/" + myGmail + "?alt=json"
+ httpResponse, err := httpClient.Post(url, "application/atom+xml", albumCreateReader)
if err != nil {
return "", err
}
+
+ defer httpResponse.Body.Close()
- if len(albumIds) > 0 {
- for i := range albumIds {
- albumId := albumIds[i]
-
- url := "https://picasaweb.google.com/data/entry/api/user/" + myGmail + "/albumid/" + albumId
+ if httpResponse.StatusCode >= 300 {
+ rb, _ := ioutil.ReadAll(httpResponse.Body)
+ fmt.Printf("createKindiAlbum post failed: response body = %s\n", rb)
+ return "", fmt.Errorf("createKindiAlbum: post: got status code %d from http.Post(%s)", httpResponse.StatusCode, url)
+ }
- deleteRequest, err := http.NewRequest("DELETE", url, nil)
- if err != nil {
- return "", err
- }
- deleteRequest.Header.Add("If-Match", "*")
-
- httpResponse, err := httpClient.Do(deleteRequest)
- if err != nil {
- return "", err
- }
+ var jsonResponse interface{}
- defer httpResponse.Body.Close()
-
- if httpResponse.StatusCode >= 300 {
- rb, _ := ioutil.ReadAll(httpResponse.Body)
- fmt.Printf("delete existing failed: response body = %s\n", rb)
- return "", fmt.Errorf("createKindiAlbum: delete existing kindi album: got status code %d from %s http.Do(%#v)", httpResponse.StatusCode, url, deleteRequest)
- }
- }
+ err = json.NewDecoder(httpResponse.Body).Decode(&jsonResponse)
+ if err != nil {
+ return "", err
}
- albumCreateBody :=
+ return jsonPath(jsonResponse, "entry/gphoto$id/$t").(string), nil
+}
+
+const kindiAlbumName = "kindi"
+
+const albumCreateBodyTemplate =
`<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:media='http://search.yahoo.com/mrss/'
xmlns:gphoto='http://schemas.google.com/photos/2007'>
- <title type='text'>__com.codemanic.kindi__</title>
+ <title type='text'>kindi</title>
<summary type='text'>kindi certificate</summary>
- <gphoto:location>Universe</gphoto:location>
+ <gphoto:location>kindi app</gphoto:location>
<gphoto:access>public</gphoto:access>
- <gphoto:timestamp>1152255600000</gphoto:timestamp>
+ <gphoto:timestamp>%d</gphoto:timestamp>
<media:group>
<media:keywords>kindi, encryption</media:keywords>
</media:group>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/photos/2007#album'></category>
</entry>`
- albumCreateReader := bytes.NewBuffer([]byte(albumCreateBody))
- url := "https://picasaweb.google.com/data/feed/api/user/" + myGmail
- httpResponse, err := httpClient.Post(url, "application/atom+xml", albumCreateReader)
- if err != nil {
- return "", err
- }
-
- defer httpResponse.Body.Close()
-
- if httpResponse.StatusCode >= 300 {
- rb, _ := ioutil.ReadAll(httpResponse.Body)
- fmt.Printf("createKindiAlbum post failed: response body = %s\n", rb)
- return "", fmt.Errorf("createKindiAlbum: post: got status code %d from http.Post(%s)", httpResponse.StatusCode, url)
- }
-
- albumIds, err = fetchKindiAlbumIds(myGmail)
- if err != nil {
- return "", err
- }
-
- if len(albumIds) != 1 {
- return "", fmt.Errorf("failed to create kindi album")
- }
- return albumIds[0], nil
-}
View
@@ -37,10 +37,11 @@ import (
)
const baseUrl = "https://uwe-oauth.appspot.com"
+const versionStr = "1.2"
func usage() {
- fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
- fmt.Fprintf(os.Stderr, "\t%s [--help] [--to <gmail address>] <file>\n", os.Args[0])
+ fmt.Fprintf(os.Stderr, "%s version %s:\n", os.Args[0], versionStr)
+ fmt.Fprintf(os.Stderr, "\t%s [--help] [--version] [--to <gmail address>] <file>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\tif --to flag is present, then kindi encrypts, otherwise it decrypts\n")
}
@@ -49,11 +50,12 @@ func main() {
configDir := flag.String("config", "", "path to config directory")
help := flag.Bool("help", false, "show this message")
+ version := flag.Bool("version", false, "show version")
to := flag.String("to", "", "recipient gmail address")
flag.Parse()
- if *help {
+ if *help || *version {
flag.Usage()
os.Exit(0)
}

0 comments on commit 006651f

Please sign in to comment.