-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
165 lines (142 loc) · 3.94 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"image"
"image/jpeg"
"image/png"
"log"
"net/http"
"os"
"os/user"
"strings"
"sync"
fb "github.com/huandu/facebook"
)
var pageName = flag.String("n", "", "Facebook page name such as: scottiepippen")
var numOfWorkersPtr = flag.Int("c", 2, "the number of concurrent rename workers. default = 2")
var m sync.Mutex
var TOKEN string
func init() {
TOKEN = os.Getenv("FBTOKEN")
}
func downloadWorker(destDir string, linkChan chan DLData, wg *sync.WaitGroup) {
defer wg.Done()
for target := range linkChan {
var imageType string
if strings.Contains(target.ImageURL, ".png") {
imageType = ".png"
} else {
imageType = ".jpg"
}
resp, err := http.Get(target.ImageURL)
if err != nil {
log.Println("Http.Get\nerror: " + err.Error() + "\ntarget: " + target.ImageURL)
continue
}
defer resp.Body.Close()
m, _, err := image.Decode(resp.Body)
if err != nil {
log.Println("image.Decode\nerror: " + err.Error() + "\ntarget: " + target.ImageURL)
continue
}
// Ignore small images
bounds := m.Bounds()
if bounds.Size().X > 300 && bounds.Size().Y > 300 {
out, err := os.Create(destDir + "/" + target.ImageID + imageType)
if err != nil {
log.Println("os.Create\nerror: %s", err)
continue
}
defer out.Close()
if imageType == ".png" {
png.Encode(out, m)
} else {
jpeg.Encode(out, m, nil)
}
}
}
}
func findPhotoByAlbum(ownerName string, albumName string, albumId string, baseDir string, photoCount int, photoOffset int) {
photoRet := FBPhotos{}
var queryString string
if photoOffset > 0 {
queryString = fmt.Sprintf("/%s/photos?limit=%d&offset=%d", albumId, photoCount, photoOffset)
} else {
queryString = fmt.Sprintf("/%s/photos?limit=%d", albumId, photoCount)
}
resPhoto := runFBGraphAPI(queryString)
parseMapToStruct(resPhoto, &photoRet)
dir := fmt.Sprintf("%v/%v/%v - %v", baseDir, ownerName, albumId, albumName)
os.MkdirAll(dir, 0755)
linkChan := make(chan DLData)
wg := new(sync.WaitGroup)
for i := 0; i < 1; i++ {
wg.Add(1)
go downloadWorker(dir, linkChan, wg)
}
for _, v := range photoRet.Data {
dlChan := DLData{}
dlChan.ImageID = v.ID
dlChan.ImageURL = v.Source
linkChan <- dlChan
}
}
func parseMapToStruct(inData interface{}, decodeStruct interface{}) {
jret, _ := json.Marshal(inData)
err := json.Unmarshal(jret, &decodeStruct)
if err != nil {
log.Fatalln(err)
}
}
func runFBGraphAPI(query string) (queryResult interface{}) {
res, err := fb.Get(query, fb.Params{
"access_token": TOKEN,
})
if err != nil {
log.Fatalln("FB connect error, err=", err.Error())
}
return res
}
func main() {
flag.Parse()
var inputPage string
if TOKEN == "" {
log.Fatalln("Set your FB token as environment variables 'export FBTOKEN=XXXXXXX'")
}
if *pageName == "" {
log.Fatalln("You need to input -n=Name_or_Id.")
}
inputPage = *pageName
//Get system user folder
usr, _ := user.Current()
baseDir := fmt.Sprintf("%v/Pictures/goFBPages", usr.HomeDir)
//Get User info
resUser := runFBGraphAPI("/" + inputPage)
userRet := FBUser{}
parseMapToStruct(resUser, &userRet)
//Get all albums
resAlbums := runFBGraphAPI("/" + inputPage + "/albums")
albumRet := FBAlbums{}
parseMapToStruct(resAlbums, &albumRet)
//use limit to avoid error: Please reduce the amount of data you're asking for, then retry your request
//Curently 30 is a magic number of FB Graph API call, 50 will still occur failed. >_<
maxCount := 30
userFolderName := fmt.Sprintf("[%s]%s", userRet.Username, userRet.Name)
for _, v := range albumRet.Data {
fmt.Println("Starting download ["+v.Name+"]-"+v.From.Name, " total count:", v.Count)
if v.Count > maxCount {
currentOffset := 0
for {
if currentOffset > v.Count {
break
}
findPhotoByAlbum(userFolderName, v.Name, v.ID, baseDir, maxCount, currentOffset)
currentOffset = currentOffset + maxCount
}
} else {
findPhotoByAlbum(userFolderName, v.Name, v.ID, baseDir, v.Count, 0)
}
}
}