-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanimegif.go
151 lines (125 loc) · 2.99 KB
/
animegif.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time"
)
const defaultCount = 8
const maxCount = 80
const defaultKeyword = "yuyushiki"
const defaultPage = 1
func main() {
keyword, count, page := args()
urls := fetchUrls(keyword, count, page)
if len(urls) == 0 {
fmt.Println("no image found")
return
}
html := generateHtml(urls)
openHtml(html)
}
func args() (keyword string, count, page int) {
flag.StringVar(&keyword, "k", defaultKeyword, "keyword")
flag.IntVar(&count, "c", defaultCount, "count")
flag.IntVar(&page, "p", defaultPage, "initial page")
flag.Parse()
if count > maxCount {
count = maxCount
}
return keyword, count, page
}
func fetchUrls(keyword string, count, page int) (urls []string) {
var _urls []string
for len(urls) < count {
_urls = search(page, keyword)
if len(_urls) == 0 {
return urls
}
urls = append(urls, _urls...)
page += 1
}
return urls
}
func generateHtml(urls []string) (html string) {
htmls := []string{"<!DOCTYPE HTML><html><body>"}
for _, url := range urls {
htmls = append(htmls, "<a href='"+url+"' target='_blank'><img src='"+url+"' /></a>")
}
htmls = append(htmls, "</body></html>")
return strings.Join(htmls, "")
}
func openHtml(html string) {
file, err := ioutil.TempFile(os.TempDir(), "animegif")
printError(err)
ioutil.WriteFile(file.Name(), []byte(html), 0644)
openCommand, err := findOpenCommand()
printError(err)
err = exec.Command(openCommand, file.Name()).Start()
printError(err)
time.Sleep(time.Second * 1)
defer os.Remove(file.Name())
}
var openCommands = []string{
"open", // Mac OS X
"xdg-open", // Linux
}
func findOpenCommand() (string, error) {
for _, c := range openCommands {
if _, err := exec.LookPath(c); err == nil {
return c, nil
}
}
return "", fmt.Errorf("Cannot find open commands: %v", openCommands)
}
func printError(err error) {
if err != nil {
panic(err)
}
}
type ResultType struct {
Url string `json:"url"`
}
type ResponseDataType struct {
Results []ResultType `json:"results"`
}
type ResponseType struct {
ResponseData ResponseDataType `json:"responseData"`
}
func search(page int, keyword string) (urls []string) {
perPage := 8
base := "http://ajax.googleapis.com/ajax/services/search/images?"
start := (page-1)*perPage + 1
params := url.Values{
"q": {keyword},
"rsz": {fmt.Sprint(perPage)},
"safe": {"off"},
"v": {"1.0"},
"as_filetype": {"gif"},
"imgsz": {"large"},
"start": {fmt.Sprint(start)},
"as_sitesearch": {"tumblr.com"},
}
body := openUrl(base + params.Encode())
var response ResponseType
err := json.Unmarshal(body, &response)
printError(err)
for _, value := range response.ResponseData.Results {
urls = append(urls, value.Url)
}
return urls
}
func openUrl(req string) (body []byte) {
res, err := http.Get(req)
printError(err)
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
printError(err)
return body
}