-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
258 lines (221 loc) · 6.06 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"io/ioutil"
"net/http"
"os"
"os/user"
"regexp"
"strings"
"sync"
"time"
"code.google.com/p/go.text/encoding/traditionalchinese"
"code.google.com/p/go.text/transform"
"github.com/PuerkitoBio/goquery"
"github.com/atotto/clipboard"
"github.com/spf13/cobra"
)
var (
baseDir string
imageId = regexp.MustCompile(`([^\/]+)\.(png|jpg)`)
urlRegex = regexp.MustCompile(`^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$`)
jsonFileGithub = "https://raw.githubusercontent.com/kkdai/webpic/master/parser.json"
)
var configSetting Parser
var targetSiteSetting WebSite
func worker(destDir string, linkChan chan string, wg *sync.WaitGroup) {
defer wg.Done()
for target := range linkChan {
imgInfo := imageId.FindStringSubmatch(target)
if (len(imgInfo) > 0 && strings.EqualFold(imgInfo[2], "gif")) || strings.Contains(target, ".gif") {
//GIF not support for now, skip
continue
}
resp, err := http.Get(target)
if err != nil {
fmt.Printf("Http.Get\nerror: %s\ntarget: %s\n", err, target)
continue
}
defer resp.Body.Close()
m, _, err := image.Decode(resp.Body)
if err != nil {
fmt.Printf("image.Decode\nerror: %s\ntarget: %s\n", err, target)
continue
}
// Ignore small images
bounds := m.Bounds()
if bounds.Size().X > 300 && bounds.Size().Y > 300 {
out, err := os.Create(destDir + "/" + imgInfo[1] + "." + imgInfo[2])
if err != nil {
fmt.Printf("os.Create\nerror: %s", err)
continue
}
defer out.Close()
switch imgInfo[2] {
case "jpg":
jpeg.Encode(out, m, nil)
case "png":
png.Encode(out, m)
}
}
}
}
func BigDecodeUTF8(s []byte) ([]byte, error) {
I := bytes.NewReader(s)
O := transform.NewReader(I, traditionalchinese.Big5.NewDecoder())
d, e := ioutil.ReadAll(O)
if e != nil {
return nil, e
}
return d, nil
}
func findCharacterSet(targetUrl string) string {
resp, _ := http.Get(targetUrl)
return resp.Header.Get("Content-Type")
}
func findDomainByURL(url string) *WebSite {
//using matching first, should goes to regexp
var targetDomain string
tokenStrings := urlRegex.FindStringSubmatch(url)
if len(tokenStrings) > 0 {
targetDomain = tokenStrings[3]
}
for index, webside := range configSetting.SupportSites {
if strings.Contains(targetDomain, webside.WebSite) {
fmt.Println("Use", configSetting.SupportSites[index].WebSite, " parser.")
return &configSetting.SupportSites[index]
}
}
//Cannot find using first as default parser.
fmt.Println("Not in our support list, skip.")
return nil
}
func crawler(target string, workerNum int) {
doc, err := goquery.NewDocument(target)
if err != nil {
panic(err)
}
//find web site from URL.
targetSiteSetting := findDomainByURL(target)
if targetSiteSetting == nil {
return
}
title := doc.Find(targetSiteSetting.TitlePattern).Text()
if targetSiteSetting.ForceBig5 {
byteStr, _ := BigDecodeUTF8([]byte(title))
title = string(byteStr)
}
fmt.Println("[", targetSiteSetting.WebSite, "]:", title, " starting downloading...")
dir := fmt.Sprintf("%v/%v - %v", baseDir, targetSiteSetting.WebSite, title)
os.MkdirAll(dir, 0755)
linkChan := make(chan string)
wg := new(sync.WaitGroup)
for i := 0; i < workerNum; i++ {
wg.Add(1)
go worker(dir, linkChan, wg)
}
doc.Find(targetSiteSetting.ImgPattern).Each(func(i int, img *goquery.Selection) {
imgUrl, _ := img.Attr(targetSiteSetting.ImgAttrPattern)
linkChan <- imgUrl
})
close(linkChan)
wg.Wait()
}
func checkFileExist(fileAdd string) bool {
if _, err := os.Stat(fileAdd); err == nil {
return true
}
return false
}
func reloadParser(jsonFileAddr string) {
//Load parser if exist.
var file []byte
if checkFileExist(jsonFileAddr) {
file, _ = ioutil.ReadFile(jsonFileAddr)
fmt.Println("Trying load local parser file")
} else {
//Download latest one from github.
updateParser(jsonFileAddr)
file, _ = ioutil.ReadFile(jsonFileAddr)
fmt.Println("Trying load local parser file")
}
if len(file) == 0 {
//file not exist, download new one.
fmt.Println("Local parser is empty, load default parser in app.")
file = DefaultJson
}
json.Unmarshal(file, &configSetting)
}
func updateParser(jsonAddr string) {
fmt.Println("Download new parser from github and update.")
out, err := os.Create(jsonAddr)
if err != nil {
return
}
defer out.Close()
resp, err := http.Get(jsonFileGithub)
if err != nil {
return
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
}
func main() {
usr, _ := user.Current()
baseDir = fmt.Sprintf("%v/Pictures/webpic", usr.HomeDir)
os.MkdirAll(baseDir, 0755)
jsonFileAddr := fmt.Sprintf("%s/parser.json", baseDir)
reloadParser(jsonFileAddr)
var postUrl string
var workerNum int
var useDaemon bool
rootCmd := &cobra.Command{
Use: "webpic",
Short: "Download all the images in given post url",
Run: func(cmd *cobra.Command, args []string) {
if useDaemon {
//Check clipboard
var previousString string
fmt.Println("Start watching clipboard.... (press ctrl+c to exit)")
for {
text, err := clipboard.ReadAll()
if previousString != text {
//Found something new in clipboard, check if it is URL.
if err == nil && len(text) > 0 {
urlInfo := urlRegex.FindStringSubmatch(text)
if len(urlInfo) > 0 {
go crawler(text, workerNum)
}
}
previousString = text
}
time.Sleep(time.Second)
}
} else {
if postUrl == "" {
fmt.Println("Please use 'webpic -u URL'.")
return
}
crawler(postUrl, workerNum)
}
},
}
rootCmd.Flags().StringVarP(&postUrl, "url", "u", "", "Url of post")
rootCmd.Flags().IntVarP(&workerNum, "worker", "w", 25, "Number of workers")
rootCmd.Flags().BoolVarP(&useDaemon, "daemon", "d", false, "Enable daemon mode to watch the clipboard.")
updateCmd := &cobra.Command{
Use: "update",
Short: "Download new parser from github and update local.",
Run: func(cmd *cobra.Command, args []string) {
updateParser(jsonFileAddr)
},
}
rootCmd.AddCommand(updateCmd)
rootCmd.Execute()
}