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
201
202
203
204
205
206
207
208
209
210
211
|
#include <string.h>
#include <stdbool.h>
#include <locale.h>
#include <langinfo.h>
#include <tidy.h>
#include <tidybuffio.h>
#include "request.h"
#include "icclient.h"
char *sampleurl;
static char *image_dir;
static locale_t loc = 0;
void icclient_init(const char *url, const char *dir, const char *certificate)
{
size_t length = strlen(url);
size_t append = url[length - 1] != '/';
sampleurl = malloc(length + append + 1);
strcpy(sampleurl, url);
if (append)
strcat(sampleurl, "/");
image_dir = malloc(strlen(dir) + 1);
strcpy(image_dir, dir);
init(certificate);
}
static void dumpNode(TidyDoc doc, TidyNode tnod, struct icclient_catalog **catalog, char **category,
bool is_sku, bool is_category, bool is_price)
{
struct icclient_product *product;
for (TidyNode child = tidyGetChild(tnod); child; child = tidyGetNext(child)) {
ctmbstr name = tidyNodeGetName(child);
if (name) {
if (!strcmp(name, "html")) {
char locale[11];
for (TidyAttr attr = tidyAttrFirst(child); attr; attr = tidyAttrNext(attr)) {
if (!strcmp(tidyAttrValue(attr), "en"))
strcpy(locale, "en_US.utf8");
else if (!strcmp(tidyAttrValue(attr), "id"))
strcpy(locale, "id_ID.utf8");
if (!loc)
loc = newlocale(LC_MONETARY_MASK | LC_NUMERIC_MASK, locale, 0);
}
} else if (!strcmp(name, "h3")) {
is_sku = false;
is_category = true;
is_price = false;
} else if (!strcmp(name, "img")) {
static const char *subdir = "/items/";
char prefix[strlen(image_dir) + strlen(subdir) + 1];
sprintf(prefix, "%s%s", image_dir, subdir);
size_t prefix_len = strlen(prefix);
for (TidyAttr attr = tidyAttrFirst(child); attr; attr = tidyAttrNext(attr)) {
if (!strcmp(tidyAttrName(attr), "src")) {
if (strncmp(tidyAttrValue(attr), prefix, prefix_len))
break;
product = malloc(sizeof(struct icclient_product));
memset(product, '\0', sizeof(struct icclient_product));
size_t len = strlen(tidyAttrValue(attr)) - prefix_len;
product->image = malloc(len + 1);
strncpy(product->image, tidyAttrValue(attr) + prefix_len, len + 1);
}
if (strcmp(tidyAttrName(attr), "title"))
continue;
product->description = malloc(strlen(tidyAttrValue(attr)) + 1);
strcpy(product->description, tidyAttrValue(attr));
if (*category) {
product->category = malloc(strlen(*category) + 1);
strcpy(product->category, *category);
}
(*catalog)->length++;
*catalog = realloc(*catalog, sizeof(struct icclient_catalog)
+ sizeof(struct icclient_product *[(*catalog)->length]));
(*catalog)->products[(*catalog)->length - 1] = product;
}
} else if (!strcmp(name, "h4")) {
is_sku = true;
is_category = false;
is_price = false;
} else if (is_sku && !strcmp(name, "a")) {
is_sku = false;
for (TidyAttr attr = tidyAttrFirst(child); attr; attr = tidyAttrNext(attr)) {
char *sku = strrchr(tidyAttrValue(attr), '/');
char *session_id = strchr(sku++, '?');
if (session_id)
*session_id = '\0';
product = (*catalog)->products[(*catalog)->length - 1];
product->sku = malloc(strlen(sku) + 1);
strcpy(product->sku, sku);
}
} else if (!strcmp(name, "p") && !((*catalog)->products[(*catalog)->length - 1])->price) {
is_sku = false;
is_category = false;
is_price = true;
} else {
is_sku = false;
is_category = false;
is_price = false;
}
} else if (is_category || is_price) {
TidyBuffer buf;
tidyBufInit(&buf);
tidyNodeGetText(doc, child, &buf);
char *text = (char *)buf.bp;
product = (*catalog)->products[(*catalog)->length - 1];
if (is_category) {
is_category = false;
text[strlen(text) - 1] = '\0';
if (*category)
free(*category);
*category = malloc(strlen(text) + 1);
strcpy(*category, text);
} else {
is_price = false;
#ifndef __ANDROID__
char *symbol = nl_langinfo_l(CRNCYSTR, loc);
size_t symbol_len = strlen(symbol) - 1;
if (symbol[0] == '-')
text += symbol_len;
char *separator = nl_langinfo_l(THOUSEP, loc);
if (strstr(text, separator)) {
size_t price_len = strlen(text) - symbol_len - 1;
char price[price_len];
memset(price, '\0', price_len);
strcat(price, strtok(text, separator));
strcat(price, strtok(NULL, separator));
product->price = atof(price);
} else
product->price = atof(text);
#endif
}
tidyBufFree(&buf);
}
dumpNode(doc, child, catalog, category, is_sku, is_category, is_price);
}
}
void handle_results(icclient_fetch_t *fetch)
{
TidyDoc tdoc = tidyCreate();
tidyOptSetBool(tdoc, TidyXhtmlOut, yes);
tidyParseString(tdoc, fetch->data);
tidyCleanAndRepair(tdoc);
tidyOptSetBool(tdoc, TidyForceOutput, yes);
TidyBuffer output = {0};
tidyBufInit(&output);
tidySaveBuffer(tdoc, &output);
struct icclient_catalog *catalog = malloc(sizeof(struct icclient_catalog));
catalog->length = 0;
dumpNode(tdoc, tidyGetRoot(tdoc), &catalog, &(char *){NULL}, false, false, false);
tidyBufFree(&output);
tidyRelease(tdoc);
((void (*)(struct icclient_catalog *))fetch->userData)(catalog);
}
void icclient_results(const char *prod_group, void (*callback)(struct icclient_catalog *), void (*handler)(icclient_fetch_t *))
{
char nonspaced[strlen(prod_group) + 1];
strcpy(nonspaced, prod_group);
char *space = NULL;
while ((space = strchr(nonspaced, ' ')))
*space = '-';
request(handler ? handler : handle_results, (void *)callback, 0, "%s", nonspaced);
}
void icclient_flypage(const char *sku, void (*handler)(icclient_fetch_t *), struct icclient_product **productptr)
{
request(handler, (void *)productptr, 0, "%s", sku);
}
void icclient_page(const char *path, void (*handler)(icclient_fetch_t *), void **dataptr)
{
request(handler, (void *)dataptr, 0, "%s", path);
}
void icclient_free_product(struct icclient_product *product)
{
if (product->crosssell)
for (size_t i = 0; i < product->crosssell->length; i++)
free(product->crosssell->skus[i]);
if (product->author)
free(product->author);
if (product->prod_group)
free(product->prod_group);
if (product->image)
free(product->image);
if (product->thumb)
free(product->thumb);
if (product->comment)
free(product->comment);
if (product->description)
free(product->description);
free(product->sku);
free(product);
}
void icclient_free_catalog(struct icclient_catalog *catalog)
{
for (size_t i = 0; i < catalog->length; i++)
icclient_free_product(catalog->products[i]);
free(catalog);
}
void icclient_cleanup()
{
if (loc)
freelocale(loc);
#ifndef __EMSCRIPTEN__
cleanup();
#endif
}
|