-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
129 lines (115 loc) · 2.9 KB
/
utils.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
/*
*Gawain Open Source Project
*Author: Gawain Antarx
*Create Date: 2018-七月-15
*
*/
package main
import (
"github.com/parnurzeal/gorequest"
"log"
"os"
)
const(
dbQueryURL = `https://api.douban.com/v2/movie/search`
dbMovieURL = `https://api.douban.com/v2/movie/subject/`
)
type Movies []MovieInfo
type MovieInfo struct {
Title string
OriginalTitle string `json:"original_title"`
Year string
Images ImageInfo
Id string
Rating Rates
}
type ImageInfo struct {
Small string
Large string
Medium string
}
type SearchInfo struct {
Count int
Start int
Total int
Subjects Movies
}
type Rates struct{
Max int
Average float32
Stars string
Min int
}
type ConnectionClient struct {
req *gorequest.SuperAgent
path string
}
func (cli ConnectionClient) Init(){
cli.req = gorequest.New()
}
func (cli ConnectionClient) Search(title string, limits int){
cond := "q="+title
var res = SearchInfo{}
if cli.req == nil{
cli.req = gorequest.New()
}
_, _, err := cli.req.Get(dbQueryURL+"?"+cond).EndStruct(&res)
//log.Println(resp.Status)
//log.Println("body:"+string(body))
if err != nil{
log.Println(err)
}else{
//log.Println(res.Subjects)
cli.searchMovies(res.Subjects,limits)
}
}
func (cli ConnectionClient) searchMovies(movies Movies,limits int) {
log.Println("Searching imgs...")
cli.CheckPath()
if limits == 0 {
for _, m := range movies {
_, imageBytes, errs := cli.req.Get(m.Images.Large).EndBytes()
if errs != nil {
log.Println(errs)
} else {
//log.Println("Write images..."+m.Title)
image, e := os.Create(m.Title + "-" + m.Year + ".jpg")
if e != nil {
log.Println(e)
} else {
image.Write(imageBytes)
}
}
}
return
}
for index, m := range movies {
//log.Println(m)
if index + 1 <= limits {
_, imageBytes, errs := cli.req.Get(m.Images.Large).EndBytes()
if errs != nil {
log.Println(errs)
} else {
image, e := os.Create( cli.path+"/"+m.Year + m.Title + ".jpg")
if e != nil {
log.Println(e)
} else {
image.Write(imageBytes)
}
}
} else {
return
}
}
}
func (cli ConnectionClient) CheckPath() bool {
_, err := os.Stat(cli.path)
if err == nil{
return true
}else if os.IsNotExist(err){
os.Mkdir(cli.path,os.ModePerm)
return true
}else{
return false
}
}