Last active
April 18, 2021 23:12
-
-
Save witnessmenow/a9f02c87207993347f3a6b1adb4a97a6 to your computer and use it in GitHub Desktop.
TindieApiToCSV
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
const axios = require('axios'); | |
const createCsvWriter = require('csv-writer').createObjectCsvWriter; | |
let fsp = require("fs").promises; | |
const defaultWeight = '100'; | |
const userName = 'tindieName' | |
const tindieAPIKey = '1234567865r4ewdfghgf' | |
function formatOrderForCSV (order){ | |
const newFormat = { | |
OrderNumber: order.number, | |
Type: "Large", | |
Destination: order.shipping_country_code.replace(/\r?\n|\r/g, " "), | |
Country: order.shipping_country.replace(/\r?\n|\r/g, " "), | |
WeightCat: defaultWeight, | |
Contents: 'Other', | |
Service: '', | |
FirstName: '', | |
LastName: '', | |
CompanyName: order.company_title.replace(/\r?\n|\r/g, " "), | |
Email: order.email.replace(/\r?\n|\r/g, " "), | |
Phone: order.phone.replace(/\-|\(|\)|\r?\n|\r/g, ""), | |
AddressLine1: order.shipping_street.replace(/\r?\n|\r/g, " "), | |
AddressLine2: order.shipping_city.replace(/\r?\n|\r/g, " "), | |
CityState: order.shipping_state.replace(/\r?\n|\r/g, " "), | |
Postcode: order.shipping_postcode.replace(/\r?\n|\r/g, " "), | |
ContentCategory: 'Sale', | |
ItemDescription0: '', | |
Quantity0: '', | |
Value0: '', | |
Weight0: '', | |
ItemDescription1: '', | |
Quantity1: '', | |
Value1: '', | |
Weight1: '' | |
} | |
switch(order.shipping_service){ | |
case 'An Post Registered Postage': | |
newFormat.Service = 'Registered'; | |
break; | |
default: | |
newFormat.Service = 'UNKNOWN!!!!'; | |
} | |
if(order.shipping_name.indexOf(' ') > 0){ | |
newFormat.FirstName = order.shipping_name.substr(0,order.shipping_name.lastIndexOf(' ')).replace(/\r?\n|\r/g, " "); | |
newFormat.LastName = order.shipping_name.substr(order.shipping_name.lastIndexOf(' ')+1).replace(/\r?\n|\r/g, " "); | |
} else { | |
newFormat.FirstName = '-'; | |
newFormat.LastName = order.shipping_name.replace(/\r?\n|\r/g, " "); | |
} | |
// Todo: | |
// Items | |
//Power BloughR | |
const powerBLoughRItem = order.items.find(e => e.sku === '14727'); | |
if(powerBLoughRItem) | |
{ | |
newFormat.ItemDescription0 = 'USB Connector (No Battery)'; | |
newFormat.Quantity0 = powerBLoughRItem.quantity; | |
newFormat.Value0 = ((powerBLoughRItem.price_unit * 0.83)* powerBLoughRItem.quantity).toFixed(0); | |
newFormat.Weight0 = '0.007'; | |
} | |
let itemQuantities = 0; | |
let itemsTotalValue = 0; | |
//22g | |
const notPowerBLoughR = order.items.filter(e => e.sku != '14727'); | |
if(notPowerBLoughR.length > 0){ | |
notPowerBLoughR.forEach(order => { | |
itemQuantities += order.quantity; | |
itemsTotalValue += (order.price_unit * 0.83) * order.quantity; | |
}) | |
if(powerBLoughRItem) | |
{ | |
newFormat.ItemDescription1 = 'Electronic Kit (No Battery)'; | |
newFormat.Quantity1 = itemQuantities; | |
newFormat.Value1 = (itemsTotalValue).toFixed(0); | |
newFormat.Weight1 = '0.022'; | |
} else { | |
newFormat.ItemDescription0 = 'Electronic Kit (No Battery)'; | |
newFormat.Quantity0 = itemQuantities; | |
newFormat.Value0 = (itemsTotalValue).toFixed(0); | |
newFormat.Weight0 = '0.022'; | |
} | |
} | |
return newFormat; | |
} | |
// Optionally the request above could also be done as | |
axios.get('https://www.tindie.com/api/v1/order', { | |
params: { | |
format: 'json', | |
shipped: 'false', | |
limit: '100', | |
username: userName, | |
api_key: tindieAPIKey, | |
} | |
}) | |
.then(function (response) { | |
const csvFormat = response.data.orders.map(order => { | |
return formatOrderForCSV(order) | |
}); | |
return fsp.copyFile('data.csv', 'data_bak.csv').then(() => { | |
const csvWriter = createCsvWriter({ | |
path: 'data.csv', | |
header: [ | |
{id: 'OrderNumber', title: 'OrderNumber'}, | |
{id: 'Type', title: 'Type'}, | |
{id: 'Destination', title: 'Destination'}, | |
{id: 'Country', title: 'Country'}, | |
{id: 'WeightCat', title: 'WeightCat'}, | |
{id: 'Contents', title: 'Contents'}, | |
{id: 'Service', title: 'Service'}, | |
{id: 'FirstName', title: 'FirstName'}, | |
{id: 'LastName', title: 'LastName'}, | |
{id: 'CompanyName', title: 'CompanyName'}, | |
{id: 'Email', title: 'Email'}, | |
{id: 'Phone', title: 'Phone'}, | |
{id: 'AddressLine1', title: 'AddressLine1'}, | |
{id: 'AddressLine2', title: 'AddressLine2'}, | |
{id: 'CityState', title: 'CityState'}, | |
{id: 'Postcode', title: 'Postcode'}, | |
{id: 'ContentCategory', title: 'ContentCategory'}, | |
{id: 'ItemDescription0', title: 'ItemDescription0'}, | |
{id: 'Quantity0', title: 'Quantity0'}, | |
{id: 'Value0', title: 'Value0'}, | |
{id: 'Weight0', title: 'Weight0'}, | |
{id: 'ItemDescription1', title: 'ItemDescription1'}, | |
{id: 'Quantity1', title: 'Quantity1'}, | |
{id: 'Value1', title: 'Value1'}, | |
{id: 'Weight1', title: 'Weight1'} | |
] | |
}); | |
return csvWriter.writeRecords(csvFormat) | |
}) | |
.then(() => { | |
console.log('...Done'); | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}) | |
.then(function () { | |
// always executed | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment