/
picasa.go
586 lines (524 loc) · 16.8 KB
/
picasa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
Copyright 2014 The Camlistore Authors
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 picasa implements an importer for picasa.com accounts.
package picasa // import "camlistore.org/pkg/importer/picasa"
// TODO: removing camliPath from gallery permanode when pic deleted from gallery
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/httputil"
"camlistore.org/pkg/importer"
"camlistore.org/pkg/schema"
"camlistore.org/pkg/schema/nodeattr"
"camlistore.org/pkg/search"
"github.com/tgulacsi/picago"
"go4.org/ctxutil"
"go4.org/syncutil"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
const (
scopeURL = "https://picasaweb.google.com/data/"
// runCompleteVersion is a cache-busting version number of the
// importer code. It should be incremented whenever the
// behavior of this importer is updated enough to warrant a
// complete run. Otherwise, if the importer runs to
// completion, this version number is recorded on the account
// permanode and subsequent importers can stop early.
runCompleteVersion = "4"
// attrPicasaId is used for both picasa photo IDs and gallery IDs.
attrPicasaId = "picasaId"
// acctAttrOAuthToken stores access + " " + refresh + " " + expiry
// See encodeToken and decodeToken.
acctAttrOAuthToken = "oauthToken"
// AttrMediaURL is an attribute set on each picasa photo permanode. It
// is the public URL for fetching the contents of the photo file.
AttrMediaURL = "picasaMediaURL"
)
var (
_ importer.Importer = imp{}
_ importer.ImporterSetupHTMLer = imp{}
)
func init() {
importer.Register("picasa", imp{})
}
// imp is the implementation of the Picasa importer.
type imp struct {
importer.OAuth2
}
func (imp) SupportsIncremental() bool { return true }
type userInfo struct {
ID string // numeric picasa user ID ("11583474931002155675")
Name string // "Jane Smith"
}
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 (imp) IsAccountReady(acctNode *importer.Object) (ok bool, err error) {
if acctNode.Attr(importer.AcctAttrUserID) != "" && acctNode.Attr(acctAttrOAuthToken) != "" {
return true, nil
}
return false, nil
}
func (im imp) SummarizeAccount(acct *importer.Object) string {
ok, err := im.IsAccountReady(acct)
if err != nil || !ok {
return ""
}
if acct.Attr(importer.AcctAttrGivenName) == "" && acct.Attr(importer.AcctAttrFamilyName) == "" {
return fmt.Sprintf("userid %s", acct.Attr(importer.AcctAttrUserID))
}
return fmt.Sprintf("userid %s (%s %s)",
acct.Attr(importer.AcctAttrUserID),
acct.Attr(importer.AcctAttrGivenName),
acct.Attr(importer.AcctAttrFamilyName))
}
func (im imp) ServeSetup(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) error {
oauthConfig, err := im.auth(ctx)
if err == nil {
// we will get back this with the token, so use it for preserving account info
state := "acct:" + ctx.AccountNode.PermanodeRef().String()
// AccessType needs to be "offline", as the user is not here all the time;
// ApprovalPrompt needs to be "force" to be able to get a RefreshToken
// everytime, even for Re-logins, too.
//
// Source: https://developers.google.com/youtube/v3/guides/authentication#server-side-apps
http.Redirect(w, r, oauthConfig.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.ApprovalForce), 302)
}
return err
}
// CallbackURLParameters returns the needed callback parameters - empty for Google Picasa.
func (im imp) CallbackURLParameters(acctRef blob.Ref) url.Values {
return url.Values{}
}
func (im imp) ServeCallback(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) {
oauthConfig, err := im.auth(ctx)
if err != nil {
httputil.ServeError(w, r, fmt.Errorf("Error getting oauth config: %v", err))
return
}
if r.Method != "GET" {
http.Error(w, "Expected a GET", 400)
return
}
code := r.FormValue("code")
if code == "" {
http.Error(w, "Expected a code", 400)
return
}
token, err := oauthConfig.Exchange(ctx, code)
if err != nil {
log.Printf("importer/picasa: token exchange error: %v", err)
httputil.ServeError(w, r, fmt.Errorf("token exchange error: %v", err))
return
}
log.Printf("importer/picasa: got exhanged token.")
picagoCtx := context.WithValue(ctx, ctxutil.HTTPClient, oauthConfig.Client(ctx, token))
userInfo, err := im.getUserInfo(picagoCtx)
if err != nil {
log.Printf("Couldn't get username: %v", err)
httputil.ServeError(w, r, fmt.Errorf("can't get username: %v", err))
return
}
if err := ctx.AccountNode.SetAttrs(
importer.AcctAttrUserID, userInfo.ID,
importer.AcctAttrName, userInfo.Name,
acctAttrOAuthToken, encodeToken(token),
); err != nil {
httputil.ServeError(w, r, fmt.Errorf("Error setting attribute: %v", err))
return
}
http.Redirect(w, r, ctx.AccountURL(), http.StatusFound)
}
// encodeToken encodes the oauth2.Token as
// AccessToken + " " + RefreshToken + " " + Expiry.Unix()
func encodeToken(token *oauth2.Token) string {
if token == nil {
return ""
}
var seconds int64
if !token.Expiry.IsZero() {
seconds = token.Expiry.Unix()
}
return token.AccessToken + " " + token.RefreshToken + " " + strconv.FormatInt(seconds, 10)
}
// decodeToken parses an access token, refresh token, and optional
// expiry unix timestamp separated by spaces into an oauth2.Token.
// It returns as much as it can.
func decodeToken(encoded string) *oauth2.Token {
t := new(oauth2.Token)
f := strings.Fields(encoded)
if len(f) > 0 {
t.AccessToken = f[0]
}
if len(f) > 1 {
t.RefreshToken = f[1]
}
if len(f) > 2 && f[2] != "0" {
sec, err := strconv.ParseInt(f[2], 10, 64)
if err == nil {
t.Expiry = time.Unix(sec, 0)
}
}
return t
}
func (im imp) auth(ctx *importer.SetupContext) (*oauth2.Config, error) {
clientID, secret, err := ctx.Credentials()
if err != nil {
return nil, err
}
conf := &oauth2.Config{
Endpoint: google.Endpoint,
RedirectURL: ctx.CallbackURL(),
ClientID: clientID,
ClientSecret: secret,
Scopes: []string{scopeURL},
}
return conf, nil
}
func (imp) AccountSetupHTML(host *importer.Host) string {
// Picasa doesn't allow a path in the origin. Remove it.
origin := host.ImporterBaseURL()
if u, err := url.Parse(origin); err == nil {
u.Path = ""
origin = u.String()
}
callback := host.ImporterBaseURL() + "picasa/callback"
gphotosURL := host.ImporterBaseURL() + "gphotos"
return fmt.Sprintf(`
<h1>Configuring Picasa</h1>
<p>Please note that because of a bug in the Picasa API, you cannot retrieve more than 10000 photos. If you have more than 10000 photos, you should use the <a href='%s'>Google Photos importer</a> instead.</p>
<p>Visit <a href='https://console.developers.google.com/'>https://console.developers.google.com/</a>
and click <b>"Create Project"</b>.</p>
<p>Then under "APIs & Auth" in the left sidebar, click on "Credentials", then click the button <b>"Create new Client ID"</b>.</p>
<p>Use the following settings:</p>
<ul>
<li>Web application</li>
<li>Authorized JavaScript origins: <b>%s</b></li>
<li>Authorized Redirect URI: <b>%s</b></li>
</ul>
<p>Click "Create Client ID". Copy the "Client ID" and "Client Secret" into the boxes above.</p>
`, gphotosURL, origin, callback)
}
// A run is our state for a given run of the importer.
type run struct {
*importer.RunContext
incremental bool // whether we've completed a run in the past
photoGate *syncutil.Gate
}
var forceFullImport, _ = strconv.ParseBool(os.Getenv("CAMLI_PICASA_FULL_IMPORT"))
func (imp) Run(rctx *importer.RunContext) error {
clientID, secret, err := rctx.Credentials()
if err != nil {
return err
}
acctNode := rctx.AccountNode()
ocfg := &oauth2.Config{
Endpoint: google.Endpoint,
ClientID: clientID,
ClientSecret: secret,
Scopes: []string{scopeURL},
}
token := decodeToken(acctNode.Attr(acctAttrOAuthToken))
baseCtx := rctx.Context()
ctx := context.WithValue(baseCtx, ctxutil.HTTPClient, ocfg.Client(baseCtx, token))
root := rctx.RootNode()
if root.Attr(nodeattr.Title) == "" {
if err := root.SetAttr(
nodeattr.Title,
fmt.Sprintf("%s - Google Photos", acctNode.Attr(importer.AcctAttrName)),
); err != nil {
return err
}
}
r := &run{
RunContext: rctx,
incremental: !forceFullImport && acctNode.Attr(importer.AcctAttrCompletedVersion) == runCompleteVersion,
photoGate: syncutil.NewGate(3),
}
if err := r.importAlbums(ctx); err != nil {
return err
}
if err := acctNode.SetAttrs(importer.AcctAttrCompletedVersion, runCompleteVersion); err != nil {
return err
}
return nil
}
func (r *run) importAlbums(ctx context.Context) error {
albums, err := picago.GetAlbums(ctxutil.Client(ctx), "default")
if err != nil {
return fmt.Errorf("importAlbums: error listing albums: %v", err)
}
albumsNode, err := r.getTopLevelNode("albums", "Albums")
for _, album := range albums {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err := r.importAlbum(ctx, albumsNode, album); err != nil {
return fmt.Errorf("picasa importer: error importing album %s: %v", album, err)
}
}
return nil
}
func (r *run) importAlbum(ctx context.Context, albumsNode *importer.Object, album picago.Album) (ret error) {
if album.ID == "" {
return errors.New("album has no ID")
}
albumNode, err := albumsNode.ChildPathObject(album.ID)
if err != nil {
return fmt.Errorf("importAlbum: error listing album: %v", err)
}
dateMod := schema.RFC3339FromTime(album.Updated)
// Data reference: https://developers.google.com/picasa-web/docs/2.0/reference
// TODO(tgulacsi): add more album info
changes, err := albumNode.SetAttrs2(
attrPicasaId, album.ID,
nodeattr.Type, "picasaweb.google.com:album",
nodeattr.Title, album.Title,
nodeattr.DatePublished, schema.RFC3339FromTime(album.Published),
nodeattr.LocationText, album.Location,
nodeattr.Description, album.Description,
nodeattr.URL, album.URL,
)
if err != nil {
return fmt.Errorf("error setting album attributes: %v", err)
}
if !changes && r.incremental && albumNode.Attr(nodeattr.DateModified) == dateMod {
return nil
}
defer func() {
// Don't update DateModified on the album node until
// we've successfully imported all the photos.
if ret == nil {
ret = albumNode.SetAttr(nodeattr.DateModified, dateMod)
}
}()
log.Printf("Importing album %v: %v/%v (published %v, updated %v)", album.ID, album.Name, album.Title, album.Published, album.Updated)
// TODO(bradfitz): GetPhotos does multiple HTTP requests to
// return a slice of all photos. My "InstantUpload/Auto
// Backup" album has 6678 photos (and growing) and this
// currently takes like 40 seconds. Fix.
photos, err := picago.GetPhotos(ctxutil.Client(ctx), "default", album.ID)
if err != nil {
return err
}
log.Printf("Importing %d photos from album %q (%s)", len(photos), albumNode.Attr(nodeattr.Title),
albumNode.PermanodeRef())
var grp syncutil.Group
for i := range photos {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
photo := photos[i]
r.photoGate.Start()
grp.Go(func() error {
defer r.photoGate.Done()
return r.updatePhotoInAlbum(ctx, albumNode, photo)
})
}
return grp.Err()
}
func (r *run) updatePhotoInAlbum(ctx context.Context, albumNode *importer.Object, photo picago.Photo) (ret error) {
if photo.ID == "" {
return errors.New("photo has no ID")
}
getMediaBytes := func() (io.ReadCloser, error) {
log.Printf("Importing media from %v", photo.URL)
resp, err := ctxutil.Client(ctx).Get(photo.URL)
if err != nil {
return nil, fmt.Errorf("importing photo %s: %v", photo.ID, err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("importing photo %s: status code = %d", photo.ID, resp.StatusCode)
}
return resp.Body, nil
}
var fileRefStr string
idFilename := photo.ID + "-" + photo.Filename
photoNode, err := albumNode.ChildPathObjectOrFunc(idFilename, func() (*importer.Object, error) {
h := blob.NewHash()
rc, err := getMediaBytes()
if err != nil {
return nil, err
}
fileRef, err := schema.WriteFileFromReader(r.Host.Target(), photo.Filename, io.TeeReader(rc, h))
if err != nil {
return nil, err
}
fileRefStr = fileRef.String()
wholeRef := blob.RefFromHash(h)
if pn, err := findExistingPermanode(r.Host.Searcher(), wholeRef); err == nil {
return r.Host.ObjectFromRef(pn)
}
return r.Host.NewObject()
})
if err != nil {
return err
}
if fileRefStr == "" {
fileRefStr = photoNode.Attr(nodeattr.CamliContent)
// Only re-download the source photo if its URL has changed.
// Empirically this seems to work: cropping a photo in the
// photos.google.com UI causes its URL to change. And it makes
// sense, looking at the ugliness of the URLs with all their
// encoded/signed state.
if !mediaURLsEqual(photoNode.Attr(AttrMediaURL), photo.URL) {
rc, err := getMediaBytes()
if err != nil {
return err
}
fileRef, err := schema.WriteFileFromReader(r.Host.Target(), photo.Filename, rc)
rc.Close()
if err != nil {
return err
}
fileRefStr = fileRef.String()
}
}
title := strings.TrimSpace(photo.Description)
if strings.Contains(title, "\n") {
title = title[:strings.Index(title, "\n")]
}
if title == "" && schema.IsInterestingTitle(photo.Filename) {
title = photo.Filename
}
// TODO(tgulacsi): add more attrs (comments ?)
// for names, see http://schema.org/ImageObject and http://schema.org/CreativeWork
attrs := []string{
nodeattr.CamliContent, fileRefStr,
attrPicasaId, photo.ID,
nodeattr.Title, title,
nodeattr.Description, photo.Description,
nodeattr.LocationText, photo.Location,
nodeattr.DateModified, schema.RFC3339FromTime(photo.Updated),
nodeattr.DatePublished, schema.RFC3339FromTime(photo.Published),
nodeattr.URL, photo.PageURL,
}
if photo.Latitude != 0 || photo.Longitude != 0 {
attrs = append(attrs,
nodeattr.Latitude, fmt.Sprintf("%f", photo.Latitude),
nodeattr.Longitude, fmt.Sprintf("%f", photo.Longitude),
)
}
if err := photoNode.SetAttrs(attrs...); err != nil {
return err
}
if err := photoNode.SetAttrValues("tag", photo.Keywords); err != nil {
return err
}
if photo.Position > 0 {
if err := albumNode.SetAttr(
nodeattr.CamliPathOrderColon+strconv.Itoa(photo.Position-1),
photoNode.PermanodeRef().String()); err != nil {
return err
}
}
// Do this last, after we're sure the "camliContent" attribute
// has been saved successfully, because this is the one that
// causes us to do it again in the future or not.
if err := photoNode.SetAttrs(AttrMediaURL, photo.URL); err != nil {
return err
}
return nil
}
func (r *run) getTopLevelNode(path string, title string) (*importer.Object, error) {
childObject, err := r.RootNode().ChildPathObject(path)
if err != nil {
return nil, err
}
if err := childObject.SetAttr(nodeattr.Title, title); err != nil {
return nil, err
}
return childObject, nil
}
var sensitiveAttrs = []string{
nodeattr.Type,
attrPicasaId,
nodeattr.Title,
nodeattr.DateModified,
nodeattr.DatePublished,
nodeattr.Latitude,
nodeattr.Longitude,
nodeattr.Description,
}
// findExistingPermanode finds an existing permanode that has a
// camliContent pointing to a file with the provided wholeRef and
// doesn't have any conflicting attributes that would prevent the
// picasa importer from re-using that permanode for its own use.
func findExistingPermanode(qs search.QueryDescriber, wholeRef blob.Ref) (pn blob.Ref, err error) {
res, err := qs.Query(&search.SearchQuery{
Constraint: &search.Constraint{
Permanode: &search.PermanodeConstraint{
Attr: "camliContent",
ValueInSet: &search.Constraint{
File: &search.FileConstraint{
WholeRef: wholeRef,
},
},
},
},
Describe: &search.DescribeRequest{
Depth: 1,
},
})
if err != nil {
return
}
if res.Describe == nil {
return pn, os.ErrNotExist
}
Res:
for _, resBlob := range res.Blobs {
br := resBlob.Blob
desBlob, ok := res.Describe.Meta[br.String()]
if !ok || desBlob.Permanode == nil {
continue
}
attrs := desBlob.Permanode.Attr
for _, attr := range sensitiveAttrs {
if attrs.Get(attr) != "" {
continue Res
}
}
return br, nil
}
return pn, os.ErrNotExist
}
func mediaURLsEqual(a, b string) bool {
const sub = ".googleusercontent.com/"
ai := strings.Index(a, sub)
bi := strings.Index(b, sub)
if ai >= 0 && bi >= 0 {
return a[ai:] == b[bi:]
}
return a == b
}