-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_shop.php
193 lines (155 loc) · 8.35 KB
/
check_shop.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
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
<?php
require_once "vendor/autoload.php";
use Twilio\Rest\Client;
require_once "config.php";
echo "[".date("Y-m-d H:i:s")."]\n";
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"]);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
error_log("Erreur de connexion à la base de données : " . $e->getMessage() . "\n", 3, __DIR__ . "/check_shop.log");
die("Erreur de connexion à la base de données. Veuillez consulter le fichier de log pour plus de détails.");
}
try {
// Récupérer la liste des boutiques
$shops = $pdo->query("SELECT shop_id, shop_url, last_checked FROM shops")->fetchAll(PDO::FETCH_ASSOC);
foreach ($shops as $shop) {
echo "Vérification de " . $shop['shop_url'] . "\n";
$page = 1;
$productsPerPage = 250;
do {
// Récupération des produits de la boutique
$jsonUrl = $shop['shop_url'] . "/products.json?limit=$productsPerPage&page=$page";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $jsonUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Chrome/59.0.3071.115');
$productsJson = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Vérifier que le fichier a bien été récupéré
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
throw new Exception("Erreur lors de la récupération du fichier products.json.");
}
if ($productsJson === false) {
throw new Exception("Erreur lors de la récupération du fichier products.json.");
}
// Décoder le JSON
$productsData = json_decode($productsJson, true);
if ($productsData === null) {
throw new Exception("Erreur lors du décodage du JSON.");
}
// Début de la transaction
$pdo->beginTransaction();
foreach ($productsData['products'] as $product) {
processProductChanges($pdo, $product, $shop);
}
// Valider la transaction
$pdo->commit();
$page++;
} while (count($productsData['products']) >= $productsPerPage);
// Mettre à jour le `last_checked`
$stmt = $pdo->prepare("UPDATE shops SET last_checked = NOW() WHERE shop_id = ?");
$stmt->execute([$shop['shop_id']]);
}
} catch (PDOException $e) {
$pdo->rollBack();
error_log("Erreur de base de données : " . $e->getMessage(). "\n", 3, __DIR__ . "/check_shop.log");
} catch (Exception $e) {
error_log("Erreur : " . $e->getMessage(). "\n", 3, __DIR__ . "/check_shop.log");
}
function processProductChanges($pdo, $product, $shop) {
$stmt = $pdo->prepare("SELECT * FROM products WHERE shopify_product_id = ?");
$stmt->execute([$product['id']]);
$existingProduct = $stmt->fetch(PDO::FETCH_ASSOC);
if ($existingProduct) {
// Vérifier les changements pour chaque variante
foreach ($product['variants'] as $variant) {
$variantStmt = $pdo->prepare("SELECT * FROM variants WHERE shopify_variant_id = ?");
$variantStmt->execute([$variant['id']]);
$existingVariant = $variantStmt->fetch(PDO::FETCH_ASSOC);
if ($existingVariant) {
// Vérifier les changements de disponibilité et de prix
if ($existingVariant['available'] != $variant['available'] || $existingVariant['price'] != $variant['price']) {
$message = "Changement détecté pour " . $product['title'] . " (Variante: " . $variant['title'] . ")";
if ($existingVariant['price'] != $variant['price']) {
$message .= " - Prix modifié de " . $existingVariant['price'] . "€ à " . $variant['price'] . "€";
$changeDetails = "Prix modifié de " . $existingVariant['price'] . "€ à " . $variant['price'] . "€";
$changeType = 'PriceChange';
recordProductChange($pdo, $product['id'], $changeType, $changeDetails);
}
if ($existingVariant['available'] != $variant['available']) {
$message .= " - Disponibilité modifiée: " . ($variant['available'] ? "Disponible" : "Non disponible");
$changeDetails = "Disponibilité modifiée: " . ($variant['available'] ? "Disponible" : "Non disponible");
$changeType = 'AvailabilityChange';
recordProductChange($pdo, $product['id'], $changeType, $changeDetails);
}
// Envoi de la notification
sendWebhookNotification($message, $product['images'][0]['src'] ?? '', $shop['shop_url'] . '/products/' . $product['handle']);
// Conversion de la disponibilité en entier (1 pour vrai, 0 pour faux)
$availableInt = $variant['available'] ? 1 : 0;
// Mise à jour de la variante dans la base de données
$updateVariantStmt = $pdo->prepare("UPDATE variants SET price = ?, available = ? WHERE shopify_variant_id = ?");
$updateVariantStmt->execute([$variant['price'], $availableInt, $variant['id']]);
}
} else {
// Nouvelle variante détectée
$message = "Nouvelle variante ajoutée pour " . $product['title'] . ": " . $variant['title'];
sendWebhookNotification($message, $product['images'][0]['src'] ?? '', $shop['shop_url'] . '/products/' . $product['handle']);
$availableInt = $variant['available'] ? 1 : 0;
// Insertion de la nouvelle variante dans la base de données
$insertVariantStmt = $pdo->prepare("INSERT INTO variants (shopify_variant_id, product_id, title, price, available) VALUES (?, ?, ?, ?, ?)");
$insertVariantStmt->execute([$variant['id'], $product['id'], $variant['title'], $variant['price'], $availableInt]);
$changeType = 'NewVariant';
recordProductChange($pdo, $product['id'], $changeType, $message);
}
}
} else {
// Nouveau produit détecté
$message = "Nouveau produit ajouté: " . $product['title'];
sendWebhookNotification($message, $product['images'][0]['src'] ?? '', $shop['shop_url'] . '/products/' . $product['handle']);
// Insertion du nouveau produit dans la base de données
$insertProductStmt = $pdo->prepare("INSERT INTO products (shopify_product_id, shop_id, title, handle, created_at, updated_at, image_url) VALUES (?, ?, ?, ?, ?, ?, ?)");
$insertProductStmt->execute([
$product['id'],
$shop['shop_id'],
$product['title'],
$product['handle'],
$product['created_at'],
$product['updated_at'],
$product['images'][0]['src'] ?? ''
]);
$changeType = 'NewProduct';
recordProductChange($pdo, $product['id'], $changeType, $message);
}
}
function recordProductChange($pdo, $productId, $changeType, $changeDetails) {
$stmt = $pdo->prepare("INSERT INTO product_changes (product_id, change_type, change_details, change_date) VALUES (?, ?, ?, NOW())");
$stmt->execute([$productId, $changeType, $changeDetails]);
}
function sendWebhookNotification($message, $imageUrl, $productUrl) {
global $sid, $to, $from, $token;
// Création du client Twilio
$client = new Twilio\Rest\Client($sid, $token);
//Add url at the beginning of the message
$message = $productUrl . "\n" . $message;
// Envoi du message WhatsApp
try {
$client->messages->create(
$to,
array(
'from' => $from,
'body' => $message,
'mediaUrl' => [$imageUrl]
)
);
echo "Message envoyé: $message\n";
//Twilio rate limit
sleep(2);
} catch (Exception $e) {
echo "Erreur lors de l'envoi du message: " . $e->getMessage();
error_log("Erreur lors de l'envoi du message: " . $e->getMessage(). "\n", 3, __DIR__ . "/check_shop.log");
}
}
?>