diff --git a/app/Helpers/Global/proxy_helper.php b/app/Helpers/Global/proxy_helper.php index 23eeaff..3471e36 100644 --- a/app/Helpers/Global/proxy_helper.php +++ b/app/Helpers/Global/proxy_helper.php @@ -52,15 +52,15 @@ function get_smartproxy_server() } if (! function_exists('calculate_smartproxy_cost')) { - function calculate_smartproxy_cost(float $kb, $amplifier = 1) + function calculate_smartproxy_cost(float $kb, $type = 'rotating_global') { - $cost_per_gb = config('platform.proxy.smartproxy.rotating_global.cost_per_gb'); + $cost_per_gb = config("platform.proxy.smartproxy.{$type}.cost_per_gb"); // Convert cost per GB to cost per KB $cost_per_kb = $cost_per_gb / (1024 * 1024); // Calculate total cost for the given $kb - $cost = ($cost_per_kb * $kb) * $amplifier; + $cost = ($cost_per_kb * $kb); return round($cost, 3); } diff --git a/app/Jobs/Tasks/SaveShopeeSellerImagesTask.php b/app/Jobs/Tasks/SaveShopeeSellerImagesTask.php index 9b997de..2fc3c37 100644 --- a/app/Jobs/Tasks/SaveShopeeSellerImagesTask.php +++ b/app/Jobs/Tasks/SaveShopeeSellerImagesTask.php @@ -219,7 +219,7 @@ private static function filterImages(array $images, string $proxy, string $user_ ]; $image['color_counts'] = self::isMostlyTextBasedOnUniqueColors($interventionImage); //$image['img'] = $interventionImage; - $costs['count-'.$count] = calculate_smartproxy_cost($sizeKb); + $costs['count-'.$count] = calculate_smartproxy_cost($sizeKb, 'rotating_global'); $filteredImages[] = $image; @@ -240,17 +240,22 @@ private static function filterImages(array $images, string $proxy, string $user_ $colorCounts[] = $image['color_counts']; } - // Compute the median of the color counts - sort($colorCounts); - $count = count($colorCounts); - $middleIndex = floor($count / 2); - $median = $count % 2 === 0 ? ($colorCounts[$middleIndex - 1] + $colorCounts[$middleIndex]) / 2 : $colorCounts[$middleIndex]; + if (! empty($colorCounts)) { + // Compute the median of the color counts + sort($colorCounts); + $count = count($colorCounts); + $middleIndex = floor($count / 2); + $median = $count % 2 === 0 ? ($colorCounts[$middleIndex - 1] + $colorCounts[$middleIndex]) / 2 : $colorCounts[$middleIndex]; - // Use the median to filter out the low outliers - $threshold = 0.10 * $median; // Adjust this percentage as needed - $filteredImages = array_filter($filteredImages, function ($image) use ($threshold) { - return $image['color_counts'] > $threshold; - }); + // Use the median to filter out the low outliers + $threshold = 0.10 * $median; // Adjust this percentage as needed + $filteredImages = array_filter($filteredImages, function ($image) use ($threshold) { + return $image['color_counts'] > $threshold; + }); + } else { + // No images found + $filteredImages = []; // Clear the array or take any other appropriate action + } usort($filteredImages, function ($a, $b) { return $b['sizeKb'] <=> $a['sizeKb']; // Using the spaceship operator to sort in descending order @@ -296,7 +301,7 @@ private static function getProductImage(array $jsonLdData, string $proxy, string $sizeInKb = strlen($interventionImage->encode()) / 1024; // Convert bytes to kilobytes // Calculate the cost - $cost = calculate_smartproxy_cost($sizeInKb); + $cost = calculate_smartproxy_cost($sizeInKb, 'rotating_global'); $costs['product_image'] = $cost; diff --git a/app/Jobs/Tasks/UrlCrawlerTask.php b/app/Jobs/Tasks/UrlCrawlerTask.php index d9c6972..41a3853 100644 --- a/app/Jobs/Tasks/UrlCrawlerTask.php +++ b/app/Jobs/Tasks/UrlCrawlerTask.php @@ -64,6 +64,7 @@ public static function handle(string $url, $directory, $postfix = null, $strip_h ->get($cached_url); if ($response->successful()) { + //$costs['html_proxy'] = calculate_smartproxy_cost() $raw_html = $response->body(); // ... your logic here ... } else { @@ -274,7 +275,7 @@ private static function filterImages(array $images, string $proxy, string $user_ ]; $image['color_counts'] = self::isMostlyTextBasedOnUniqueColors($interventionImage); //$image['img'] = $interventionImage; - $costs['count-'.$count] = calculate_smartproxy_cost($sizeKb); + $costs['count-'.$count] = calculate_smartproxy_cost($sizeKb, 'rotating_global'); $filteredImages[] = $image; @@ -357,7 +358,7 @@ private static function getProductImage(array $jsonLdData, string $proxy, string $sizeInKb = strlen($interventionImage->encode()) / 1024; // Convert bytes to kilobytes // Calculate the cost - $cost = calculate_smartproxy_cost($sizeInKb); + $cost = calculate_smartproxy_cost($sizeInKb, 'rotating_global'); $costs['product_image'] = $cost;