Change url naming convention. Update README.md

This commit is contained in:
Krzysztof Płaczek
2024-10-14 14:04:15 +02:00
parent 2b595c1403
commit d043e8efb1
7 changed files with 73 additions and 69 deletions

View File

@@ -5,10 +5,17 @@
1. Clone repository using `git clone https://git.techtube.pl/krzysiej/ryobi-crawler.git` 1. Clone repository using `git clone https://git.techtube.pl/krzysiej/ryobi-crawler.git`
2. Cd into project directory `cd ryobi-crawler` 2. Cd into project directory `cd ryobi-crawler`
3. Build and start docker container `docker compose up -d` 3. Build and start docker container `docker compose up -d`
4. Run `docker compose exec php-app php index.php app:migrate` file to create `database.sqlite` and create tables. 4. Run `docker compose exec php-app php console.php app:migrate` file to create `database.sqlite` and create tables.
5. Run `docker compose exec php-app php index.php app:scrape` command to scrape all the products from the ryobi website. 5. Run `docker compose exec php-app php console.php app:scrape` command to scrape all the products from the ryobi website.
6. Access web interface using `localhost:9000` address in web browser. 6. Access web interface using `localhost:9000` address in web browser.
## Update project
1. Cd into project directory
2. Run `git pull`
3. Start and build image in one go with command: `docker compose up -d --build --force-recreate`
## Screenshots ## Screenshots
### Main screen of the web view ### Main screen of the web view

View File

@@ -1,48 +0,0 @@
<?php
include_once 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
use Krzysiej\RyobiCrawler\Controller\CategoryController;
use Krzysiej\RyobiCrawler\Controller\IndexController;
use Krzysiej\RyobiCrawler\Controller\ProductController;
use Krzysiej\RyobiCrawler\Controller\SearchController;
use Krzysiej\RyobiCrawler\Controller\StarController;
use Krzysiej\RyobiCrawler\Models\Product;
use Twig\{Environment, Loader\FilesystemLoader};
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
if (!file_exists('database.sqlite')) {
exit('Database file <code>database.sqlite</code> missing. Run docker compose <blockquote>docker compose exec php-app php index.php app:migrate</blockquote> to create it.');
}
$productRoute = new Route('/browser.php/product/{product_id}', ['_controller' => ProductController::class], ['product_id' => '\d+']);
$searchRoute = new Route('/browser.php?search={search_term}', ['_controller' => SearchController::class]);
$categoryRoute = new Route('/browser.php/category/{category_name}', ['_controller' => CategoryController::class]);
$starRoute = new Route('/browser.php/star/{product_id}', ['_controller' => StarController::class]);
$indexRoute = new Route('/browser.php', ['_controller' => IndexController::class]);
$routes = new RouteCollection();
$routes->add('product_show', $productRoute);
$routes->add('search_show', $searchRoute);
$routes->add('category_show', $categoryRoute);
$routes->add('start_show', $starRoute);
$routes->add('index_show', $indexRoute);
$context = new RequestContext();
$matcher = new UrlMatcher($routes, $context);
try {
$parameters = $matcher->match($_SERVER['REQUEST_URI']);
} catch (Exception $e) {
die($e->getMessage());
}
match ($parameters['_controller']) {
SearchController::class => (new $parameters['_controller']())($parameters['search_term']),
CategoryController::class => (new $parameters['_controller']())($parameters['category_name']),
ProductController::class, StarController::class => (new $parameters['_controller']())($parameters['product_id']),
IndexController::class => (new $parameters['_controller']())(),
default => throw new Exception('Route not found')
};

19
console.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
include_once 'vendor/autoload.php';
use Krzysiej\RyobiCrawler\Command\Migrate;
use Krzysiej\RyobiCrawler\Command\ScrapeWebsite;
use Symfony\Component\Console\Application;
if (php_sapi_name() !== 'cli') {
header('Location: browser.php');
echo 'Execute this script in cli only';
exit;
}
$application = new Application('Ryobi website scraper application', '1.1.1');
$application->add(new ScrapeWebsite());
$application->add(new Migrate());
$application->run();

View File

@@ -2,18 +2,44 @@
include_once 'vendor/autoload.php'; include_once 'vendor/autoload.php';
use Krzysiej\RyobiCrawler\Command\Migrate; use Krzysiej\RyobiCrawler\Controller\CategoryController;
use Krzysiej\RyobiCrawler\Command\ScrapeWebsite; use Krzysiej\RyobiCrawler\Controller\IndexController;
use Krzysiej\RyobiCrawler\Controller\ProductController;
use Krzysiej\RyobiCrawler\Controller\SearchController;
use Krzysiej\RyobiCrawler\Controller\StarController;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Console\Application; if (!file_exists('database.sqlite')) {
exit('Database file <code>database.sqlite</code> missing. Run docker compose <blockquote>docker compose exec php-app php index.php app:migrate</blockquote> to create it.');
}
$productRoute = new Route('/product/{product_id}', ['_controller' => ProductController::class], ['product_id' => '\d+']);
$searchRoute = new Route('/?search={search_term}', ['_controller' => SearchController::class]);
$categoryRoute = new Route('/category/{category_name}', ['_controller' => CategoryController::class]);
$starRoute = new Route('/star/{product_id}', ['_controller' => StarController::class]);
$indexRoute = new Route('/', ['_controller' => IndexController::class]);
$routes = new RouteCollection();
if (php_sapi_name() !== 'cli') { $routes->add('product_show', $productRoute);
header('Location: browser.php'); $routes->add('search_show', $searchRoute);
echo 'Execute this script in cli only'; $routes->add('category_show', $categoryRoute);
exit; $routes->add('start_show', $starRoute);
$routes->add('index_show', $indexRoute);
$context = new RequestContext();
$matcher = new UrlMatcher($routes, $context);
try {
$parameters = $matcher->match($_SERVER['REQUEST_URI']);
} catch (Exception $e) {
die($e->getMessage());
} }
$application = new Application('Ryobi website scraper application', '1.1.1'); match ($parameters['_controller']) {
$application->add(new ScrapeWebsite()); SearchController::class => (new $parameters['_controller']())($parameters['search_term']),
$application->add(new Migrate()); CategoryController::class => (new $parameters['_controller']())($parameters['category_name']),
$application->run(); ProductController::class, StarController::class => (new $parameters['_controller']())($parameters['product_id']),
IndexController::class => (new $parameters['_controller']())(),
default => throw new Exception('Route not found')
};

View File

@@ -3,14 +3,14 @@
{% block content %} {% block content %}
<table class='table table-hover'> <table class='table table-hover'>
<tr> <tr>
<td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="/browser.php/star/{{ product.id }}">{% if product.starred %}{% else %}{% endif %}</a></td> <td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="/star/{{ product.id }}">{% if product.starred %}{% else %}{% endif %}</a></td>
<td><img src='{{ product.image }}&width=150' class='img-fluid' alt='{{ product.name }}'/></td> <td><img src='{{ product.image }}&width=150' class='img-fluid' alt='{{ product.name }}'/></td>
<td><a href='/browser.php/product/{{ product.id }}'>{{ product.name }}</a></td> <td><a href='/product/{{ product.id }}'>{{ product.name }}</a></td>
<td>{{ product.subTitle }}</td> <td>{{ product.subTitle }}</td>
<td> <td>
<ul class='nav'> <ul class='nav'>
{% for category in product.categories %} {% for category in product.categories %}
<li class="nav-item"><a class="nav-link" href="/browser.php/category/{{ category }}"> {{ category }} </a></li> <li class="nav-item"><a class="nav-link" href="/category/{{ category }}"> {{ category }} </a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</td> </td>

View File

@@ -4,14 +4,14 @@
<table class='table table-hover'> <table class='table table-hover'>
{% for product in products %} {% for product in products %}
<tr> <tr>
<td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="/browser.php/star/{{ product.id }}">{% if product.starred %}{% else %}{% endif %}</a></td> <td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="/star/{{ product.id }}">{% if product.starred %}{% else %}{% endif %}</a></td>
<td><img src='{{ product.image }}&width=70' class='img-fluid' alt='{{ product.name }}'/></td> <td><img src='{{ product.image }}&width=70' class='img-fluid' alt='{{ product.name }}'/></td>
<td class="align-middle"><a href='/browser.php/product/{{ product.id }}'>{{ product.name }}</a></td> <td class="align-middle"><a href='/product/{{ product.id }}'>{{ product.name }}</a></td>
<td class="align-middle">{{ product.subTitle }}</td> <td class="align-middle">{{ product.subTitle }}</td>
<td class="align-middle"> <td class="align-middle">
<ul class='nav'> <ul class='nav'>
{% for category in product.categories %} {% for category in product.categories %}
<li class="nav-item"><a class="nav-link" href="/browser.php/category/{{ category }}"> {{ category }} </a></li> <li class="nav-item"><a class="nav-link" href="/category/{{ category }}"> {{ category }} </a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</td> </td>

View File

@@ -11,8 +11,8 @@
<body> <body>
<nav class="navbar sticky-top bg-body-tertiary border-bottom border-secondary border-1"> <nav class="navbar sticky-top bg-body-tertiary border-bottom border-secondary border-1">
<div class="container-fluid"> <div class="container-fluid">
<a class="navbar-brand" href="/browser.php">Crawler</a> <a class="navbar-brand" href="/">Crawler</a>
<form class="d-flex w-50" role="search"> <form class="d-flex w-50" role="search" action="/">
<input class="form-control me-2" type="search" name="search" placeholder="Search term eg. 36v or RCS18X" value="{{ search }}" aria-label="Search"> <input class="form-control me-2" type="search" name="search" placeholder="Search term eg. 36v or RCS18X" value="{{ search }}" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button> <button class="btn btn-outline-success" type="submit">Search</button>
</form> </form>