You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
2.2 KiB
JavaScript
55 lines
2.2 KiB
JavaScript
const superagent = require('superagent');
|
|
const LotListing = require('./LotListing')
|
|
const brickLinkAgent = superagent.agent();
|
|
module.exports = {
|
|
getInternalPartId(legoPartNumber) {
|
|
return new Promise((resolve, reject) => {
|
|
brickLinkAgent.get(`https://www.bricklink.com/v2/catalog/catalogitem.page?P=${legoPartNumber}`)
|
|
// .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:107.0) Gecko/20100101 Firefox/107.0')
|
|
.then((response => {
|
|
const partIdRegex = /idItem:\s+(?<idItem>\d+)/gm;
|
|
const matches = partIdRegex.exec(response.text);
|
|
resolve(matches.groups?.idItem)
|
|
})).catch(error => {
|
|
console.error('HTTP error from internalPartId', error);
|
|
reject(error);
|
|
})
|
|
|
|
})
|
|
},
|
|
getListings(internalPartId) {
|
|
return new Promise((resolve, reject) => {
|
|
brickLinkAgent.get(`https://www.bricklink.com/ajax/clone/catalogifs.ajax`)
|
|
.query({
|
|
itemid: internalPartId,
|
|
loc: 'US'
|
|
})
|
|
// .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:107.0) Gecko/20100101 Firefox/107.0')
|
|
.set('Accept', 'application/json').then(response => {
|
|
if(!response.body.list) {
|
|
return reject('no list found');
|
|
}
|
|
resolve(response.body.list.map(l => new LotListing(l)));
|
|
}).catch(error => {
|
|
console.error('HTTP error from getListings', error)
|
|
reject(error);
|
|
})
|
|
})
|
|
|
|
},
|
|
getStoreId(sellerUsername) {
|
|
return new Promise((resolve, reject) => {
|
|
brickLinkAgent.get(`https://store.bricklink.com/${sellerUsername}`)
|
|
.then((response => {
|
|
const storeIdRegex = /username:\s+'.+',\s+id:\s+(?<storeId>\d+)/gm;
|
|
const matches = storeIdRegex.exec(response.text);
|
|
resolve(matches.groups?.storeId)
|
|
})).catch(error => {
|
|
console.error('HTTP error from getStoreId', error)
|
|
reject(error);
|
|
})
|
|
|
|
})
|
|
}
|
|
}
|