forked from rmpacheco/go-ebay
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathebay.go
226 lines (206 loc) · 7.2 KB
/
ebay.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
package ebay
import (
"encoding/xml"
"errors"
"net/url"
"strconv"
"time"
"github.com/heatxsink/go-httprequest"
)
const (
GLOBAL_ID_EBAY_US = "EBAY-US"
GLOBAL_ID_EBAY_FR = "EBAY-FR"
GLOBAL_ID_EBAY_DE = "EBAY-DE"
GLOBAL_ID_EBAY_IT = "EBAY-IT"
GLOBAL_ID_EBAY_ES = "EBAY-ES"
)
// Item for sale on EBay
type Item struct {
ItemID string `xml:"itemId"`
Title string `xml:"title"`
Location string `xml:"location"`
CurrentPrice float64 `xml:"sellingStatus>currentPrice"`
ShippingPrice float64 `xml:"shippingInfo>shippingServiceCost"`
BinPrice float64 `xml:"listingInfo>buyItNowPrice"`
ShipsTo []string `xml:"shippingInfo>shipToLocations"`
ListingURL string `xml:"viewItemURL"`
ImageURL string `xml:"galleryURL"`
GalleryPlusPictureURL string `xml:"galleryPlusPictureURL"`
Condition struct {
Text string `xml:",chardata"`
ConditionId string `xml:"conditionId"`
ConditionDisplayName string `xml:"conditionDisplayName"`
} `xml:"condition"`
PostalCode string `xml:"postalCode"`
PaymentMethod []string `xml:"paymentMethod"`
AutoPay string `xml:"autoPay"`
Country string `xml:"country"`
Site string `xml:"globalId"`
EndTime time.Time `xml:"listingInfo>endTime"`
}
// FindItemsResponse from EBay
type FindItemsResponse struct {
XMLName xml.Name `xml:"findItemsByKeywordsResponse"`
Items []Item `xml:"searchResult>item"`
Timestamp string `xml:"timestamp"`
PaginationOutput struct {
PageNumber string `xml:"pageNumber"`
EntriesPerPage string `xml:"entriesPerPage"`
TotalPages string `xml:"totalPages"`
TotalEntries string `xml:"totalEntries"`
} `xml:"paginationOutput"`
}
// FindCompletedItemsResponse from EBay
type FindCompletedItemsResponse struct {
XMLName xml.Name `xml:"findCompletedItemsResponse"`
Items []Item `xml:"searchResult>item"`
Timestamp string `xml:"timestamp"`
}
// ErrorMessage from EBay
type ErrorMessage struct {
XMLName xml.Name `xml:"errorMessage"`
Error Error `xml:"error"`
}
// Error response from EBay
type Error struct {
ErrorID string `xml:"errorId"`
Domain string `xml:"domain"`
Severity string `xml:"severity"`
Category string `xml:"category"`
Message string `xml:"message"`
SubDomain string `xml:"subdomain"`
}
// EBay API request
type EBay struct {
ApplicationID string
HTTPRequest *httprequest.HttpRequest
}
type soldURL func(string, string, int) (string, error)
type searchURL func(string, string, int, bool) (string, error)
// New EBay API request
func New(applicationID string) *EBay {
e := EBay{}
e.ApplicationID = applicationID
e.HTTPRequest = httprequest.NewWithDefaults()
return &e
}
func (e *EBay) buildSoldURL(globalID string, keywords string, pageNumber int, entriesPerPage int) (string, error) {
filters := url.Values{}
filters.Add("itemFilter(0).name", "Condition")
filters.Add("itemFilter(0).value(0)", "Used")
filters.Add("itemFilter(0).value(1)", "Unspecified")
filters.Add("itemFilter(1).name", "SoldItemsOnly")
filters.Add("itemFilter(1).value(0)", "true")
return e.buildURL(globalID, keywords, "findCompletedItems", pageNumber, entriesPerPage, filters)
}
func (e *EBay) buildSearchURL(globalID string, keywords string, pageNumber int, entriesPerPage int, binOnly bool) (string, error) {
filters := url.Values{}
filters.Add("itemFilter(0).name", "ListingType")
filters.Add("itemFilter(0).value(0)", "AuctionWithBIN")
if !binOnly {
filters.Add("itemFilter(0).value(1)", "FixedPrice")
filters.Add("itemFilter(0).value(2)", "Auction")
}
return e.buildURL(globalID, keywords, "findItemsByKeywords", pageNumber, entriesPerPage, filters)
}
func (e *EBay) buildURL(globalID string, keywords string, operationName string, pageNumber int, entriesPerPage int, filters url.Values) (string, error) {
var u *url.URL
u, err := url.Parse("https://svcs.ebay.com/services/search/FindingService/v1")
if err != nil {
return "", err
}
params := url.Values{}
params.Add("OPERATION-NAME", operationName)
params.Add("SERVICE-VERSION", "1.0.0")
params.Add("SECURITY-APPNAME", e.ApplicationID)
params.Add("GLOBAL-ID", globalID)
params.Add("RESPONSE-DATA-FORMAT", "XML")
params.Add("REST-PAYLOAD", "")
params.Add("keywords", keywords)
params.Add("paginationInput.pageNumber", strconv.Itoa(pageNumber))
params.Add("paginationInput.entriesPerPage", strconv.Itoa(entriesPerPage))
for key := range filters {
for _, val := range filters[key] {
params.Add(key, val)
}
}
u.RawQuery = params.Encode()
return u.String(), err
}
func (e *EBay) findItems(globalID string, keywords string, entriesPerPage int, url string) (FindItemsResponse, error) {
var response FindItemsResponse
headers := make(map[string]string)
headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
body, statusCode, err := e.HTTPRequest.Get(url, headers)
if err != nil {
return response, err
}
if statusCode != 200 {
var em ErrorMessage
err = xml.Unmarshal([]byte(body), &em)
if err != nil {
return response, err
}
return response, errors.New(em.Error.Message)
}
err = xml.Unmarshal([]byte(body), &response)
if err != nil {
return response, err
}
return response, err
}
// FindItemsByKeywords returns items matching the keyword search terms
func (e *EBay) FindItemsByKeywords(globalID string, keywords string, pageNumber int, entriesPerPage int, binOnly bool) (FindItemsResponse, error) {
var response FindItemsResponse
url, err := e.buildSearchURL(globalID, keywords, pageNumber, entriesPerPage, binOnly)
if err != nil {
var response FindItemsResponse
return response, err
}
headers := make(map[string]string)
headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
body, statusCode, err := e.HTTPRequest.Get(url, headers)
if err != nil {
return response, err
}
if statusCode != 200 {
var em ErrorMessage
err = xml.Unmarshal([]byte(body), &em)
if err != nil {
return response, err
}
return response, errors.New(em.Error.Message)
}
err = xml.Unmarshal([]byte(body), &response)
if err != nil {
return response, err
}
return response, err
}
// FindSoldItems returns sold items by keyword
func (e *EBay) FindSoldItems(globalID string, keywords string, pageNumber int, entriesPerPage int) (FindCompletedItemsResponse, error) {
var response FindCompletedItemsResponse
url, err := e.buildSoldURL(globalID, keywords, pageNumber, entriesPerPage)
if err != nil {
return response, err
}
headers := make(map[string]string)
headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
body, statusCode, err := e.HTTPRequest.Get(url, headers)
if err != nil {
return response, err
}
if statusCode != 200 {
var em ErrorMessage
err = xml.Unmarshal([]byte(body), &em)
if err != nil {
return response, err
}
return response, errors.New(em.Error.Message)
}
err = xml.Unmarshal([]byte(body), &response)
if err != nil {
return response, err
}
return response, err
}