-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebay.js
94 lines (83 loc) · 2.62 KB
/
ebay.js
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
const baseURL = "https://onyx-elevator-270422.appspot.com/";
function getURL(parameters) {
let url = new URL(baseURL);
for (let [key, value] of Object.entries(parameters)) {
url.searchParams.append(key, value);
}
return url.href;
}
function getJSON(url) {
return fetch(url).then(response => response.json());
}
// Searchs eBay for items that match the given keyword. Returns these in an
// array of objects.
//
// REQUIRED PARAMS:
// keyword (string): the keywords to search for items with. this can be multiple
// words, just put a space between them in a single string
// results (integer): number of results to return
//
// OPTIONAL PARAMS:
// postalCode (integer): the postal code of the buyer
// maxRadius (integer): the maximum radius around the postalCode to search for
//
// Returns in this format:
//
function findItemsByKeywordsAndRadius(keyword, results, postalCode, maxRadius) {
if (!keyword || !results) {
return [];
}
let parameters = {
"OPERATION-NAME": "findItemsByKeywords",
"SERVICE-VERSION": "1.0.0",
"SECURITY-APPNAME": "ebayhack-ecoBay-PRD-669e8f5f8-11393d83",
"GLOBAL-ID": "EBAY-US",
"RESPONSE-DATA-FORMAT": "JSON",
keywords: keyword,
"paginationInput.entriesPerPage": results
};
if (postalCode && maxRadius) {
parameters["buyerPostalCode"] = postalCode;
parameters["itemFilter(0).name"] = "LocalSearchOnly";
parameters["itemFilter(0).value"] = true;
parameters["itemFilter(1).name"] = "MaxDistance";
parameters["itemFilter(1).value"] = 100;
}
let url = getURL(parameters);
return getJSON(url).then(raw => {
let rawItems = raw.findItemsByKeywordsResponse[0].searchResult[0].item;
if (rawItems == undefined) {
return [];
} else {
let newItems = [];
for (let item of rawItems) {
newItems.push({
title: item.title[0],
imageURL: item.galleryURL[0],
location: item.location[0],
price: parseFloat(item.sellingStatus[0].currentPrice[0].__value__),
itemURL: item.viewItemURL[0]
});
}
return newItems;
}
});
}
function findItemsByKeywords(keyword, results) {
return findItemsByKeywordsAndRadius(keyword, results, null, null);
}
/*
EXAMPLE: prints out the top 5 results for 'shoes' on eBay:
findItemsByKeywords('shoes', 5).then((items) => {
for (let item of items) {
console.log(item);
}
});
EXAMPLE: prints out the top 5 results for 'red shirt', with a radius of 10 miles
around the Zip Code 98195.
findItemsByKeywordsAndRadius('red shirt', 5, 98195, 10).then((items) => {
for (let item of items) {
console.log(item);
}
});
*/