-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdata.go
239 lines (211 loc) · 6.74 KB
/
testdata.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
/*
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
import (
"encoding/xml"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/httputil"
"camlistore.org/pkg/importer"
"camlistore.org/pkg/osutil"
"github.com/tgulacsi/picago"
)
var _ importer.TestDataMaker = (*imp)(nil)
func (im *imp) SetTestAccount(acctNode *importer.Object) error {
// TODO(mpl): refactor with twitter
return acctNode.SetAttrs(
importer.AcctAttrAccessToken, "fakeAccessToken",
importer.AcctAttrAccessTokenSecret, "fakeAccessSecret",
importer.AcctAttrUserID, "fakeUserId",
importer.AcctAttrName, "fakeName",
importer.AcctAttrUserName, "fakeScreenName",
)
}
func (im *imp) MakeTestData() http.RoundTripper {
const (
apiURL = "https://picasaweb.google.com/data/feed/api"
nAlbums = 10 // Arbitrary number of albums generated.
nEntries = 3 // number of albums or photos returned in the feed at each call.
defaultUserId = "default"
)
albumsListCached := make(map[int]string)
okHeader := `HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
`
responses := make(map[string]func() *http.Response)
// register the get albums list calls
for i := 1; i < nAlbums+1; i += nEntries {
url := fmt.Sprintf("%s/user/%s?start-index=%d", apiURL, defaultUserId, i)
response := okHeader + fakeAlbumsList(i, nAlbums, nEntries, albumsListCached)
responses[url] = httputil.StaticResponder(response)
}
// register the get album calls
for i := 1; i < nAlbums+1; i++ {
albumId := blob.RefFromString(fmt.Sprintf("Album %d", i)).DigestPrefix(10)
for j := 1; j < i+1; j += nEntries {
url := fmt.Sprintf("%s/user/%s/albumid/%s?imgmax=d&start-index=%d", apiURL, defaultUserId, albumId, j)
// Using i as nTotal argument means album N will have N photos in it.
response := okHeader + fakePhotosList(j, i, nEntries)
responses[url] = httputil.StaticResponder(response)
}
}
// register the photo download calls
pudgyPic := fakePhoto()
photoURL1 := "https://camlistore.org/pic/pudgy1.png"
photoURL2 := "https://camlistore.org/pic/pudgy2.png"
responses[photoURL1] = httputil.FileResponder(pudgyPic)
responses[photoURL2] = httputil.FileResponder(pudgyPic)
return httputil.NewFakeTransport(responses)
}
// fakeAlbumsList returns an xml feed of albums. The feed starts at index, and
// ends at index + nEntries (exclusive), or at nTotal (inclusive), whichever is the
// lowest.
func fakeAlbumsList(index, nTotal, nEntries int, cached map[int]string) string {
if cl, ok := cached[index]; ok {
return cl
}
max := index + nEntries
if max > nTotal+1 {
max = nTotal + 1
}
var entries []picago.Entry
for i := index; i < max; i++ {
entries = append(entries, fakeAlbum(i))
}
atom := &picago.Atom{
TotalResults: nTotal,
Entries: entries,
}
feed, err := xml.MarshalIndent(atom, "", " ")
if err != nil {
log.Fatalf("%v", err)
}
cached[index] = string(feed)
return cached[index]
}
func fakeAlbum(counter int) picago.Entry {
author := picago.Author{
Name: "fakeAuthorName",
}
media := &picago.Media{
Description: "fakeAlbumDescription",
Keywords: "fakeKeyword1,fakeKeyword2",
}
title := fmt.Sprintf("Album %d", counter)
year := time.Hour * 24 * 365
month := year / 12
return picago.Entry{
ID: blob.RefFromString(title).DigestPrefix(10),
Published: time.Now().Add(-time.Duration(counter) * year),
Updated: time.Now().Add(-time.Duration(counter) * month),
Name: "fakeAlbumName",
Title: title,
Summary: "fakeAlbumSummary",
Location: "fakeAlbumLocation",
Author: author,
Media: media,
}
}
// fakePhotosList returns an xml feed of an album's photos. The feed starts at
// index, and ends at index + nEntries (exclusive), or at nTotal (inclusive),
// whichever is the lowest.
func fakePhotosList(index, nTotal, nEntries int) string {
max := index + nEntries
if max > nTotal+1 {
max = nTotal + 1
}
var entries []picago.Entry
for i := index; i < max; i++ {
entries = append(entries, fakePhotoEntry(i, nTotal))
}
atom := &picago.Atom{
NumPhotos: nTotal,
Entries: entries,
}
feed, err := xml.MarshalIndent(atom, "", " ")
if err != nil {
log.Fatalf("%v", err)
}
return string(feed)
}
func fakePhotoEntry(photoNbr int, albumNbr int) picago.Entry {
var content picago.EntryContent
if photoNbr%2 == 0 {
content = picago.EntryContent{
URL: "https://camlistore.org/pic/pudgy1.png",
Type: "image/png",
}
}
var point string
if photoNbr%3 == 0 {
point = "37.7447124 -122.4341914"
} else {
point = "45.1822842 5.7141854"
}
mediaContent := picago.MediaContent{
URL: "https://camlistore.org/pic/pudgy2.png",
Type: "image/png",
}
media := &picago.Media{
Title: "fakePhotoTitle",
Description: "fakePhotoDescription",
Keywords: "fakeKeyword1,fakeKeyword2",
Content: []picago.MediaContent{mediaContent},
}
// to be consistent, all the pics times should be anterior to their respective albums times. whatever.
day := time.Hour * 24
year := day * 365
created := time.Now().Add(-time.Duration(photoNbr) * year)
published := created.Add(day)
updated := published.Add(day)
exif := &picago.Exif{
FStop: 7.7,
Make: "whatisthis?", // not obvious to me, needs doc in picago
Model: "potato",
Exposure: 7.7,
Flash: false,
FocalLength: 7.7,
ISO: 100,
Timestamp: created.Unix(),
UID: "whatisthis?", // not obvious to me, needs doc in picago
}
title := fmt.Sprintf("Photo %d of album %d", photoNbr, albumNbr)
return picago.Entry{
ID: blob.RefFromString(title).DigestPrefix(10),
Exif: exif,
Summary: "fakePhotoSummary",
Title: title,
Location: "fakePhotoLocation",
Published: published,
Updated: updated,
Media: media,
Point: point,
Content: content,
}
}
// TODO(mpl): refactor with twitter
func fakePhoto() string {
camliDir, err := osutil.GoPackagePath("camlistore.org")
if err == os.ErrNotExist {
log.Fatal("Directory \"camlistore.org\" not found under GOPATH/src; are you not running with devcam?")
}
if err != nil {
log.Fatalf("Error searching for \"camlistore.org\" under GOPATH: %v", err)
}
return filepath.Join(camliDir, filepath.FromSlash("vendor/embed/glitch/npc_piggy__x1_walk_png_1354829432.png"))
}