2023-05-30 09:56:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Route;
|
2023-05-30 12:10:17 +00:00
|
|
|
use GuzzleHttp\Client;
|
2023-05-30 14:29:46 +00:00
|
|
|
use App\Http\Controllers\WebCrawlController;
|
2023-05-30 17:19:53 +00:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2023-05-30 09:56:38 +00:00
|
|
|
|
2023-05-30 17:19:53 +00:00
|
|
|
Route::get('/crawl', function (Request $request) {
|
|
|
|
// invode WebCrawlController index method in WebCrawlController
|
|
|
|
$url = $request->input('url');
|
|
|
|
// check if the url is valid URL
|
|
|
|
if (!$url || !filter_var($url, FILTER_VALIDATE_URL)) {
|
|
|
|
return response()->json([
|
|
|
|
'error' => 'Missing required parameter `url`'
|
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
$depth = $request->input('depth', 3); // default depth is 3 if not provided
|
|
|
|
|
|
|
|
$crawlerController = new WebCrawlController();
|
|
|
|
$isAlreadyDone = $crawlerController->getOne($url);
|
|
|
|
if(!$isAlreadyDone){
|
|
|
|
$crawlerController->crawlWebsite($url, $depth);
|
|
|
|
} else {
|
|
|
|
return response()->json([
|
|
|
|
'error' => 'This URL has already been crawled',
|
|
|
|
'data' => $isAlreadyDone
|
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
});
|
2023-05-30 12:10:17 +00:00
|
|
|
|
2023-05-30 17:19:53 +00:00
|
|
|
// Route::post('/crawl/{id}', function (String $id, Request $request, Response $response) {
|
|
|
|
// $id = $request->input('id');
|
|
|
|
// $crawlerController = new WebCrawlController();
|
|
|
|
// if(!$crawlerController->destroy($id)) {
|
|
|
|
// return response()->json([
|
|
|
|
// 'error' => 'Url Not Found',
|
|
|
|
// ], 404);
|
|
|
|
// } else {
|
|
|
|
// return response()->json([
|
|
|
|
// 'success' => 'This URL has been deleted',
|
|
|
|
// ], 200);
|
|
|
|
// }
|
2023-05-30 14:29:46 +00:00
|
|
|
// });
|
2023-05-30 12:10:17 +00:00
|
|
|
|