-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflip.go
More file actions
277 lines (232 loc) · 6.13 KB
/
flip.go
File metadata and controls
277 lines (232 loc) · 6.13 KB
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
package main
import (
"fmt"
"encoding/json"
"errors"
"flag"
"ipernity"
"io/ioutil"
"os"
"strconv"
)
const (
last_doc_file = "ipernity_last_doc"
)
type Doc struct {
Docid string `json:"docid"`
OriginalUrl string `json:"originalurl"`
OriginalSize int `json:"originalsize"`
Ext string `json:"ext"`
Media string `json:"media"`
Title string `json:"title"`
Description string `json:"description"`
Date string `json:"date"`
Albums []string `json:"albums"`
FamilyVisible bool `json:"family"`
FriendVisible bool `json:"friends"`
PublicVisible bool `json:"public"`
}
type Docslice []Doc
var (
Docs Docslice
)
func main() {
startdocPtr := flag.Int("startdoc", 0, "starting document to download (0 means most recent undownloaded document)")
numdocsPtr := flag.Int("numdocs", 0, "number of documents to download (0 means all)")
flag.Parse()
startdoc := *startdocPtr
numdocs := *numdocsPtr
fmt.Println("startdoc: ", startdoc)
fmt.Println("numdocs: ", numdocs)
err := ipernity.Login()
if err != nil {
fmt.Println("Error logging in to ipernity: " + err.Error())
os.Exit(1)
}
totaldocs, err := GetNumDocs()
fmt.Println("total number of documents ", totaldocs)
if err != nil {
fmt.Println("Error getting number of pictures on ipernity: " + err.Error())
os.Exit(2)
}
if startdoc < 1 {
startdocascii, err := ioutil.ReadFile(last_doc_file)
if err != nil {
startdoc = 1
} else {
startdoc, err = strconv.Atoi(string(startdocascii))
if (err != nil) || (startdoc < 1) {
fmt.Println("corrupted ipernity last doc file")
os.Exit(5)
}
}
}
if numdocs < 1 {
numdocs = totaldocs - startdoc
}
if numdocs < 1 {
fmt.Println("No documents to download")
os.Exit(0)
}
if startdoc < 1 {
fmt.Println("Must start at at least document 1")
os.Exit(6)
}
err = EnumeratePictures(startdoc, numdocs, totaldocs)
if err != nil {
fmt.Println("Error enumerating pictures on ipernity: " + err.Error())
os.Exit(3)
}
err = SavePictures(startdoc)
if err != nil {
fmt.Println("Error saving picture from ipernity: " + err.Error())
os.Exit(4)
}
}
func strToBool(s string) bool {
if s != "0" {
return true
}
return false
}
func GetNumDocs() (int, error) {
usergetdata, err := ipernity.Call_user_get("")
if err != nil {
return 0, err
}
numdocs, err := strconv.Atoi(usergetdata.User.Count.Docs)
if err != nil {
return 0, err
}
return numdocs, nil
}
func EnumeratePictures(startdoc int, numdocs int, totalnumdocs int) error {
var (
doclist []ipernity.Docgetlist
)
if (startdoc < 1) || (numdocs < 1) || (totalnumdocs < 1) {
return errors.New("all function arguments must be >= 1")
}
fmt.Println("startdoc " + strconv.Itoa(startdoc))
fmt.Println("numdocs " + strconv.Itoa(numdocs))
fmt.Println("totalnumdocs " + strconv.Itoa(totalnumdocs))
numpages := (totalnumdocs / 100)
if (totalnumdocs % 100) != 0 {
numpages++
}
fmt.Println("numpages " + strconv.Itoa(numpages))
// XXX: only download the pages we need
startdocpage := startdoc / 100
if (startdoc % 100) != 0 {
startdocpage++
}
enddocpage := (startdoc + numdocs) / 100
if ((startdoc + numdocs) % 100) != 0 {
enddocpage++
}
fmt.Printf("startdocpage %v enddocpage %v\n", startdocpage, enddocpage)
for page := 1; page <= numpages; page++ {
fmt.Printf("Downloading photo list data page %v of %v\n",
page, numpages)
tmplist, err := ipernity.Call_doc_getList("", page, "")
if err != nil {
return err
}
doclist = append(doclist, tmplist)
}
k := 0
docpos := 0
// a list of pages of docs
for i := len(doclist) - 1; i >= 0; i-- {
// if we're done, exit the loop over the pages
if docpos >= (numdocs + startdoc) {
break
}
// one page of docs
for j := len(doclist[i].Docs.Doc) - 1; j >= 0; j-- {
// if we're done, exit the loop over the page
if docpos < startdoc {
docpos++
continue;
}
// if we shouldn't start yet, skip to the next doc
if docpos >= (numdocs + startdoc) {
break
}
if (k % 10) == 0 {
fmt.Printf("Document metadata %v\n", k)
}
Docs = append(Docs, Doc{Docid: doclist[i].Docs.Doc[j].Doc_id})
docinfo, err := ipernity.Call_doc_get(Docs[k].Docid, "")
if err != nil {
return err
}
Docs[k].OriginalUrl = docinfo.Doc.Original.Url
Docs[k].OriginalSize, _ = strconv.Atoi(docinfo.Doc.Original.Bytes)
Docs[k].Title = docinfo.Doc.Title
Docs[k].FamilyVisible = strToBool(docinfo.Doc.Visibility.Isfamily)
Docs[k].FriendVisible = strToBool(docinfo.Doc.Visibility.Isfriend)
Docs[k].PublicVisible = strToBool(docinfo.Doc.Visibility.Ispublic)
Docs[k].Description = docinfo.Doc.Description
Docs[k].Date = docinfo.Doc.Dates.Created
Docs[k].Ext = docinfo.Doc.Original.Ext
Docs[k].Media = docinfo.Doc.Media
if docinfo.Doc.Count.Albums != "0" {
docconts, err := ipernity.Call_doc_getContainers(Docs[k].Docid)
if err != nil {
return err
}
numalbums, err := strconv.Atoi(docconts.Albums.Total)
if err != nil {
return err
}
for l := 0; l < numalbums; l++ {
Docs[k].Albums = append(Docs[k].Albums, docconts.Albums.Album[l].Title)
}
}
k++
docpos++
}
}
return nil
}
func SavePicture(doc Doc, i int, startdoc int) error {
resp, err := ipernity.HttpClient.Get(doc.OriginalUrl)
defer resp.Body.Close()
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
if len(body) != doc.OriginalSize {
fmt.Printf("downloaded length: %v original length %v\n",
len(body), doc.OriginalSize)
return errors.New("length mismatch for URL " + doc.OriginalUrl)
}
err = ioutil.WriteFile(doc.Docid+"."+doc.Ext, body, 0644)
if err != nil {
return err
}
docjson, err := json.Marshal(doc)
if err != nil {
return err
}
err = ioutil.WriteFile(doc.Docid+".json", docjson, 0644)
if err != nil {
return err
}
istr := strconv.Itoa(i + startdoc + 1)
err = ioutil.WriteFile(last_doc_file, []byte(istr), 0644)
return nil
}
func SavePictures(startdoc int) error {
for i, doc := range Docs {
if (i % 10) == 0 {
fmt.Printf("downloading doc %v\n", i)
}
err := SavePicture(doc, i, startdoc)
if err != nil {
return err
}
}
return nil
}