From a9e810b8fbd98a5c5647a81c8504dbd6e6382732 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Thu, 1 Jun 2023 08:50:34 +0300 Subject: [PATCH] improved - nameing convention and spaces --- README.md | 4 +- app/Http/Controllers/WebCrawlController.php | 48 ++++++++++----------- app/Services/WebCrawlerService.php | 15 +++---- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 22919be..63d1dd7 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,8 @@ # GET /api/crawl: Crawls a website and stores the crawled data in the database. Required query parameter: url. Optional query parameter: depth (default: 1). Parameters: - `url` (required): The URL of the website to crawl. - - `depth` (optional): The depth of the crawling process (default: 1). - - `refresh` (optional): If set to true, the crawler will refresh the results for an existing URL (default: false). + - `depth` (optional): The depth of the crawling process (default: 0). + - `refresh` (optional): If set to 1, the crawler will refresh the results for an existing URL (default: false). # GET /api: Retrieves all crawled data from the database. # DELETE /api/crawl/{id}: diff --git a/app/Http/Controllers/WebCrawlController.php b/app/Http/Controllers/WebCrawlController.php index 3a153a3..2785cae 100644 --- a/app/Http/Controllers/WebCrawlController.php +++ b/app/Http/Controllers/WebCrawlController.php @@ -7,34 +7,34 @@ class WebCrawlController extends Controller { - protected $webCrawlerService; + protected $webCrawlerService; - public function __construct(WebCrawlerService $webCrawlerService) - { - $this->webCrawlerService = $webCrawlerService; - } + public function __construct(WebCrawlerService $webCrawlerService) + { + $this->webCrawlerService = $webCrawlerService; + } - public function index() - { - return $this->webCrawlerService->getAllCrawls(); - } + public function index() + { + return $this->webCrawlerService->getAllCrawls(); + } - public function crawlWebsite(WebCrawlRequest $request) - { - $url = $request->query('url'); - $depth = $request->query('depth', 0); - $refresh = $request->query('refresh', false); + public function crawlWebsite(WebCrawlRequest $request) + { + $url = $request->query('url'); + $depth = $request->query('depth', 0); + $refresh = $request->query('refresh', 0); - return $this->webCrawlerService->crawlWebsite($url, $depth, $refresh); - } + return $this->webCrawlerService->crawlWebsite($url, $depth, $refresh); + } - public function destroy($id) - { - return $this->webCrawlerService->deleteCrawl($id); - } + public function destroy($id) + { + return $this->webCrawlerService->deleteCrawl($id); + } - public function destroyAll() - { - return $this->webCrawlerService->deleteAllCrawls(); - } + public function destroyAll() + { + return $this->webCrawlerService->deleteAllCrawls(); + } } diff --git a/app/Services/WebCrawlerService.php b/app/Services/WebCrawlerService.php index 211b7f3..2266796 100644 --- a/app/Services/WebCrawlerService.php +++ b/app/Services/WebCrawlerService.php @@ -23,10 +23,9 @@ public function getAllCrawls() public function crawlWebsite($url, $depth, $refresh) { - // Check if the URL is already in the database $webCrawl = WebCrawl::where('url', $url)->first(); if ($webCrawl && !$refresh) { - Log::error("This URL already exists in the database $url"); + Log::error("This URL already exists in the database: $url"); return response()->json([ 'error' => 'This URL already exists in the database', ], 400); @@ -57,13 +56,13 @@ public function crawlWebsite($url, $depth, $refresh) ]); } } else { - Log::error("Failed to retrieve the URL $url"); + Log::error("Failed to retrieve the URL: $url"); return response()->json([ 'error' => 'Failed to retrieve the URL', ], 500); } - Log::info("Crawling completed successfully For URL $url"); + Log::info("Crawling completed successfully for URL: $url"); return response()->json([ 'message' => 'Crawling completed successfully', ]); @@ -74,12 +73,12 @@ public function deleteCrawl($id) $webCrawl = WebCrawl::find($id); if ($webCrawl) { $webCrawl->delete(); - Log::info("Web crawl deleted successfully For ID $id"); + Log::info("Web crawl deleted successfully for ID: $id"); return response()->json([ 'message' => 'Web crawl deleted successfully', ]); } - Log::error("Web crawl not found For ID $id"); + Log::error("Web crawl not found for ID: $id"); return response()->json([ 'error' => 'Web crawl not found', ], 404); @@ -109,9 +108,9 @@ protected function crawlWebsiteRecursive($url, $depth) $linksFromPage = $this->getLinksFromPage($crawler->content); try { $crawler->save(); - Log::info("URL saved to the database $url"); + Log::info("URL saved to the database: $url"); } catch (\Exception $e) { - Log::error("Can't save the URL to the database $url"); + Log::error("Can't save the URL to the database: $url"); return []; } if ($depth > 0 && count($linksFromPage) > 0) {