Web_Crawler_API/app/Http/Controllers/WebCrawlController.php

160 lines
5.1 KiB
PHP
Raw Normal View History

2023-05-30 14:29:46 +00:00
<?php
namespace App\Http\Controllers;
use App\Models\WebCrawl;
2023-05-30 17:19:53 +00:00
use GuzzleHttp\Client;
use Illuminate\Http\Request;
2023-05-31 10:27:00 +00:00
use Illuminate\Support\Facades\Log;
2023-05-30 14:29:46 +00:00
class WebCrawlController extends Controller
{
protected $webClient;
2023-05-30 14:29:46 +00:00
public function index()
2023-05-30 17:19:53 +00:00
{
$allCrawls = WebCrawl::all();
return response()->json($allCrawls);
}
public function crawlWebsite(Request $request)
{
$url = $request->query('url');
2023-05-31 11:52:51 +00:00
$depth = $request->query('depth', 0);
2023-05-31 10:17:19 +00:00
$refresh = $request->query('refresh', false);
2023-05-30 17:19:53 +00:00
// Check if the URL is already in the database
$webCrawl = WebCrawl::where('url', $url)->first();
2023-05-31 10:17:19 +00:00
if ($webCrawl && !$refresh) {
2023-05-31 10:27:00 +00:00
Log::error("This URL already exists in the database $url");
return response()->json([
'error' => 'This URL already exists in the database',
], 400);
2023-05-30 17:19:53 +00:00
}
$this->webClient = new Client();
2023-05-30 17:19:53 +00:00
// Use GuzzleHttp client to send HTTP requests
$response = $this->scan($url);
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
2023-05-31 10:17:19 +00:00
$crawler = $webCrawl ?? new WebCrawl();
$crawler->url = $url;
$crawler->content = $response->getBody()->getContents();
$linksFromPage = $this->getLinksFromPage($crawler->content);
try {
$crawler->save();
} catch (\Exception $e) {
2023-05-31 10:27:00 +00:00
Log::error($e->getMessage());
return response()->json([
'error' => 'Failed to save the URL to the database',
], 500);
}
if ($depth > 0 && count($linksFromPage) > 0) {
2023-05-31 10:17:19 +00:00
$results = [];
foreach ($linksFromPage as $link) {
2023-05-31 10:17:19 +00:00
$results[] = $this->crawlWebsiteRecursive($link, $depth - 1);
}
2023-05-31 10:17:19 +00:00
return response()->json([
'message' => 'Crawling completed successfully',
'data' => $url
]);
}
} else {
2023-05-31 10:27:00 +00:00
Log::error("Failed to retrieve the URL $url");
return response()->json([
'error' => 'Failed to retrieve the URL',
], 500);
}
2023-05-30 17:19:53 +00:00
2023-05-31 10:27:00 +00:00
Log::info("Crawling completed successfully For URL $url");
return response()->json([
'message' => 'Crawling completed successfully',
]);
2023-05-30 17:19:53 +00:00
}
protected function crawlWebsiteRecursive($url, $depth)
2023-05-30 17:19:53 +00:00
{
// Check if the URL is already in the database
2023-05-30 17:19:53 +00:00
$webCrawl = WebCrawl::where('url', $url)->first();
if ($webCrawl) {
2023-05-31 10:17:19 +00:00
return [];
2023-05-30 17:19:53 +00:00
}
2023-05-30 14:29:46 +00:00
// Use GuzzleHttp client to send HTTP requests
$response = $this->scan($url);
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
$crawler = new WebCrawl();
$crawler->url = $url;
$crawler->content = $response->getBody()->getContents();
$linksFromPage = $this->getLinksFromPage($crawler->content);
try {
$crawler->save();
2023-05-31 10:27:00 +00:00
Log::info("URL saved to the database $url");
} catch (\Exception $e) {
2023-05-31 10:27:00 +00:00
Log::error("Can't save the URL to the database $url");
2023-05-31 10:17:19 +00:00
return [];
}
if ($depth > 0 && count($linksFromPage) > 0) {
2023-05-31 10:17:19 +00:00
$results = [];
foreach ($linksFromPage as $link) {
2023-05-31 10:17:19 +00:00
$results = array_merge($results, $this->crawlWebsiteRecursive($link, $depth - 1));
}
2023-05-31 10:17:19 +00:00
return $results;
}
}
2023-05-31 10:17:19 +00:00
return [];
2023-05-30 14:29:46 +00:00
}
protected function getLinksFromPage($crawlerContent)
2023-05-30 14:29:46 +00:00
{
$dom = new \DOMDocument();
@$dom->loadHTML($crawlerContent);
$links = $dom->getElementsByTagName('a');
$linksFromPage = [];
foreach ($links as $link) {
if ($link->getAttribute('href') && filter_var($link->getAttribute('href'), FILTER_VALIDATE_URL)) {
$linksFromPage[] = $link->getAttribute('href');
}
}
return $linksFromPage;
2023-05-30 14:29:46 +00:00
}
protected function scan($url)
2023-05-30 14:29:46 +00:00
{
2023-05-31 11:52:51 +00:00
try {
$result = $this->webClient->request('GET', $url);
} catch (\Exception $e) {
return response()->json([
'error' => 'Failed to retrieve the URL',
], 500);
}
return $result;
2023-05-30 14:29:46 +00:00
}
2023-05-30 17:19:53 +00:00
public function destroy($id)
2023-05-30 14:29:46 +00:00
{
$webCrawl = WebCrawl::find($id);
2023-05-30 17:19:53 +00:00
if ($webCrawl) {
$webCrawl->delete();
2023-05-31 10:27:00 +00:00
Log::info("Web crawl deleted successfully For ID $id");
return response()->json([
'message' => 'Web crawl deleted successfully',
]);
2023-05-30 17:19:53 +00:00
}
2023-05-31 10:27:00 +00:00
Log::error("Web crawl not found For ID $id");
return response()->json([
'error' => 'Web crawl not found',
], 404);
}
public function destroyAll()
{
WebCrawl::truncate();
2023-05-31 10:27:00 +00:00
Log::info("All web crawls deleted successfully");
return response()->json([
'message' => 'All web crawls deleted successfully',
]);
2023-05-30 14:29:46 +00:00
}
}