-
Notifications
You must be signed in to change notification settings - Fork 4
/
edc_assets_mws.php
150 lines (140 loc) · 4.46 KB
/
edc_assets_mws.php
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
<?php
/**
* Download all product images for MWS
*/
require __DIR__ . "/core/init.php";
require __DIR__ . "/core/error.php";
require __DIR__ . "/core/db.php";
require __DIR__ . "/core/strings.php";
require __DIR__ . "/filter.php";
require "/var/www/masturbatorwebshop.be/prebuild/_cats.php";
use core\Strings;
// Utils
function xml($res) {
$xml = new SimpleXMLElement($res);
return json_decode(json_encode($xml), true);
}
define("IMGDIR", "/var/www/a.masturbatorwebshop.be/pub");
if (! file_exists(IMGDIR)) {
if (! mkdir(IMGDIR)) {
user_error(sprintf("mkdir(%s) failed", IMGDIR));
}
}
$db = new core\Db(sprintf("sqlite:%s/db.sqlite", CACHE), "", "");
$xml = new XMLReader();
if (! $xml->open('zip://' . CACHE . "/edc_prods.zip#eg_xml_feed_2015_nl.xml")) {
user_error("ERR: Failed opening edc_prods.zip");
}
while($xml->read() && $xml->name != 'product')
{
;
}
$pfx = "";
define("EDC_URL", "http://cdn.edc.nl/500/%s");
while($xml->name == 'product') {
$prod = xml($xml->readOuterXML());
if (count($prod["variants"]["variant"]) === 0) {
echo sprintf("no variants for prod=%s\n", $prod["title"]);
$xml->next('product');
unset($element);
continue;
}
if (! isset($prod["variants"]["variant"]["ean"]) && ! isset($prod["variants"]["variant"][0]["ean"])) {
var_dump($prod["variants"]);
echo sprintf("broken1 ean for prod=%s\n", $prod["title"]);
$xml->next('product');
unset($element);
continue; // horrible broken input data..
}
$ean = $prod["variants"]["variant"]["ean"] ?? $prod["variants"]["variant"][0]["ean"];
if (is_array($ean)) {
echo sprintf("broken ean for prod=%s\n", $prod["title"]);
$xml->next('product');
unset($element);
continue; // horrible broken input data..
}
if (VERBOSE) $pfx = $prod["title"] . " " . $ean;
if (filter_ignore($prod["title"])) {
if (VERBOSE) echo sprintf("$pfx Ignore %s\n", $prod["title"]);
$xml->next('product');
unset($element);
continue;
}
$ok = false;
foreach ($site_cats as $m) {
if (strpos(strtolower($prod["title"]), $m["keyword"]) !== false) {
$ok = true;
break;
}
}
if (! $ok) {
if (VERBOSE) echo sprintf("Not on site %s\n", $prod["title"]);
$xml->next('product');
unset($element);
continue;
}
$imgs = $prod["pics"]["pic"];
if (! is_array($imgs)) {
$imgs = [$imgs];
}
if (count($imgs) === 0) {
user_error("No image for asset?");
}
if (VERBOSE) echo sprintf("$pfx found imgs=%d\n", count($imgs));
// Map in DB
{
$variants = $prod["variants"]["variant"];
if (isset($variants["id"])) {
// hack. convert to array to generalize struct
$variants = [$variants];
}
$up = 0;
foreach ($variants as $variant) {
// TODO: perf?
if ("1" === $db->getCell("select 1 from prod_img where ean = ?", [$variant["ean"]])) continue;
$db->insert("prod_img", ["ean" => $variant["ean"], "count" => count($imgs)]);
$up++;
}
if (VERBOSE) echo "$pfx prod_img insert=$up\n";
}
$brandTitle = Strings::slugify($prod["brand"]["title"]);
if (! file_exists(IMGDIR . "/$brandTitle")) {
if (! mkdir(IMGDIR . "/$brandTitle")) {
user_error("mkdir brand fail");
}
}
foreach ($imgs as $idx => $pic) {
$f = IMGDIR . "/$brandTitle/$ean-" . Strings::slugify($prod["title"]) . "_" . $idx;
$webp = file_exists("$f.webp");
if (! file_exists("$f.jpg") || $webp === false) {
if (VERBOSE) echo sprintf("$pfx Download " . EDC_URL . "\n", $pic);
file_put_contents("$f.jpg", file_get_contents(sprintf(EDC_URL, $pic)));
$out = "";
$ret = 1;
// jpg to webp with highest compression
ob_start();
exec(sprintf("cwebp -quiet -m 6 -q 80 %s -o %s", escapeshellarg("$f.jpg"), escapeshellarg("$f.webp")), $out, $ret);
ob_get_clean();
if ($ret !== 0) {
$i = new Imagick("$f.jpg");
$i->setImageColorspace(Imagick::COLORSPACE_SRGB);
$i->writeImage("$f.jpg");
$i->destroy();
exec(sprintf("cwebp -quiet -m 6 -q 80 %s -o %s", escapeshellarg("$f.jpg"), escapeshellarg("$f.webp")), $out, $ret);
if ($ret !== 0) {
var_dump($out);
var_dump($ret);
user_error("exec(cwebp failed)");
}
}
//echo sprintf("WARN: JPG(CMYK/RGB) file=%s.jpg\n", $f);
} else {
if ($webp === false) {
var_dump($f);
user_error("jpg exist, webp not. buggy?");
}
if (VERBOSE) echo "$pfx Already exists: $f\n";
}
}
$xml->next('product');
}