-
Notifications
You must be signed in to change notification settings - Fork 14
/
goris.go
118 lines (108 loc) · 2.83 KB
/
goris.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
// Package main (goris.go) :
// This file is included all commands and options.
package main
import (
"fmt"
"os"
"goris/ris"
"github.com/urfave/cli"
)
const (
appname = "goris"
)
// dispres : Display results
func dispres(r []string, c int) {
if len(r) < c {
c = len(r)
}
for i := 0; i < c; i++ {
fmt.Printf("%s\n", r[i])
}
}
// handler : Handler of goris
func handler(c *cli.Context) error {
n := c.Int("number")
if n > 100 {
n = 100
}
if len(c.String("fromurl")) == 0 && len(c.String("fromfile")) == 0 {
return fmt.Errorf("no parameters. You can see help by '$ %s -h'", appname)
}
var results []string
var err error
if len(c.String("fromurl")) > 0 && len(c.String("fromfile")) == 0 {
results, err = ris.DefImg(c.Bool("webpages")).ImgFromURL(c.String("fromurl"))
if err != nil {
return err
}
}
if len(c.String("fromurl")) == 0 && len(c.String("fromfile")) > 0 {
fmt.Println("# Specification at Google side has been changed. By this, this command cannot be still modifying. So, in the current stage, this command cannot be used. I apologize for this situation.")
fmt.Println("# Please use goris s -u URL")
os.Exit(0)
// results, err = ris.DefImg(c.Bool("webpages")).ImgFromFile(c.String("fromfile"))
// if err != nil {
// return err
// }
}
if c.Bool("download") && (len(c.String("fromurl")) > 0 || len(c.String("fromfile")) > 0) && !c.Bool("webpages") {
err := ris.Download(results, n)
if err != nil {
return err
}
}
dispres(results, n)
return nil
}
// createHelp : Create help document.
func createHelp() *cli.App {
a := cli.NewApp()
a.Name = appname
a.Authors = []cli.Author{
{Name: "tanaike [ https://github.com/tanaikech/goris ] ", Email: "[email protected]"},
}
a.Usage = "Search for images with Google Reverse Image Search."
a.Version = "3.0.4"
a.Commands = []cli.Command{
{
Name: "search",
Aliases: []string{"s"},
Usage: "[ " + appname + " s -u URL ] or [ " + appname + " s -f file ]",
Description: "Do search images.",
Action: handler,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "fromurl, u",
Usage: "Reverse Image Search from an URL.",
},
&cli.StringFlag{
Name: "fromfile, f",
Usage: "Reverse Image Search from an image file.",
},
&cli.IntFlag{
Name: "number, n",
Usage: "Number of retrieved image URLs. ( 1 - 100 )",
Value: 10,
},
&cli.BoolFlag{
Name: "download, d",
Usage: "Download images from retrieved URLs.",
},
&cli.BoolFlag{
Name: "webpages, w",
Usage: "This is boolean. Retrieve web pages with matching images on Google top page. When this is not used, images are retrieved.",
},
},
},
}
return a
}
// main : Main of goris
func main() {
a := createHelp()
err := a.Run(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}