-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
200 lines (180 loc) · 5.22 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { appendFile, access, unlink, constants } from "fs";
import api from "./lib/lightspeed.js";
import slugify from "slugify";
import xml from "xml";
const FILE_PATH = "./products.xml";
const BASE_URL = "https://www.shootingsuppliesltd.co.uk";
class Item {
constructor(
id = 0,
title = "",
description = "",
link = "",
image_link = "",
price = 0,
availability = "",
condition = "",
brand = "",
gtin = "",
mpn = "",
quantity = 0
) {
this.id = id;
this.title = title;
this.description = description;
this.link = link;
this.image_link = image_link;
this.price = price;
this.availability = availability;
this.condition = condition;
this.brand = brand;
this.gtin = gtin;
this.mpn = mpn;
this.quantity = quantity;
}
toXMLObject() {
return {
item: [
{ "g:id": this.id },
{ "g:title": this.title },
{ "g:description": this.description },
{ "g:link": this.link },
{ "g:image_link": this.image_link },
{ "g:condition": this.condition },
{ "g:availability": this.availability },
{ "g:price": this.price + " GBP" },
{
"g:shipping": [
{ "g:country": "GB" },
{ "g:service": "Parcel Force" },
{ "g:price": "5.95 GBP" },
{ "g:min_handling_time": "1" },
{ "g:max_handling_time": "3" },
{ "g:min_transit_time": "1" },
{ "g:max_transit_time": "2" },
],
},
{ "g:gtin": this.gtin },
{ "g:brand": this.brand },
{ "g:mpn": this.mpn },
{ "g:store_code": "5e79d2c0-c252-581a-be7a-060029190706" },
{ "g:quantity": this.quantity },
],
};
}
static async deleteFile() {
// Check if the file exists
access(FILE_PATH, constants.F_OK, (err) => {
if (err) {
console.error("File does not exist");
return;
}
// Delete the file
unlink(FILE_PATH, (err) => {
if (err) {
console.error("Error deleting the file", err);
return;
}
console.log("File deleted successfully");
});
});
}
static async saveAsXML(items) {
const xmlObj = {
rss: [
{
_attr: {
"xmlns:g": "http://base.google.com/ns/1.0",
version: "2.0",
},
},
{
channel: [
{ title: "Shooting Supplies Ltd" },
{ link: BASE_URL },
{
description: "This is the product feed for Shooting Supplies Ltd",
},
...items.map((item) => item.toXMLObject()),
],
},
],
};
const xmlString = xml(xmlObj, { declaration: true, indent: " " });
try {
await appendFile(FILE_PATH, xmlString, (err) => {
if (err) {
console.error("Error appending to products.xml:", err);
} else {
console.log("Items appended to products.xml");
}
});
} catch (err) {
console.error("Error: ", err);
}
}
}
const startApp = async () => {
try {
await Item.deleteFile();
console.log("Fetching Items...");
const items = await api.getItems(
`["Category", "Manufacturer", "Images", "ItemShops", "ItemECommerce", "ItemPrices", "ItemAttributes", "ItemAttributes.ItemAttributeSet", "CustomFieldValues"]`
);
console.log("Writing to products.xml...");
const formattedItems = items.map((item) => {
let imgUrl = "";
let newItem;
if (!item.Images?.Image) {
imgUrl = "";
} else if (Array.isArray(item.Images.Image)) {
imgUrl = item.Images.Image[0].FullPath
? item.Images.Image[0].FullPath
: `${item.Images.Image[0].baseImageURL}/w_250/${item.Images.Image[0].publicID}.webp`;
} else {
imgUrl = `${item.Images.Image.baseImageURL}/w_250/${item.Images.Image.publicID}.webp`;
}
const getAvailability = (item) => {
const quantity = parseInt(item.ItemShops.ItemShop[0].qoh);
if (quantity <= 0) {
return "out of stock";
} else if (quantity > 0 && quantity <= 3) {
return "limited availability";
} else {
return "in stock";
}
};
let description = `"${item.ItemECommerce?.shortDescription
.replace(/<[^>]*>?/gm, "")
.replaceAll('"', "inch")}"`;
newItem = new Item(
item.itemID,
item.description
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" "),
description,
`${BASE_URL}/${slugify(item.description, {
lower: true,
replacement: "-",
strict: true,
})}/${item.itemID}`,
imgUrl,
parseFloat(item.Prices.ItemPrice[0].amount).toFixed(2),
parseInt(item.ItemShops.ItemShop[0].qoh) <= 0 ? "Out of stock" : "In stock",
"new",
item.Manufacturer?.name,
item.upc,
item.manufacturerSku,
item.ItemShops.ItemShop[0].qoh
);
return newItem;
});
await Item.saveAsXML(formattedItems);
console.log("Done! 🎉");
} catch (error) {
console.error("There was an error: ", error);
}
};
startApp();