Skip to content

Instantly share code, notes, and snippets.

@stuartabrown
Created September 30, 2020 08:22
Show Gist options
  • Save stuartabrown/eef18047d47387e3314a6cd6ed9c73a9 to your computer and use it in GitHub Desktop.
Save stuartabrown/eef18047d47387e3314a6cd6ed9c73a9 to your computer and use it in GitHub Desktop.
Ebay getCompletedItems
exports = async function(payload) {
const axios = require("axios");
const throttledQueue = require('throttled-queue');
var throttle = throttledQueue(5, 1000, true);
// const Bottleneck = require("bottleneck");
// const limiter = new Bottleneck({
// maxConcurrent: 1,
// minTime: 333
// });
// const mongodb = "foo";
// const eventsdb = "bar";
// const eventscoll = "baz";
const mongodb = context.services.get("mongodb-atlas");
const eventsdb = mongodb.db("cards");
const eventscoll = eventsdb.collection("ebay-prices");
//////////// Get data from Mongo
var collection = context.services.get("mongodb-atlas").db("cards").collection("stuart collection");
var ownedCards = await collection.find({status: "Want"}).toArray(); //change back to status:Want after testing.
// var ownedCards = await collection.find({forTrade:"Yes"}).toArray();
for (let card of ownedCards) {
let url = "http://svcs.ebay.com/services/search/FindingService/v1?GLOBAL-ID=EBAY-US&REST-PAYLOAD&keywords=" + card.brand + " " + card.series + " " + card.player +
"&itemFilter(0).name=SoldItemsOnly&itemFilter(0).value=true&OPERATION-NAME=findCompletedItems&paginationInput.entriesPerPage=6&paginationInput.pageNumber=1&RESPONSE-DATA-FORMAT=json&SECURITY-APPNAME=<myapp>&SERVICE-NAME=FindingService&SERVICE-VERSION=1.12.0";
// let searchTerm = card.brand+" "+card.series+" "+card.player;
let cardId = card.card_id;
console.log(url);
throttle(function() {
// make a network request.
axios
.get(url)
.then((resp) => {
// console.log("HERE IS LIMITER"+JSON.stringify(limiter));
for (const item in resp.data.findCompletedItemsResponse[0].searchResult[0].item) {
//console.log("GOING AROUND");
const itemId = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].itemId[0];
const title = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].title[0];
const galleryURL = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].galleryURL[0];
const endDate = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].listingInfo[0].endTime[0];
const price = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].sellingStatus[0].currentPrice[0].__value__;
const currencyId = resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].sellingStatus[0].currentPrice[0]['@currencyId'];
// console.log("END DATE "+resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].listingInfo[0].endTime[0]);
// console.log("PRICE "+resp.data.findCompletedItemsResponse[0].searchResult[0].item[item].sellingStatus[0].currentPrice[0].__value__);
const result = eventscoll.updateOne({
itemId: itemId
}, {
$addToSet: {
cardId: cardId
},
$set: {
itemId: itemId,
title: title,
endDate: endDate,
price: price,
currencyId: currencyId,
// cardId: [cardId],
galleryURL: galleryURL
}
}, {
upsert: true
})
}
})
});
// .catch((error) => {
// if (error.message.indexOf('A job with the same id already exists') === 0) {
// // ignore
// } else {
// // handle other errors
// }
// }
// // )
// );
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment