-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
124 lines (104 loc) · 4.15 KB
/
main.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
<?php
namespace MyProject;
require_once __DIR__ . '/vendor/autoload.php';
function validateAWSCredentials(): void
{
$credentials = [
'AWS_ACCESS_KEY' => AWS_ACCESS_KEY,
'AWS_SECRET_KEY' => AWS_SECRET_KEY,
'AWS_ASSOCIATE_TAG' => AWS_ASSOCIATE_TAG
];
$placeholders = [
'YOUR_AWS_ACCESS_KEY',
'YOUR_AWS_SECRET_KEY',
'YOUR_ASSOCIATE_TAG'
];
try {
foreach ($credentials as $key => $value) {
if (in_array($value, $placeholders)) {
$message = "$key is not properly configured.";
Debug::log($message, "CRITICAL");
throw new \Exception($message);
}
}
Debug::log("AWS credentials configuration passed.");
} catch (\Exception $e) {
Debug::log("AWS credentials configuration failed: " . $e->getMessage(), "CRITICAL");
Debug::sendErrorEmail("Amazon Rank Updater - Configuration Error", "AWS credentials configuration failed: " . $e->getMessage());
exit(1);
}
}
function checkAndCreateTables(): void
{
$db = new Database();
try {
$db->checkSourceTable();
Debug::log("Source table check passed.");
} catch (\Exception $e) {
Debug::log("Source table check failed: " . $e->getMessage(), "CRITICAL");
exit(1);
}
if (!$db->checkTargetTable()) {
Debug::log("Target table does not exist. Attempting to create...");
try {
$db->createTargetTable();
Debug::log("Target table created successfully.");
} catch (\Exception $e) {
Debug::log("Failed to create target table: " . $e->getMessage(), "CRITICAL");
Debug::log("Please run the following SQL to create the table manually:");
Debug::log($db->getCreateTableStatement());
exit(1);
}
} else {
Debug::log("Target table exists.");
}
}
function main(): void
{
checkAndCreateTables();
validateAWSCredentials();
$amazonAPI = new AmazonAPI();
$amazonAPI->checkAWSCredentials();
$db = new Database();
if ($_SERVER['PHP_LIVE_MODE'] == 1) {
Debug::log("Running in LIVE mode. API calls will be made to Amazon.", "WARNING");
} else {
Debug::log("Running in TEST mode. No actual API calls will be made.", "INFO");
}
while (true) {
try {
$asins = $db->getASINsToUpdate();
$strict_request_interval = (24 * 60 * 60) / count($asins); // share requests over a complete day
$cumulated_request_interval = 0;
foreach ($asins as $asin) {
$flex_request_interval = intval($strict_request_interval - rand(0, $strict_request_interval * 0.2));
$cumulated_request_interval .= $flex_request_interval + 3; // 3 seconds per request
Debug::log("Processing ASIN: $asin");
$info = [];
if ($_SERVER['PHP_LIVE_MODE'] == 1) {
$info = $amazonAPI->getRankAndTitle($asin);
} elseif ($_SERVER['PHP_DEBUG_MODE'] == 1) {
// fake mockdata
$info['rank'] = rand(1000, 1000000);
}
if ($info !== false) {
$db->updateRank($asin, $info['title'], $info['rank']);
Debug::log("Updated ASIN: $asin, Rank: " . $info['rank']);
} else {
$db->updateRank($asin, 'N/A', null);
Debug::log("Updated ASIN: $asin, Rank: NULL");
}
Debug::log("Sleep " . $flex_request_interval . " seconds");
sleep($flex_request_interval);
}
$seconds_big_sleep = (24 * 60 * 60) - $cumulated_request_interval;
Debug::log("Completed update cycle. Waiting " . $seconds_big_sleep . " seconds for next day...");
sleep($seconds_big_sleep);
} catch (\Exception $e) {
$errorMessage = "Critical error in main loop: " . $e->getMessage();
Debug::log($errorMessage, "CRITICAL");
Debug::sendErrorEmail("Amazon Rank Updater - Critical Error", $errorMessage);
exit(1);
}
}
}