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
|
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Smugup uploads a collection of photos to SmugMug.
//
// Run 'smugup -help' for details.
package main
import (
"crypto/md5"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/mattermost/rsc/keychain"
"github.com/mattermost/rsc/smugmug"
)
const apiKey = "8qH4UgiunBKpsYpvcBXftbCYNEreAZ0m"
var usageMessage = `usage: smugup [options] 'album title' [photo.jpg ...]
Smugup creates a new album with the given title if one does not already exist.
Then uploads the list of images to the album. If a particular image
already exists in the album, the JPG replaces the album image if the
contents differ.
Smugup fetches the SmugMug user name and password from the
user's keychain.
By default, new albums are created as private as possible: not public,
not world searchable, and not SmugMug-searchable.
Smugup prints the URL for the album when finished.
The options are:
-u user
SmugMug user account name (email address).
This is not typically needed, as the user name found in the keychain will be used.
`
var smugUser = flag.String("u", "", "SmugMug user name")
func usage() {
fmt.Fprint(os.Stderr, usageMessage)
os.Exit(2)
}
func main() {
flag.Usage = usage
flag.Parse()
log.SetFlags(0)
args := flag.Args()
if len(args) < 1 {
usage()
}
title, files := args[0], args[1:]
user, passwd, err := keychain.UserPasswd("smugmug.com", *smugUser)
if err != nil {
log.Fatal(err)
}
smug, err := smugmug.Login(user, passwd, apiKey)
if err != nil {
log.Fatal(err)
}
albums, err := smug.Albums(smug.NickName)
if err != nil {
log.Fatal(err)
}
var a *smugmug.Album
for _, a = range albums {
if a.Title == title {
goto HaveAlbum
}
}
a, err = smug.CreateAlbum(title)
if err != nil {
log.Fatal(err)
}
HaveAlbum:
imageFiles := map[string]*smugmug.ImageInfo{}
if len(files) > 0 {
images, err := smug.Images(a)
if err != nil {
log.Fatal(err)
}
n := 0
c := make(chan *smugmug.ImageInfo)
rate := make(chan bool, 4)
for _, image := range images {
go func(image *smugmug.Image) {
rate <- true
info, err := smug.ImageInfo(image)
<-rate
if err != nil {
log.Print(err)
c <- nil
return
}
c <- info
}(image)
n++
}
for i := 0; i < n; i++ {
info := <-c
if info == nil {
continue
}
imageFiles[info.FileName] = info
}
}
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Print(err)
continue
}
_, elem := filepath.Split(file)
info := imageFiles[elem]
if info != nil {
h := md5.New()
h.Write(data)
digest := fmt.Sprintf("%x", h.Sum(nil))
if digest == info.MD5Sum {
// Already have that image.
continue
}
_, err = smug.ReplaceImage(file, data, &smugmug.Image{ID: info.ID})
} else {
_, err = smug.AddImage(file, data, a)
}
if err != nil {
log.Print(err)
}
}
info, err := smug.AlbumInfo(a)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", info.URL)
}
|