From 11e5c66bfcf249a8a753568674558252cd9e87f9 Mon Sep 17 00:00:00 2001 From: Angus Goody Date: Tue, 15 Aug 2023 00:32:42 +0100 Subject: [PATCH] Tweaks (#47) * Added handler for 403 messages * Added missing fields alert * fixed tests --- .../Controllers/ProductScraperController.php | 12 +++++++- app/Services/Product.php | 1 + .../js/Components/wishlist/NewItemModal.vue | 24 ++++++++++----- tests/Feature/ProductScraperTest.php | 30 ++++++++++++------- 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/ProductScraperController.php b/app/Http/Controllers/ProductScraperController.php index d14474d..09b7a69 100644 --- a/app/Http/Controllers/ProductScraperController.php +++ b/app/Http/Controllers/ProductScraperController.php @@ -40,7 +40,7 @@ public function scrapeProduct(Request $request): JsonResponse // Try to connect 3 times before giving up $response = Http::timeout(3)->withHeaders([ - 'User-Agent' => "MyWishlistApp lookup", + 'User-Agent' => "Wish4it", ])->retry(2, 1000)->get($url); @@ -60,8 +60,18 @@ public function scrapeProduct(Request $request): JsonResponse } catch (RequestException $exception) { + $statusCode = $exception->response->status(); + if ($statusCode == 403) { + Log::channel("scraper")->warning('403 Forbidden error when scraping', [ + 'url' => $url, + 'error_message' => $exception->getMessage(), + ]); + return response()->json(['error' => 'The website has blocked us from retrieving product details :('], 403); + } + Log::channel("scraper")->error('A request error occurred during URL scraping', [ 'url' => $url, + 'status_code' => $statusCode, 'error_message' => $exception->getMessage(), ]); diff --git a/app/Services/Product.php b/app/Services/Product.php index 092f4f9..2893f1d 100644 --- a/app/Services/Product.php +++ b/app/Services/Product.php @@ -26,6 +26,7 @@ public function toArray(): array "brand" => $this->brand, "price" => $this->price, "image" => $this->image, + "hasMissedFields" => count($this->getMissingFields()) > 0 ]; } diff --git a/resources/js/Components/wishlist/NewItemModal.vue b/resources/js/Components/wishlist/NewItemModal.vue index 53d75cf..c4f95c9 100644 --- a/resources/js/Components/wishlist/NewItemModal.vue +++ b/resources/js/Components/wishlist/NewItemModal.vue @@ -67,6 +67,17 @@
+ + +
@@ -74,10 +85,6 @@ Your Image
- - - -
@@ -121,7 +128,6 @@ id="price" type="text" class="w-full mt-1" - placeholder="5" /> @@ -215,6 +221,7 @@ const urlForm = useForm({ let isOpen = ref(props.open) let showDetails = ref(false) +let hasMissedFields = ref(false) let showLoadUrlButton = ref(false) let loadUrlError = ref(false); @@ -277,10 +284,11 @@ function sendUrl(){ form.brand = product.brand form.price = product.price form.url = urlForm.url - showDetails.value = true; form.image = product.image + hasMissedFields.value = product.hasMissedFields + showDetails.value = true; + } - console.log(response) }) .catch(error => { @@ -340,6 +348,8 @@ function reset(){ form.errors = {}; urlForm.errors = {}; + hasMissedFields.value = false; + loadUrlError.value = false; form.reset(); urlForm.reset(); diff --git a/tests/Feature/ProductScraperTest.php b/tests/Feature/ProductScraperTest.php index 8e9d171..0e387bb 100644 --- a/tests/Feature/ProductScraperTest.php +++ b/tests/Feature/ProductScraperTest.php @@ -29,7 +29,8 @@ public function test_hoodie_scrape(): void "name" => "Balatapa Hood", "brand" => "Idioma", "price" => "89.00", - "image" => "//idioma.world/cdn/shop/products/Balatapa_Hood_Grey_Full_Hung_Web_grande.jpg?v=1679829385" + "image" => "//idioma.world/cdn/shop/products/Balatapa_Hood_Grey_Full_Hung_Web_grande.jpg?v=1679829385", + "hasMissedFields" => false ]; // Assert specific values @@ -56,7 +57,8 @@ public function test_john_lewis_scrape() "name" => "Bialetti Moka Express Hob Espresso Coffee Maker, 1 Cup", "brand" => "Bialetti", "price" => "24.00", - "image" => "http://johnlewis.scene7.com/is/image/JohnLewis/001407520" + "image" => "http://johnlewis.scene7.com/is/image/JohnLewis/001407520", + "hasMissedFields" => false ]; // Assert specific values @@ -84,7 +86,8 @@ public function test_go_outdoors_scrape() "name" => "Men’s Boreas GTX Mid Walking Boots", "brand" => "Scarpa", "price" => "190.00", - "image" => "https://i1.adis.ws/i/jpl/bl_16243260_a" + "image" => "https://i1.adis.ws/i/jpl/bl_16243260_a", + "hasMissedFields" => false ]; // Assert specific values @@ -113,7 +116,8 @@ public function test_apple_store_scrape() "name" => "iPhone 14 Pro 256GB Silver", "brand" => null, "price" => "1209.00", - "image" => "https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/iphone-14-pro-finish-select-202209-6-1inch-silver?wid=2560&hei=1440&fmt=jpeg&qlt=95&.v=1663703840488" + "image" => "https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/iphone-14-pro-finish-select-202209-6-1inch-silver?wid=2560&hei=1440&fmt=jpeg&qlt=95&.v=1663703840488", + "hasMissedFields" => true ]; // Assert specific values @@ -141,7 +145,8 @@ public function test_ebay_scrape() "name" => "UK Delivery | Herman Miller Sayl Chairs | Black Frame & Base | Blue Seat", "brand" => "Herman Miller", "price" => "289.00", - "image" => "https://i.ebayimg.com/images/g/YjAAAOSwR0Rklf8P/s-l1600.jpg" + "image" => "https://i.ebayimg.com/images/g/YjAAAOSwR0Rklf8P/s-l1600.jpg", + "hasMissedFields" => false ]; // Assert specific values @@ -170,7 +175,8 @@ public function test_argos_scrape() "name" => "Sony Full Frame A7mk3 Camera with SEL2870 Lens ", "brand" => null, "price" => "1699.99", - "image" => null + "image" => null, + "hasMissedFields" => true ]; // Assert specific values @@ -197,7 +203,8 @@ public function test_smaller_website_scrape() "name" => "Canon RF 1.4x Extender", "brand" => null, "price" => "579.00", - "image" => "https://harrison-cameras.s3.amazonaws.com/p/s/4113C005AA.jpg" + "image" => "https://harrison-cameras.s3.amazonaws.com/p/s/4113C005AA.jpg", + "hasMissedFields" => true ]; // Assert specific values @@ -221,7 +228,8 @@ public function test_bandq_scrape() "name" => "GoodHome Denia Brown Wooden 2 seater Square Table", "brand" => null, "price" => "34.00", - "image" => "https://media.diy.com/is/image/Kingfisher/goodhome-denia-brown-wooden-2-seater-square-table~3663602936015_01bq?\$MOB_PREV$&\$width=768&\$height=768" + "image" => "https://media.diy.com/is/image/Kingfisher/goodhome-denia-brown-wooden-2-seater-square-table~3663602936015_01bq?\$MOB_PREV$&\$width=768&\$height=768", + "hasMissedFields" => true ]; // Assert specific values @@ -248,7 +256,8 @@ public function test_amazon_scrape() "name" => "Duracell 2032 Twin Pack - silver", "brand" => null, "price" => "4.30", - "image" => "https://m.media-amazon.com/images/I/71iAp6vtJeL.__AC_SX300_SY300_QL70_ML2_.jpg" + "image" => "https://m.media-amazon.com/images/I/71iAp6vtJeL.__AC_SX300_SY300_QL70_ML2_.jpg", + "hasMissedFields" => true ]; // Assert specific values @@ -275,7 +284,8 @@ public function test_etsy_scrape() "name" => "Babies personalised Easter Bunny plush toy", "brand" => "LulabayUK", "price" => "12.00", - "image" => "https://i.etsystatic.com/28485446/c/3000/2384/0/153/il/92de6f/4089068291/il_340x270.4089068291_imz3.jpg" + "image" => "https://i.etsystatic.com/28485446/c/3000/2384/0/153/il/92de6f/4089068291/il_340x270.4089068291_imz3.jpg", + "hasMissedFields" => false ]; // Assert specific values