-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathoptions.go
167 lines (155 loc) · 4.33 KB
/
options.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
// Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package diffbot
import (
"net/http"
"net/url"
"strconv"
"time"
)
// Options holds the optional parameters for Diffbot client.
//
// See http://diffbot.com/products/automatic/
type Options struct {
Fields string
Timeout time.Duration
Callback string
FrontpageAll string
ClassifierMode string
ClassifierStats string
BulkNotifyEmail string
BulkNotifyWebHook string
BulkRepeat string
BulkMaxRounds string
BulkPageProcessPattern string
CrawlMaxToCrawl string
CrawlMaxToProcess string
CrawlRestrictDomain string
CrawlNotifyEmail string
CrawlNotifyWebHook string
CrawlDelay string
CrawlRepeat string
CrawlOnlyProcessIfNew string
CrawlMaxRounds string
BatchMethod string
BatchRelativeUrl string
CustomHeader http.Header
}
// MethodParamString return string as the url params.
//
// If the Options is not empty, the return string begin with a '&'.
func (p *Options) MethodParamString(method string) string {
if p == nil || method == "" {
return ""
}
switch method {
case "article", "image", "product":
var s []byte
if p.Fields != "" {
s = append(s, ("&fields=" + p.Fields)...)
}
if p.Timeout != 0 {
timeout := strconv.FormatInt(int64(p.Timeout/time.Millisecond), 10)
s = append(s, ("&timeout=" + timeout)...)
}
if p.Callback != "" {
s = append(s, ("&callback=" + url.QueryEscape(p.Callback))...)
}
return string(s)
case "frontpage":
var s []byte
if p.Timeout != 0 {
timeout := strconv.FormatInt(int64(p.Timeout/time.Millisecond), 10)
s = append(s, ("&timeout=" + timeout)...)
}
if p.FrontpageAll != "" {
s = append(s, ("&all=" + p.FrontpageAll)...)
}
return string(s)
case "analyze":
var s []byte
if p.ClassifierMode != "" {
s = append(s, ("&mode=" + p.ClassifierMode)...)
}
if p.Fields != "" {
s = append(s, ("&fields=" + p.Fields)...)
}
if p.ClassifierStats != "" {
s = append(s, ("&stats=" + p.ClassifierStats)...)
}
return string(s)
case "bulk":
var s []byte
if p.BulkNotifyEmail != "" {
s = append(s, ("¬ifyEmail=" + p.BulkNotifyEmail)...)
}
if p.BulkNotifyWebHook != "" {
s = append(s, ("¬ifyWebHook=" + p.BulkNotifyWebHook)...)
}
if p.BulkRepeat != "" {
s = append(s, ("&repeat=" + p.BulkRepeat)...)
}
if p.BulkMaxRounds != "" {
s = append(s, ("&maxRounds=" + p.BulkMaxRounds)...)
}
if p.BulkPageProcessPattern != "" {
s = append(s, ("&pageProcessPattern=" + p.BulkPageProcessPattern)...)
}
return string(s)
case "crawl":
var s []byte
if p.CrawlMaxToCrawl != "" {
s = append(s, ("&maxToCrawl=" + p.CrawlMaxToCrawl)...)
}
if p.CrawlMaxToProcess != "" {
s = append(s, ("&maxToProcess=" + p.CrawlMaxToProcess)...)
}
if p.CrawlRestrictDomain != "" {
s = append(s, ("&restrictDomain=" + p.CrawlRestrictDomain)...)
}
if p.CrawlNotifyEmail != "" {
s = append(s, ("¬ifyEmail=" + p.CrawlNotifyEmail)...)
}
if p.CrawlNotifyWebHook != "" {
s = append(s, ("¬ifyWebHook=" + p.CrawlNotifyWebHook)...)
}
if p.CrawlDelay != "" {
s = append(s, ("&crawlDelay=" + p.CrawlDelay)...)
}
if p.CrawlRepeat != "" {
s = append(s, ("&repeat=" + p.CrawlRepeat)...)
}
if p.CrawlOnlyProcessIfNew != "" {
s = append(s, ("&onlyProcessIfNew=" + p.CrawlOnlyProcessIfNew)...)
}
if p.CrawlMaxRounds != "" {
s = append(s, ("&maxRounds=" + p.CrawlMaxRounds)...)
}
return string(s)
case "batch":
var s []byte
if p.Timeout != 0 {
timeout := strconv.FormatInt(int64(p.Timeout/time.Millisecond), 10)
s = append(s, ("&timeout=" + timeout)...)
}
if p.BatchMethod != "" {
s = append(s, ("&method=" + p.BatchMethod)...)
}
if p.BatchRelativeUrl != "" {
s = append(s, ("&relative_url=" + url.QueryEscape(p.BatchRelativeUrl))...)
}
return string(s)
default: // Custom APIs
var s []byte
if p.Timeout != 0 {
timeout := strconv.FormatInt(int64(p.Timeout/time.Millisecond), 10)
s = append(s, ("&timeout=" + timeout)...)
}
if p.Callback != "" {
s = append(s, ("&callback=" + url.QueryEscape(p.Callback))...)
}
return string(s)
}
return ""
}