Last active
October 18, 2017 03:55
-
-
Save aflansburg/34d99ab1012ddeff35d636f59abed4ea to your computer and use it in GitHub Desktop.
Update multiple images using CSV file and custom uri parser (specific application)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node v 8.5.0 (should work with earlier as long as regex available) & ES6 | |
const axios = require('axios'); | |
const fs = require('fs'); | |
const parse = require('csv-parse'); | |
const delimiter = ','; | |
const filename = 'update.csv'; | |
const endpoint = 'https://api.ebay.com/ws/api.dll'; | |
let results = []; | |
fs.createReadStream(filename) | |
.pipe(parse({delimiter: delimiter})) | |
.on('data', function (row) { | |
let obj = {}; | |
if (row[0].length > 0) { | |
obj.itemId = row[0]; | |
obj.uris = []; | |
for (let i = 0; i < row.length; i++) { | |
let index = i + 1; | |
if (row[index]) { | |
if (row[i+1].indexOf("cache") >= 0){ | |
obj.uris.push(cleanImgUri(row[i + 1])); | |
} | |
else { | |
obj.uris.push(row[i+1]); | |
} | |
} | |
} | |
} | |
results.push(obj); | |
results.forEach((item,index)=>{ | |
if(!item){ | |
results.delete(index); | |
} | |
}) | |
results = results.filter(val=> Object.keys(val).length !== 0); | |
}) | |
.on('end', function () { | |
// console.log(results); | |
results.forEach(item=>{ | |
updateImage(item.itemId, item.uris); | |
}) | |
}); | |
function updateImage(itemId, uris){ | |
let aBody = '<?xml version="1.0" encoding="utf-8"?>' + | |
'<ReviseItemRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\"><RequesterCredentials>' + | |
'<eBayAuthToken>****REDACTED****</eBayAuthToken>' + | |
`</RequesterCredentials><Item><ItemID>${itemId}</ItemID><PrimaryCategory><CategoryID>33585</CategoryID></PrimaryCategory>` + | |
'<PictureDetails>'; | |
let zBody = '</PictureDetails></Item></ReviseItemRequest>'; | |
uris.forEach(uri=>{ | |
aBody += '<PictureURL>' + uri + '</PictureURL>'; | |
}); | |
let body = aBody + zBody; | |
// console.log(aBody + zBody); | |
axios.defaults.headers.common['Content-Type'] = 'text/xml'; | |
axios.defaults.headers.common['X-EBAY-API-COMPATIBILITY-LEVEL'] = 967; | |
axios.defaults.headers.common['X-EBAY-API-DEV-NAME'] = 'REDACTED'; | |
axios.defaults.headers.common['X-EBAY-API-APP-NAME'] = 'REDACTED'; | |
axios.defaults.headers.common['X-EBAY-API-CERT-NAME'] = 'REDACTED'; | |
axios.defaults.headers.common['X-EBAY-API-SITEID'] = 0; | |
axios.defaults.headers.common['X-EBAY-API-CALL-NAME'] = 'ReviseItem'; | |
axios.post(endpoint, body) | |
.then(response=>{ | |
let xml = response; | |
// let json = convert.xml2json(xml, {compact: true}); | |
console.log(xml); | |
console.log('operation complete'); | |
}) | |
.catch(error=>{ | |
console.log(error); | |
}) | |
} | |
function cleanImgUri(uri){ | |
const regex = /^(.*product)\/cache.*(\/\w\/\w\/.*)/g; | |
let m; | |
let uriArr = []; | |
let cleanUri; | |
while ((m = regex.exec(uri)) !== null) { | |
if (m.index === regex.lastIndex) { | |
regex.lastIndex++; | |
} | |
uriArr.push(m[1]); | |
uriArr.push(m[2]); | |
cleanUri = uriArr.join(''); | |
// m.forEach((match, groupIndex) => { | |
// console.log(`Found match, group ${groupIndex}: ${match}`); | |
// }) | |
} | |
return cleanUri; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would have to supply X-EBAY-API application/dev credentials for the seller PROD or SANDBOX account as well as the and customize the uri cleaner if needed (or just take it out). Keep in mind each readable/parseable row on the csv is a subsequent API call (eBay limit is 5000 calls per day).